Skip to content

Commit 8681223

Browse files
StickyScrolling: Exceptions while opening Java editors from Search view
The search view with "reuse editor" enabled sets new source code in the existing editor and causes a resize. In this call, it can happen that the sticky line number exceeds the amount of line in the new source code. In general this is not a problem since the sticky lines will be recalculated afterwards, but to avoid IllegalArgumentException it is checked for this case. Fixes #2678
1 parent d876bc7 commit 8681223

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ private void addSourceViewerListeners() {
407407
controlListener= new ControlListener() {
408408
@Override
409409
public void controlResized(ControlEvent e) {
410+
if (areStickyLinesOutDated(textWidget)) {
411+
return;
412+
}
410413
limitVisibleStickyLinesToTextWidgetHeight(textWidget);
411414
layoutStickyLines();
412415
if (stickyScrollingHandler != null) {
@@ -422,6 +425,24 @@ public void controlMoved(ControlEvent e) {
422425
textWidget.addControlListener(controlListener);
423426
}
424427

428+
/**
429+
* Checks if the sticky lines are out dated. Specifically, it verifies that the
430+
* line number of the last sticky line does not exceed the total line count of
431+
* the source viewer.
432+
*
433+
* This situation can occur, for example, when an editor is opened via the
434+
* search view and "reuse editor" is enabled. In such cases, the text in the
435+
* source viewer is replaced, but the out dated sticky lines associated with the
436+
* previous source code remain in the first call.
437+
*/
438+
private boolean areStickyLinesOutDated(StyledText textWidget) {
439+
if (stickyLines.size() > 0) {
440+
int lastStickyLineNumber = stickyLines.get(stickyLines.size() - 1).getLineNumber();
441+
return lastStickyLineNumber > textWidget.getLineCount();
442+
}
443+
return true;
444+
}
445+
425446
private void limitVisibleStickyLinesToTextWidgetHeight(StyledText textWidget) {
426447
int lineHeight= textWidget.getLineHeight() + textWidget.getLineSpacing();
427448
int textWidgetHeight= textWidget.getBounds().height;

tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,25 @@ public void testLayoutStickyLinesCanvasOnResize() {
252252
assertEquals(boundsAfterResize.height, boundsBeforeResize.height);
253253
}
254254

255+
@Test
256+
public void testDontLayoutOutDatedStickyLinesOnResize() {
257+
sourceViewer.getTextWidget().setBounds(0, 0, 200, 200);
258+
259+
List<IStickyLine> stickyLines = List.of(new StickyLineStub("line 10", 10));
260+
stickyScrollingControl.setStickyLines(stickyLines);
261+
262+
Canvas stickyControlCanvas = getStickyControlCanvas(shell);
263+
Rectangle boundsBeforeResize = stickyControlCanvas.getBounds();
264+
265+
sourceViewer.getTextWidget().setBounds(0, 0, 150, 200);
266+
267+
stickyControlCanvas = getStickyControlCanvas(shell);
268+
Rectangle boundsAfterResize = stickyControlCanvas.getBounds();
269+
270+
// No IllegalArgumentException: Index out of bounds
271+
assertEquals(boundsAfterResize, boundsBeforeResize);
272+
}
273+
255274
@Test
256275
public void testNavigateToStickyLine() {
257276
String text = """

0 commit comments

Comments
 (0)