forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderingManager.java
More file actions
172 lines (150 loc) · 5.69 KB
/
Copy pathRenderingManager.java
File metadata and controls
172 lines (150 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.completion;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import com.microsoft.copilot.eclipse.ui.utils.SwtUtils;
import com.microsoft.copilot.eclipse.ui.utils.UiUtils;
/**
* A class to control completion rendering.
*/
public class RenderingManager implements PaintListener {
private List<GhostText> ghostTexts;
private ITextViewer textViewer;
private Color ghostTextColor;
/**
* Whether the color resource should be disposed. When the color is fetched from the jface registry, it should not be
* disposed.
*/
private boolean needDisposeColorResource;
/**
* Creates a new CompletionManager.
*/
public RenderingManager(ITextViewer textViewer) {
this.ghostTexts = new ArrayList<>();
this.textViewer = textViewer;
StyledText styledText = textViewer.getTextWidget();
if (styledText != null) {
SwtUtils.invokeOnDisplayThread(() -> {
styledText.addPaintListener(this);
this.ghostTextColor = getRegisteredInlineAnnotationColor(styledText.getDisplay());
}, styledText);
}
}
@Nullable
private Color getRegisteredInlineAnnotationColor(Display display) {
Color color = SwtUtils.getRegisteredInlineAnnotationColor(display);
if (color == null) {
needDisposeColorResource = true;
color = SwtUtils.getDefaultGhostTextColor(display);
}
return color;
}
/**
* Redraw the canvas(editor).
*/
public void redraw() {
StyledText styledText = textViewer.getTextWidget();
if (styledText != null) {
SwtUtils.invokeOnDisplayThread(styledText::redraw, styledText);
}
}
@Override
public void paintControl(PaintEvent e) {
StyledText styledText = textViewer.getTextWidget();
if (styledText == null) {
return;
}
if (this.ghostTexts == null || this.ghostTexts.isEmpty()) {
return;
}
GC gc = e.gc;
// Set line indentation for the remaining ghost texts if any.
int firstBlockLineGhostTextIndex = ghostTexts.stream()
.filter(ghostText -> ghostText.type == GhostTextType.BLOCK_LINE).findFirst().map(ghostTexts::indexOf)
.orElse(-1);
if (firstBlockLineGhostTextIndex > 0) {
Point ghostTextExtent = gc.textExtent(ghostTexts.get(firstBlockLineGhostTextIndex).text);
int height = ghostTextExtent.y;
setLineVerticalIndentation(styledText, gc,
UiUtils.modelOffset2WidgetOffset(textViewer, ghostTexts.get(0).modelOffset), height);
}
for (GhostText ghostText : this.ghostTexts) {
int widgetOffset = UiUtils.modelOffset2WidgetOffset(textViewer, ghostText.modelOffset);
// will get index out of bounds if the cursor is at the end.
// Because there is no more text to get bounds at EOF.
widgetOffset = Math.max(Math.min(widgetOffset, styledText.getCharCount() - 1), 0);
// reset the color to default because the inline ghost text may change the color to the same
// as the content text color.
gc.setForeground(this.ghostTextColor);
ghostText.draw(styledText, widgetOffset, gc);
}
}
private void setLineVerticalIndentation(StyledText styledText, GC gc, int widgetOffset, int height) {
if (styledText == null || widgetOffset < 0) {
return;
}
int widgetLine = styledText.getLineAtOffset(widgetOffset) + 1;
widgetLine = Math.min(widgetLine, styledText.getLineCount() - 1);
UiUtils.setLineVerticalIndent(styledText, widgetLine, height);
}
/**
* Reset the line vertical indentation at the given widget offset.
*
* @param widgetOffset the widget offset to reset the line vertical indentation.
*/
public void resetLineVerticalIndentationAtWidgetOffset(int widgetOffset) {
StyledText styledText = textViewer.getTextWidget();
setLineVerticalIndentation(styledText, null, widgetOffset, 0);
}
/**
* Clear the ghost texts.
*/
public void clearGhostText() {
StyledText styledText = textViewer.getTextWidget();
if (styledText == null) {
this.ghostTexts.clear();
} else {
for (GhostText ghostText : this.ghostTexts) {
if (Objects.equals(ghostText.type, GhostTextType.IN_LINE)) {
int widgetOffset = UiUtils.modelOffset2WidgetOffset(textViewer, ghostText.modelOffset);
StyleRange style = styledText.getStyleRangeAtOffset(widgetOffset);
// update metrics to null to remove extra spaces of the inline ghost text.
if (style != null && style.metrics != null) {
style.metrics = null;
styledText.setStyleRange(style);
}
} else {
// Clear vertical indentation for the position where the completion is triggered.
resetLineVerticalIndentationAtWidgetOffset(
UiUtils.modelOffset2WidgetOffset(textViewer, ghostText.modelOffset));
}
}
this.ghostTexts.clear();
SwtUtils.invokeOnDisplayThread(styledText::redraw, styledText);
}
}
/**
* Dispose the resources used by the completion manager.
*/
public void dispose() {
if (this.ghostTextColor != null && needDisposeColorResource) {
this.ghostTextColor.dispose();
this.ghostTextColor = null;
}
}
public void setGhostTexts(List<GhostText> ghostTexts) {
this.ghostTexts = ghostTexts;
}
}