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 e1acc16ddd2..07452e4d2ee 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 @@ -77,6 +77,15 @@ */ public class StickyScrollingControl { + /** + * This threshold represents the minimum number of source lines that must remain visible in the + * editor. If the StickyScrollingControl's size would result in fewer than this number of lines + * being visible, the height of the StickyScrollingControl will be reduced to ensure visibility + * of at least this many lines. Thus, it guarantees a minimum visibility threshold for the + * source content in the editor underneath the StickyScrollingControl. + */ + private final static int MIN_VISIBLE_EDITOR_LINES_THRESHOLD= 3; + private List stickyLines; private ISourceViewer sourceViewer; @@ -103,6 +112,8 @@ public class StickyScrollingControl { private StickyScrollingHandler stickyScrollingHandler; + private int maximumVisibleStickyLines= Integer.MAX_VALUE; + public StickyScrollingControl(ISourceViewer sourceViewer, StickyScrollingControlSettings settings) { this(sourceViewer, null, settings, null); } @@ -185,6 +196,7 @@ private void createControls() { bottomSeparator.setEnabled(false); layoutLineNumbers(); + limitVisibleStickyLinesToTextWidgetHeight(sourceViewer.getTextWidget()); stickyLinesCanvas.pack(); stickyLinesCanvas.moveAbove(null); @@ -379,7 +391,9 @@ private void ensureSourceViewerLineVisible(int line) { } private int getNumberStickyLines() { - return Math.min(settings.maxCountStickyLines(), this.stickyLines.size()); + int numberStickyLines= Math.min(settings.maxCountStickyLines(), this.stickyLines.size()); + numberStickyLines= Math.min(maximumVisibleStickyLines, numberStickyLines); + return numberStickyLines; } /** @@ -412,7 +426,12 @@ private void addSourceViewerListeners() { controlListener= new ControlListener() { @Override public void controlResized(ControlEvent e) { + StyledText textWidget= sourceViewer.getTextWidget(); + limitVisibleStickyLinesToTextWidgetHeight(textWidget); layoutStickyLines(); + if (stickyScrollingHandler != null) { + stickyScrollingHandler.viewportChanged(textWidget.getTopPixel()); + } } @Override @@ -423,6 +442,16 @@ public void controlMoved(ControlEvent e) { sourceViewer.getTextWidget().addControlListener(controlListener); } + private void limitVisibleStickyLinesToTextWidgetHeight(StyledText textWidget) { + int lineHeight= textWidget.getLineHeight() + textWidget.getLineSpacing(); + int textWidgetHeight= textWidget.getBounds().height; + + int visibleLinesInTextWidget= textWidgetHeight / lineHeight; + + maximumVisibleStickyLines= Math.max(0, visibleLinesInTextWidget - MIN_VISIBLE_EDITOR_LINES_THRESHOLD); + updateStickyScrollingControls(); + } + /** * Sets the cursor on the canvas to {@link SWT#CURSOR_HAND} and adds several mouse listeners to * the canvas.
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 a98e2ec183c..0e4363abf0a 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 @@ -62,6 +62,7 @@ public void setup() { ruler = new CompositeRuler(); sourceViewer = new SourceViewer(shell, ruler, SWT.V_SCROLL | SWT.H_SCROLL); sourceViewer.setDocument(new Document()); + sourceViewer.getTextWidget().setBounds(0, 0, 200, 200); lineNumberColor = new Color(0, 0, 0); hoverColor = new Color(1, 1, 1); @@ -116,12 +117,10 @@ public void testLimitStickyLinesCount() { stickyScrollingControl.applySettings(settings); StyledText stickyLineNumber = getStickyLineNumber(); - String expLineNumber = """ - 10"""; + String expLineNumber = "10"; assertEquals(expLineNumber, stickyLineNumber.getText()); StyledText stickyLineText = getStickyLineText(); - String expStickyLineText = """ - line 10"""; + String expStickyLineText = "line 10"; assertEquals(expStickyLineText, stickyLineText.getText()); } @@ -215,6 +214,7 @@ public void testNavigateToStickyLine() { @Test public void testVerticalScrollingIsDispatched() { + sourceViewer.getTextWidget().setBounds(0, 0, 200, 0); Canvas stickyControlCanvas = getStickyControlCanvas(shell); String text = """ line 1 @@ -232,6 +232,7 @@ public void testVerticalScrollingIsDispatched() { @Test public void testHorizontalScrollingIsDispatched() { + sourceViewer.getTextWidget().setBounds(0, 0, 0, 200); Canvas stickyControlCanvas = getStickyControlCanvas(shell); String text = """ line 1 @@ -247,6 +248,20 @@ public void testHorizontalScrollingIsDispatched() { assertEquals(10, sourceViewer.getTextWidget().getHorizontalPixel()); } + @Test + public void limitStickyLinesToTextWidgetHeight() { + sourceViewer.getTextWidget().setBounds(0, 0, 200, 200); + List stickyLines = List.of(new StickyLine("line 2", 1)); + stickyScrollingControl.setStickyLines(stickyLines); + + StyledText stickyLineText = getStickyLineText(); + assertEquals("line 2", stickyLineText.getText()); + + sourceViewer.getTextWidget().setBounds(0, 0, 200, 20); + stickyLineText = getStickyLineText(); + assertEquals("", stickyLineText.getText()); + } + private Canvas getStickyControlCanvas(Composite composite) { for (Control control : composite.getChildren()) { if (control instanceof Canvas canvas) { diff --git a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingHandlerTest.java b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingHandlerTest.java index 948986466b4..17bb7e5e60b 100644 --- a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingHandlerTest.java +++ b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingHandlerTest.java @@ -64,6 +64,7 @@ public void setup() { ruler = new CompositeRuler(); sourceViewer = new SourceViewer(shell, ruler, SWT.None); sourceViewer.setDocument(new Document()); + sourceViewer.getTextWidget().setBounds(0, 0, 200, 200); lineNumberColor = new Color(0, 0, 0); hoverColor = new Color(1, 1, 1);