Skip to content

Commit 41ef29b

Browse files
committed
Display CodeLens according the leading spaces. See
#181
1 parent c8ab622 commit 41ef29b

File tree

6 files changed

+77
-71
lines changed

6 files changed

+77
-71
lines changed

eclipse/codelens/org.eclipse.codelens/src/org/eclipse/jface/text/provisional/codelens/CodeLensStrategy.java

+18-26
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
2121
import org.eclipse.swt.custom.StyledText;
2222
import org.eclipse.swt.custom.patch.StyledTextPatcher;
23-
import org.eclipse.swt.widgets.Display;
2423

2524
// /vscode/src/vs/editor/contrib/codelens/common/codelens.ts
2625
public class CodeLensStrategy implements IReconcilingStrategy {
@@ -43,21 +42,15 @@ public CodeLensStrategy(ITextViewer textViewer, boolean invalidateTextPresentati
4342
this.textViewer = textViewer;
4443
this.invalidateTextPresentation = invalidateTextPresentation;
4544
this.targets = new ArrayList<>();
46-
textViewer.getTextWidget().getDisplay().syncExec(new Runnable() {
47-
48-
@Override
49-
public void run() {
50-
CodeLensStrategy.this.accessor = new ViewZoneChangeAccessor(textViewer);
51-
}
45+
// Initialize the view change accessor in the UI Thread because teh
46+
// constructor updat ethe StyledTextRenderer which i saccessible only in
47+
// an UI Thread.
48+
textViewer.getTextWidget().getDisplay().syncExec(() -> {
49+
CodeLensStrategy.this.accessor = new ViewZoneChangeAccessor(textViewer);
5250
});
53-
5451
this._lenses = new ArrayList<>();
5552
}
5653

57-
public void start() {
58-
onModelChange();
59-
}
60-
6154
private void onModelChange() {
6255
if (symbolsPromise != null) {
6356
symbolsPromise.cancel(true);
@@ -69,8 +62,6 @@ private void onModelChange() {
6962
e.printStackTrace();
7063
return null;
7164
});
72-
;
73-
7465
}
7566

7667
private CompletableFuture<Collection<CodeLensData>> getCodeLensData(ITextViewer textViewer, List<String> targets,
@@ -231,19 +222,20 @@ private void _onViewportChanged() {
231222
}
232223

233224
final Integer top = topMargin;
234-
Display.getDefault().syncExec(() -> {
235-
StyledText styledText = textViewer.getTextWidget();
225+
final StyledText styledText = textViewer.getTextWidget();
226+
styledText.getDisplay().syncExec(() -> {
236227
if (invalidateTextPresentation) {
237-
// if (top != null && styledText.getTopMargin() != top) {
238-
// try {
239-
// Field f = styledText.getClass().getDeclaredField("topMargin");
240-
// f.setAccessible(true);
241-
// f.set(styledText, top);
242-
// } catch (Exception e) {
243-
// // TODO Auto-generated catch block
244-
// e.printStackTrace();
245-
// }
246-
// }
228+
// if (top != null && styledText.getTopMargin() != top) {
229+
// try {
230+
// Field f =
231+
// styledText.getClass().getDeclaredField("topMargin");
232+
// f.setAccessible(true);
233+
// f.set(styledText, top);
234+
// } catch (Exception e) {
235+
// // TODO Auto-generated catch block
236+
// e.printStackTrace();
237+
// }
238+
// }
247239
textViewer.invalidateTextPresentation();
248240
} else {
249241
if (top != null && styledText.getTopMargin() != top) {

eclipse/codelens/org.eclipse.codelens/src/org/eclipse/jface/text/provisional/codelens/internal/CodeLensViewZone.java

+30-33
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void updateCommands(List<ICodeLens> resolvedSymbols) {
6262
}
6363

6464
@Override
65-
public void draw(int paintX, int paintY, GC gc) {
65+
public void draw(int paintX, int paintSpaceLeadingX, int paintY, GC gc) {
6666
StyledText styledText = super.getStyledText();
6767
Rectangle client = styledText.getClientArea();
6868
gc.setBackground(styledText.getDisplay().getSystemColor(SWT.COLOR_WHITE));
@@ -72,44 +72,19 @@ public void draw(int paintX, int paintY, GC gc) {
7272

7373
Font font = new Font(styledText.getDisplay(), "Arial", 9, SWT.ITALIC);
7474
gc.setFont(font);
75-
String text = getText(gc);
75+
String text = getText(gc, paintSpaceLeadingX);
7676
if (text != null) {
77-
Point topLeft = null;
78-
int leading = getLeadingSpaces(styledText.getLine(super.getAfterLineNumber()));
79-
if (leading > 0) {
80-
topLeft = styledText.getLocationAtOffset(super.getOffsetAtLine() + leading);
81-
paintX += topLeft.x;
82-
}
83-
int x = paintX;
8477
int y = paintY + 4;
85-
gc.drawText(text, x, y);
78+
gc.drawText(text, paintSpaceLeadingX, y);
8679

8780
if (hoveredCodeLensEndX != null) {
8881
Point extent = gc.textExtent(text);
89-
int startX = topLeft != null ? topLeft.x : 0;
90-
gc.drawLine(startX + hoveredCodeLensStartX, y + extent.y - 1, startX + hoveredCodeLensEndX,
91-
y + extent.y - 1);
82+
gc.drawLine(hoveredCodeLensStartX, y + extent.y - 1, hoveredCodeLensEndX, y + extent.y - 1);
9283
}
9384
}
9485
}
9586

96-
private static int getLeadingSpaces(String line) {
97-
int counter = 0;
98-
99-
char[] chars = line.toCharArray();
100-
for (char c : chars) {
101-
if (c == '\t')
102-
counter++;
103-
else if (c == ' ')
104-
counter++;
105-
else
106-
break;
107-
}
108-
109-
return counter;
110-
}
111-
112-
public String getText(GC gc) {
87+
public String getText(GC gc, int x) {
11388
hoveredCodeLens = null;
11489
hoveredCodeLensStartX = null;
11590
hoveredCodeLensEndX = null;
@@ -125,21 +100,43 @@ public String getText(GC gc) {
125100
}
126101
Integer startX = null;
127102
if (hasHover && hoveredCodeLens == null) {
128-
startX = gc.textExtent(text.toString()).x;
103+
startX = gc.textExtent(text.toString()).x + x;
129104
}
130105
text.append(codeLens.getCommand().getTitle());
131106
if (hasHover && hoveredCodeLens == null) {
132-
int endX = gc.textExtent(text.toString()).x;
107+
int endX = gc.textExtent(text.toString()).x + x;
133108
if (hover.x < endX) {
134109
hoveredCodeLensStartX = startX;
135110
hoveredCodeLensEndX = endX;
136111
hoveredCodeLens = codeLens;
137112
}
138-
}
113+
}
139114
i++;
140115
}
141116
return text.toString();
142117
}
143118
}
144119

120+
@Override
121+
protected int getOffsetAtLine(int lineIndex) {
122+
String line = getStyledText().getLine(lineIndex);
123+
return super.getOffsetAtLine(lineIndex) + getLeadingSpaces(line);
124+
}
125+
126+
private static int getLeadingSpaces(String line) {
127+
int counter = 0;
128+
129+
char[] chars = line.toCharArray();
130+
for (char c : chars) {
131+
if (c == '\t')
132+
counter++;
133+
else if (c == ' ')
134+
counter++;
135+
else
136+
break;
137+
}
138+
139+
return counter;
140+
}
141+
145142
}

eclipse/codelens/org.eclipse.codelens/src/org/eclipse/jface/text/provisional/viewzones/DefaultViewZone.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public String getText() {
2828
}
2929

3030
@Override
31-
public void draw(int paintX, int paintY, GC gc) {
31+
public void draw(int paintX, int paintSpaceLeadingX, int paintY, GC gc) {
3232
StyledText styledText = super.getStyledText();
3333
Rectangle client = styledText.getClientArea();
3434
gc.setBackground(styledText.getDisplay().getSystemColor(SWT.COLOR_WHITE));

eclipse/codelens/org.eclipse.codelens/src/org/eclipse/jface/text/provisional/viewzones/IViewZone.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ public interface IViewZone {
3232

3333
void onMouseClick(MouseEvent event);
3434

35-
void draw(int paintX, int paintY, GC gc);
35+
void draw(int paintX, int paintSpaceLeadingX, int paintY, GC gc);
3636
}

eclipse/codelens/org.eclipse.codelens/src/org/eclipse/jface/text/provisional/viewzones/ViewZone.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ public int getAfterLineNumber() {
4141

4242
public int getOffsetAtLine() {
4343
if (offsetAtLine == -1) {
44-
offsetAtLine = styledText.getOffsetAtLine(afterLineNumber);
44+
offsetAtLine = getOffsetAtLine(afterLineNumber);
4545
}
4646
return offsetAtLine;
4747
}
4848

49+
protected int getOffsetAtLine(int lineIndex) {
50+
return styledText.getOffsetAtLine(lineIndex);
51+
}
52+
4953
public void setOffsetAtLine(int offsetAtLine) {
5054
this.afterLineNumber = -1;
5155
this.offsetAtLine = offsetAtLine;

eclipse/codelens/org.eclipse.codelens/src/org/eclipse/jface/text/provisional/viewzones/ViewZoneChangeAccessor.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,17 @@ public ViewZoneChangeAccessor(ITextViewer textViewer) {
110110

111111
textViewer.getDocument().addDocumentListener(new IDocumentListener() {
112112

113+
private List<IViewZone> toUpdate = new ArrayList<>();
114+
113115
@Override
114116
public void documentChanged(DocumentEvent event) {
115-
117+
if (!toUpdate.isEmpty()) {
118+
textViewer.getTextWidget().getDisplay().asyncExec(() -> {
119+
for (IViewZone viewZone : toUpdate) {
120+
ViewZoneChangeAccessor.this.layoutZone(viewZone);
121+
}
122+
});
123+
}
116124
}
117125

118126
@Override
@@ -121,20 +129,25 @@ public void documentAboutToBeChanged(DocumentEvent e) {
121129
int replaceCharCount = e.getLength();
122130
int newCharCount = e.getText().length();
123131
synchronized (viewZones) {
132+
toUpdate.clear();
124133
List<IViewZone> toRemove = new ArrayList<>();
125134
for (IViewZone viewZone : viewZones) {
126135
// System.err.println("before:" +
127136
// viewZone.getAfterLineNumber());
128-
int offset = viewZone.getOffsetAtLine();
129-
if (start <= offset && offset < start + replaceCharCount) {
137+
int oldOffset = viewZone.getOffsetAtLine();
138+
int newOffset = oldOffset;
139+
if (start <= newOffset && newOffset < start + replaceCharCount) {
130140
// this zone is being deleted from the text
131141
toRemove.add(viewZone);
132-
offset = -1;
142+
newOffset = -1;
143+
}
144+
if (newOffset != -1 && newOffset >= start) {
145+
newOffset += newCharCount - replaceCharCount;
133146
}
134-
if (offset != -1 && offset >= start) {
135-
offset += newCharCount - replaceCharCount;
147+
if (oldOffset != newOffset) {
148+
viewZone.setOffsetAtLine(newOffset);
149+
toUpdate.add(viewZone);
136150
}
137-
viewZone.setOffsetAtLine(offset);
138151
// System.err.println("after:" +
139152
// viewZone.getAfterLineNumber());
140153
}
@@ -280,7 +293,7 @@ public void paintControl(PaintEvent event) {
280293
int lineCount = fTextWidget.getLineCount();
281294
int x = fTextWidget.getLeftMargin() - fTextWidget.getHorizontalPixel();
282295
// leftMargin - horizontalScrollOffset;
283-
for (int lineIndex = startLine; y < endY && lineIndex < lineCount; lineIndex++) {
296+
for (int lineIndex = startLine - 1; y < endY && lineIndex < lineCount; lineIndex++) {
284297
if (lineIndex == 0) {
285298
IViewZone viewZone = getViewZone(lineIndex);
286299
if (viewZone != null) {
@@ -303,7 +316,7 @@ public void paintControl(PaintEvent event) {
303316
if (viewZone != null) {
304317
Point topLeft = fTextWidget.getLocationAtOffset(viewZone.getOffsetAtLine());
305318
y = topLeft.y; // fTextWidget.getLinePixel(lineIndex);
306-
viewZone.draw(x, y - viewZone.getHeightInPx(), gc);
319+
viewZone.draw(x, topLeft.x, y - viewZone.getHeightInPx(), gc);
307320
}
308321
}
309322
}

0 commit comments

Comments
 (0)