[JIMT][CT-482] - fixed editor jumping #57867
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
CodeBridge has an issue where any typing would jump the position of the cursor back to the start.
I traced it back to the
lab2/CodeEditor
component itself, and specifically the seconduseEffect
hook. What's happening is that thestartCode
value is changing because something new is handed in (this is part of the external loop where the editor caused a change, which fires off an action, which updates state, which eventually circles back to the component itself with a "new" file that's only what the user had just typed.Fix is easy - in the effect, just compare the current value of the editor's doc and only set it to the new one if it's something different.
Side note - the original version of this fix operated differently. Instead of checking to see if the values had changed, it would just set the new string and reset the selection to what it was previously. That worked, but was concerning because if the file itself actually changed (e.g., we have the same editor open and were working with File A, and have now switched to File B), we could end up with the cursor in a random position in the new file. The way the code exists now, an external file change would always reset the cursor position back to the start, which is probably what we want.
One odd minor edge case could be if the file contents change, but it's still the same one. For example, if we format the file. We'll still reset the cursor position back to the beginning of the file, even though that may not be what the user would like. Ideally, the cursor would stay in the same position in the modified file. AFAIK, fixing that edge case would not be trivial in the general case and I think we should ignore it. This potential edge case is much smaller surface area than the original one with preserving the selection.