From 91383e04ad978e4893a684c59aa203887454fcf1 Mon Sep 17 00:00:00 2001 From: Christopher Hermann Date: Tue, 5 Nov 2024 15:53:29 +0100 Subject: [PATCH] Fix IllegalArgumentException when styling the sticky lines When the sticky lines are limited by the settings, the not visible sticky lines should not be styled. Fixes #2496 --- .../stickyscroll/StickyScrollingControl.java | 16 ++++++------- .../StickyScrollingControlTest.java | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java b/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java index f4927a3fe28..0e02cbd4980 100644 --- a/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java +++ b/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java @@ -242,20 +242,20 @@ private void styleStickyLines() { return; } - int stickyLineOffset= 0; - List styleRanges= new ArrayList<>(); - for (IStickyLine stickyLine : stickyLines) { + List stickyLinesStyleRanges= new ArrayList<>(); + int stickyLineTextOffset= 0; + for (int i= 0; i < getNumberStickyLines(); i++) { + IStickyLine stickyLine= stickyLines.get(i); StyleRange[] ranges= stickyLine.getStyleRanges(); if (ranges != null) { for (StyleRange styleRange : ranges) { - styleRange.start+= stickyLineOffset; - styleRanges.add(styleRange); + styleRange.start+= stickyLineTextOffset; + stickyLinesStyleRanges.add(styleRange); } } - - stickyLineOffset+= stickyLine.getText().length() + System.lineSeparator().length(); + stickyLineTextOffset+= stickyLine.getText().length() + System.lineSeparator().length(); } - stickyLineText.setStyleRanges(styleRanges.toArray(StyleRange[]::new)); + stickyLineText.setStyleRanges(stickyLinesStyleRanges.toArray(StyleRange[]::new)); stickyLineNumber.setFont(textWidget.getFont()); stickyLineNumber.setStyleRange(new StyleRange(0, stickyLineNumber.getText().length(), settings.lineNumberColor(), null)); diff --git a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java index ce8664daf8f..d9442aa9ec5 100644 --- a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java +++ b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java @@ -182,6 +182,29 @@ public void testCopyStyleRanges() { assertEquals(separatorColor, styleRanges[1].background); } + @Test + public void testCopyStyleRangesWithLimitedStickyLines() { + settings = new StickyScrollingControlSettings(1, lineNumberColor, hoverColor, backgroundColor, separatorColor, + true); + stickyScrollingControl.applySettings(settings); + + StyleRange styleRangeLine1 = new StyleRange(0, 1, lineNumberColor, backgroundColor); + StyleRange styleRangeLine2 = new StyleRange(0, 2, hoverColor, separatorColor); + List stickyLines = List.of(// + new StickyLineStub("line 1", 0, new StyleRange[] { styleRangeLine1 }), + new StickyLineStub("line 2", 0, new StyleRange[] { styleRangeLine2 })); + stickyScrollingControl.setStickyLines(stickyLines); + + StyledText stickyLineText = getStickyLineText(); + + StyleRange[] styleRanges = stickyLineText.getStyleRanges(); + assertEquals(1, styleRanges.length); + assertEquals(0, styleRanges[0].start); + assertEquals(1, styleRanges[0].length); + assertEquals(lineNumberColor, styleRanges[0].foreground); + assertEquals(backgroundColor, styleRanges[0].background); + } + @Test public void testWithoutVerticalRuler() { sourceViewer = new SourceViewer(shell, null, SWT.None);