Skip to content

Commit

Permalink
Fix IllegalArgumentException when styling the sticky lines
Browse files Browse the repository at this point in the history
When the sticky lines are limited by the settings, the not visible sticky lines should not be styled.

Fixes eclipse-platform#2496
  • Loading branch information
Christopher-Hermann committed Nov 5, 2024
1 parent 136afb4 commit 91383e0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,20 @@ private void styleStickyLines() {
return;
}

int stickyLineOffset= 0;
List<StyleRange> styleRanges= new ArrayList<>();
for (IStickyLine stickyLine : stickyLines) {
List<StyleRange> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IStickyLine> 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);
Expand Down

0 comments on commit 91383e0

Please sign in to comment.