Prevent IllegalStateException setting visible region with collapsed projection region #3456
+145
−1
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.
When collapsing projection regions and then setting the visible region in a certain way and then changing it to the surrounding visible region, it is possible that consecutive
ProjectionMapping#toImageLinecalls throw anIllegalStateException. This is becauseProjectionDocument#internalRemoveMasterDocumentRangeadds aPositionto the document when aPositionwith the same offset but a length of0can be present already. In this case,ISynchronizableDocument#addPosition(which is supposed to insert it into the sorted array) might insert it before thePositionresulting in the fragments being sorted incorrectly (the 0-length fragment after the positive-length fragment with both having the same offset). This can then result inIllegalStateExceptions when callingProjectionMapping#toImageLine(which happens while drawing line numbers for example).A similar issue also happens in
ProjectionDocument#internalAddMasterDocumentRange. There, it is possible that a region is expanded to the right in a way that it fully surrounds the region on the right. While expansion is normally handled bySegments (ranges in the slave document), theFragments are not correctly reordered in this case causingcomputeProjectedMasterRegions(int offsetInMaster, int lengthInMaster)not finding the region (at least that's what happened with my reproducer) and hence not being able to remove it from the editor. In the situation where oneFragmentincludes (is a "superset" of) another, it should be safe to delete the smaller one fixing the invalid state.This change fixes these problems.
Standalone reproducer/test
To reproduce the issue, run the test added with this PR without the changes to
ProjectionDocument.Without the fix, the tests throw exceptions similar to the following:
and
Reproducing with JDT (patch)
This can be reproduced with JDT using the code from this PR I wrote following these steps:
b()methoda()in the outline (does not happen with the other methods) to only show that methodTMclass in the outlineTMclass in the outline but instead theb()method, thec()method will be shown incorrectly.