2121import static org .eclipse .ui .texteditor .AbstractDecoratedTextEditorPreferenceConstants .EDITOR_TAB_WIDTH ;
2222
2323import java .time .Duration ;
24+ import java .util .Collections ;
2425import java .util .List ;
2526
2627import org .eclipse .swt .graphics .Color ;
3738import org .eclipse .jface .text .source .IVerticalRuler ;
3839
3940import org .eclipse .ui .internal .editors .text .EditorsPlugin ;
41+ import org .eclipse .ui .internal .texteditor .stickyscroll .IStickyLinesProvider .StickyLinesProperties ;
4042
4143/**
42- * A sticky scrolling handler that retrieves stick lines from the {@link StickyLinesProvider } and
44+ * A sticky scrolling handler that retrieves stick lines from a {@link IStickyLinesProvider } and
4345 * shows them in a {@link StickyScrollingControl} on top of the given source viewer.
4446 */
4547public class StickyScrollingHandler implements IViewportListener {
@@ -50,94 +52,95 @@ public class StickyScrollingHandler implements IViewportListener {
5052
5153 private StickyScrollingControl stickyScrollingControl ;
5254
53- private int tabWidth ;
54-
5555 private IPropertyChangeListener propertyChangeListener ;
5656
5757 private IPreferenceStore preferenceStore ;
5858
59- private StickyLinesProvider stickyLinesProvider ;
59+ private IStickyLinesProvider stickyLinesProvider ;
60+
61+ private StickyLinesProperties stickyLinesProperties ;
6062
6163 private Throttler throttler ;
6264
6365 private int verticalOffset ;
6466
6567 /**
66- * Creates a StickyScrollingHandlerIndentation that will be linked to the given source viewer.
67- * The sticky scrolling will be computed by the default {@link StickyLinesProvider }.
68+ * Creates a StickyScrollingHandler that will be linked to the given source viewer. The sticky
69+ * lines will be provided by the {@link DefaultStickyLinesProvider }.
6870 *
69- * @param sourceViewer The source viewer to link the handler
71+ * @param sourceViewer The source viewer to link the handler with
7072 * @param verticalRuler The vertical ruler of the source viewer
7173 * @param preferenceStore The preference store
7274 */
7375 public StickyScrollingHandler (ISourceViewer sourceViewer , IVerticalRuler verticalRuler , IPreferenceStore preferenceStore ) {
74- this (sourceViewer , verticalRuler , preferenceStore , new StickyLinesProvider ());
76+ this (sourceViewer , verticalRuler , preferenceStore , new DefaultStickyLinesProvider ());
7577 }
7678
7779 /**
78- * Creates a StickyScrollingHandlerIndentation that will be linked to the given source viewer.
80+ * Creates a StickyScrollingHandler that will be linked to the given source viewer. The sticky
81+ * lines will be provided by the given <code>stickyLinesProvider</code>.
7982 *
80- * @param sourceViewer The source viewer to link the handler
83+ * @param sourceViewer The source viewer to link the handler with
8184 * @param verticalRuler The vertical ruler of the source viewer
8285 * @param preferenceStore The preference store
83- * @param stickyLinesProvider The sticky scrolling computer
86+ * @param stickyLinesProvider The sticky scrolling provider
8487 */
8588 public StickyScrollingHandler (ISourceViewer sourceViewer , IVerticalRuler verticalRuler , IPreferenceStore preferenceStore ,
86- StickyLinesProvider stickyLinesProvider ) {
89+ IStickyLinesProvider stickyLinesProvider ) {
8790 this .sourceViewer = sourceViewer ;
8891
8992 throttler = new Throttler (sourceViewer .getTextWidget ().getDisplay (), Duration .ofMillis (THROTTLER_DELAY ), this ::calculateAndShowStickyLines );
9093 this .stickyLinesProvider = stickyLinesProvider ;
9194
92- StickyScrollingControlSettings settings = loadAndListenForProperties (preferenceStore );
95+ listenForPropertiesChanges (preferenceStore );
96+ stickyLinesProperties = loadStickyLinesProperties (preferenceStore );
97+ StickyScrollingControlSettings settings = loadControlSettings (preferenceStore );
98+
9399 stickyScrollingControl = new StickyScrollingControl (sourceViewer , verticalRuler , settings , this );
94100
95101 sourceViewer .addViewportListener (this );
96102 }
97103
98- private StickyScrollingControlSettings loadAndListenForProperties (IPreferenceStore store ) {
104+ private void listenForPropertiesChanges (IPreferenceStore store ) {
99105 preferenceStore = store ;
100106 propertyChangeListener = e -> {
101107 if (e .getProperty ().equals (EDITOR_TAB_WIDTH ) || e .getProperty ().equals (EDITOR_STICKY_SCROLLING_MAXIMUM_COUNT )
102108 || e .getProperty ().equals (EDITOR_CURRENT_LINE_COLOR ) || e .getProperty ().equals (EDITOR_LINE_NUMBER_RULER )
103109 || e .getProperty ().equals (STICKY_LINES_SEPARATOR_COLOR )) {
104110 if (stickyScrollingControl != null && !sourceViewer .getTextWidget ().isDisposed ()) {
105- StickyScrollingControlSettings settings = loadSettings (preferenceStore );
111+ StickyScrollingControlSettings settings = loadControlSettings (preferenceStore );
106112 stickyScrollingControl .applySettings (settings );
107- stickyLinesProvider . setTabWidth ( tabWidth );
113+ stickyLinesProperties = loadStickyLinesProperties ( preferenceStore );
108114 }
109115 }
110116 };
111117 store .addPropertyChangeListener (propertyChangeListener );
112- return loadSettings (store );
113118 }
114119
115- private StickyScrollingControlSettings loadSettings (IPreferenceStore store ) {
116- tabWidth = store .getInt (EDITOR_TAB_WIDTH );
117-
120+ private StickyScrollingControlSettings loadControlSettings (IPreferenceStore store ) {
118121 int stickyScrollingMaxCount = store .getInt (EDITOR_STICKY_SCROLLING_MAXIMUM_COUNT );
119122
120123 Color lineNumberColor = new Color (PreferenceConverter .getColor (store , EDITOR_LINE_NUMBER_RULER_COLOR ));
121124 sourceViewer .getTextWidget ().addDisposeListener (e -> lineNumberColor .dispose ());
122-
123125 Color stickyLineHoverColor = new Color (PreferenceConverter .getColor (store , EDITOR_CURRENT_LINE_COLOR ));
124126 sourceViewer .getTextWidget ().addDisposeListener (e -> stickyLineHoverColor .dispose ());
125-
126127 Color stickyLineBackgroundColor = sourceViewer .getTextWidget ().getBackground ();
127-
128128 boolean showLineNumbers = store .getBoolean (EDITOR_LINE_NUMBER_RULER );
129-
130129 Color stickyLineSeparatorColor = null ;
131130 if (EditorsPlugin .getDefault () != null ) {
132131 RGB rgb = PreferenceConverter .getColor (store , STICKY_LINES_SEPARATOR_COLOR );
133132 ISharedTextColors sharedTextColors = EditorsPlugin .getDefault ().getSharedTextColors ();
134133 stickyLineSeparatorColor = sharedTextColors .getColor (rgb );
135134 }
136-
137135 return new StickyScrollingControlSettings (stickyScrollingMaxCount ,
138136 lineNumberColor , stickyLineHoverColor , stickyLineBackgroundColor , stickyLineSeparatorColor , showLineNumbers );
139137 }
140138
139+ private StickyLinesProperties loadStickyLinesProperties (IPreferenceStore store ) {
140+ int tabWidth = store .getInt (EDITOR_TAB_WIDTH );
141+ return new StickyLinesProperties (tabWidth );
142+ }
143+
141144 @ Override
142145 public void viewportChanged (int newVerticalOffset ) {
143146 if (this .verticalOffset == newVerticalOffset ) {
@@ -148,7 +151,10 @@ public void viewportChanged(int newVerticalOffset) {
148151 }
149152
150153 private void calculateAndShowStickyLines () {
151- List <StickyLine > stickyLines = stickyLinesProvider .get (verticalOffset , sourceViewer );
154+ List <StickyLine > stickyLines = stickyLinesProvider .getStickyLines (sourceViewer , stickyLinesProperties );
155+ if (stickyLines == null ) {
156+ stickyLines = Collections .emptyList ();
157+ }
152158 stickyScrollingControl .setStickyLines (stickyLines );
153159 }
154160
0 commit comments