Skip to content

Commit 6c2a6d9

Browse files
wahlbrinkBeckerWdf
authored andcommitted
Fix computation of the indentation in DefaultStickyLinesProvider
Fixes: #3254
1 parent c2fe144 commit 6c2a6d9

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.internal.texteditor.stickyscroll;
1515

16-
import java.util.Collections;
1716
import java.util.LinkedList;
1817
import java.util.List;
1918

@@ -35,8 +34,6 @@ public class DefaultStickyLinesProvider implements IStickyLinesProvider {
3534

3635
private final static int IGNORE_LINE_INDENTATION= -1;
3736

38-
private final static String TAB= "\t"; //$NON-NLS-1$
39-
4037
private StickyLinesProperties fProperties;
4138

4239
@Override
@@ -101,13 +98,22 @@ private String getPreviousContentLine(int startFromLine, StyledText styledText)
10198
}
10299

103100
private int getIndentation(String line) {
104-
if (line == null || line.isBlank()) {
105-
return IGNORE_LINE_INDENTATION;
101+
if (line != null && !line.isBlank()) {
102+
int indent = 0;
103+
for (int i = 0; i < line.length(); i++) {
104+
switch (line.charAt(i)) {
105+
case ' ':
106+
indent++;
107+
continue;
108+
case '\t':
109+
indent += fProperties.tabWith() - (indent % fProperties.tabWith());
110+
continue;
111+
default:
112+
return indent;
113+
}
114+
}
106115
}
107-
String tabAsSpaces= String.join("", Collections.nCopies(fProperties.tabWith(), " ")); //$NON-NLS-1$ //$NON-NLS-2$
108-
109-
line= line.replace(TAB, tabAsSpaces);
110-
return line.length() - line.stripLeading().length();
116+
return IGNORE_LINE_INDENTATION;
111117
}
112118

113119
private int mapLineNumberToWidget(ISourceViewer sourceViewer, int line) {

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ public void testIgnoreEmptyLines() {
137137
assertEquals(2, stickyLines.get(1).getLineNumber());
138138
}
139139

140+
@Test
141+
public void testIgnoreOtherBlankLines() {
142+
String text = """
143+
line 1
144+
\t
145+
line 2
146+
\f
147+
line 3<""";
148+
textWidget.setText(text);
149+
150+
List<IStickyLine> stickyLines = stickyLinesProvider.getStickyLines(sourceViewer, 4, stickyLinesProperties);
151+
152+
assertEquals(2, stickyLines.size());
153+
assertEquals(0, stickyLines.get(0).getLineNumber());
154+
assertEquals(2, stickyLines.get(1).getLineNumber());
155+
}
156+
140157
@Test
141158
public void testLinesWithTabs() {
142159
stickyLinesProperties = new StickyLinesProperties(2, editorPart);
@@ -153,6 +170,24 @@ public void testLinesWithTabs() {
153170
assertEquals(1, stickyLines.get(1).getLineNumber());
154171
}
155172

173+
@Test
174+
public void testLinesWithTabsAndSpaces() {
175+
// tab witdh 4
176+
String text = """
177+
line 1
178+
\tline 2
179+
\t line 3
180+
\t\tline 4<""";
181+
textWidget.setText(text);
182+
183+
List<IStickyLine> stickyLines = stickyLinesProvider.getStickyLines(sourceViewer, 3, stickyLinesProperties);
184+
185+
assertEquals(3, stickyLines.size());
186+
assertEquals(0, stickyLines.get(0).getLineNumber());
187+
assertEquals(1, stickyLines.get(1).getLineNumber());
188+
assertEquals(2, stickyLines.get(2).getLineNumber());
189+
}
190+
156191
@Test
157192
public void testStartAtEmptyLineWithNext() {
158193
String text = """

0 commit comments

Comments
 (0)