JetBrains / MPS-extensions

MPS-extensions aims to ease language development within MPS.
https://jetbrains.github.io/MPS-extensions/
Apache License 2.0
83 stars 51 forks source link

Prevent IndexOutOfBounds in Multiline EditorCells #663

Open dalbrecht1 opened 1 year ago

dalbrecht1 commented 1 year ago

In de.slisson.mps.editor.multiline.runtime.MultilineSelection there is a situation in which editing the displayed content of a Multiline EditorCell can cause OutOfBoundsExceptions that makes the handy { property }\n EditorCell hard to use.

The situation though is pretty specific. The prerequisite for the exception to occur is that the content of this property has to be a single word, so no spaces in the string and also no other white-spaces or line-breaks. If you then go to any position in the string and mark letters (with the keyboard while holding Shift), then deleting/overwriting/copying/pasting/etc the content works.

If however, you trigger Shift+Arrow-key once more after you already reached either the start or the end of the content, then the selection changes (if for example you selected the whole word before, the selection box gets slightly bigger, I think that this tries to indicate that you selected more than one word, because the same behavior in the selection box is noticeable if you select several words in these cells across some white-spaces) and attempting to edit the content then will result in the mentioned exception. The (technical) problem is that one of the two selection indexes (probably selectionEnd) becomes either -1 or string-property.length respectively depending on whether you "overstepped" the start of end of the word, which the Selection-helper class can't handle.

I debugged the problem to this extent and also thought about fixing this problem myself. but I wasn't sure where to actually fix this. Intuitively, the selection index should never become invalid in the first place, but I couldn't pin-point the line of code that increments/decrements the index even though it already reached its valid limits. There are already checks in place that should prevent the indexes from becoming invalid. Additionally, I wasn't sure whether these invalid indexes are used to draw the different selection box, so capping the index would cause visual changes (though I'm not sure if anyone would actually NEED the different box for anything). Alternatively, in this helper we should prevent either that the indexes are set to anything invalid (within the setSelectionRange method) or do additional checks each time these indexes are used.

The issue is observable both in MPS 2020.3 and 2022.2 (and probably every version in between) and can be fixed for any version that is currently in maintenance.

Node-Ref to the Selection-helper when including the extensions as dependency, not sure if that works as well when opening the extensions to edit