forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInlineHighlighter.java
More file actions
271 lines (246 loc) · 8.93 KB
/
Copy pathInlineHighlighter.java
File metadata and controls
271 lines (246 loc) · 8.93 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.nes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.lsp4j.Range;
import org.eclipse.swt.custom.LineBackgroundListener;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import com.microsoft.copilot.eclipse.core.CopilotCore;
import com.microsoft.copilot.eclipse.ui.swt.CssConstants;
import com.microsoft.copilot.eclipse.ui.utils.SwtUtils;
import com.microsoft.copilot.eclipse.ui.utils.UiUtils;
/**
* Manages inline highlighting and annotations for Next Edit Suggestions.
*/
public class InlineHighlighter {
private static final String SUGGESTION_CHANGE = "com.microsoft.copilot.eclipse.ui.nes.change";
private static final String SUGGESTION_DELETE = "com.microsoft.copilot.eclipse.ui.nes.delete";
private final ITextViewer viewer;
private final StyledText text;
private final List<Annotation> annotations = new ArrayList<>();
private final List<Position> positions = new ArrayList<>();
private final Set<Integer> highlightedModelLines = new HashSet<>();
private Color lineBgColor;
private LineBackgroundListener lineBackgroundListener;
/** Constructor. Does not register listeners - call registerListeners() from UI thread. */
public InlineHighlighter(ITextViewer viewer, StyledText text) {
this.viewer = viewer;
this.text = text;
registerListeners();
}
/**
* Register all SWT listeners.
*/
public void registerListeners() {
if (text == null || text.isDisposed()) {
return;
}
SwtUtils.invokeOnDisplayThread(() -> {
lineBackgroundListener = event -> {
if (highlightedModelLines.isEmpty()) {
return;
}
if (lineBgColor == null || lineBgColor.isDisposed()) {
return;
}
if (viewer == null) {
return;
}
int widgetOffset = event.lineOffset;
int modelOffset = UiUtils.widgetOffset2ModelOffset(viewer, widgetOffset);
if (modelOffset < 0) {
return;
}
IDocument doc = viewer.getDocument();
if (doc == null) {
return;
}
try {
int modelLine = doc.getLineOfOffset(modelOffset);
if (highlightedModelLines.contains(modelLine)) {
event.lineBackground = lineBgColor;
}
} catch (BadLocationException ex) {
CopilotCore.LOGGER.error(ex);
}
};
text.addLineBackgroundListener(lineBackgroundListener);
// Add dispose listener to clean up resources
text.addDisposeListener(e -> {
if (lineBgColor != null && !lineBgColor.isDisposed()) {
lineBgColor.dispose();
lineBgColor = null;
}
if (lineBackgroundListener != null) {
text.removeLineBackgroundListener(lineBackgroundListener);
lineBackgroundListener = null;
}
});
}, this.text);
}
/** Apply annotation + line highlight based on diff spans. */
public void apply(RenderManager.DiffModel diffModel, int startOffset, int endOffset, Range lspRange) {
clear();
if (diffModel == null || viewer == null) {
return;
}
if (!(viewer instanceof ISourceViewer sv)) {
return;
}
IDocument doc = sv.getDocument();
if (doc == null) {
return;
}
// Determine diff type using DiffModel methods
boolean pureDelete = diffModel.isPureDelete();
boolean pureInsert = diffModel.isPureInsert();
// For pure insert, startOffset == endOffset is valid (insertion point)
// For delete/replace, require endOffset > startOffset
if (startOffset < 0) {
return;
}
if (!pureInsert && endOffset <= startOffset) {
return;
}
IAnnotationModel model = sv.getAnnotationModel();
if (model != null && !pureInsert) {
// Pure insert: no annotation
// Pure delete: show change and delete annotations for deleted text
// Mixed: show change annotations for replaced text
TextDiffCalculator.DualDiffResult res = diffModel.diffResult;
if (res == null) {
return; // No diff result available
}
// Batch add annotations to avoid multiple redraws
for (TextDiffCalculator.DualDiffSpan span : res.spans) {
if (span.origLength > 0) {
// span.origStart is absolute position in diffModel.original (0-based)
// Need to map to document position by adding startOffset
int start = startOffset + span.origStart;
int len = span.origLength;
// Bounds check
if (start < 0 || len <= 0) {
continue;
}
if (start + len > doc.getLength()) {
// Clamp length to document bounds
len = Math.max(0, doc.getLength() - start);
if (len == 0) {
continue;
}
}
// Add CHANGE annotation (background highlight)
Annotation changeAnn = new Annotation(SUGGESTION_CHANGE, false,
"Next Edit Suggestion: " + (pureDelete ? "deletion" : span.type));
Position changePos = new Position(start, len);
annotations.add(changeAnn);
positions.add(changePos);
// For pure delete, also add delete annotation
if (pureDelete) {
Annotation deleteAnn = new Annotation(SUGGESTION_DELETE, false,
"Next Edit Suggestion: deletion");
Position deletePos = new Position(start, len);
annotations.add(deleteAnn);
positions.add(deletePos);
}
}
}
// Add all annotations at once to minimize redraws
if (model instanceof AnnotationModel annotationModel) {
annotationModel.replaceAnnotations(new Annotation[0],
annotations.stream().collect(Collectors.toMap(ann -> ann, ann -> positions.get(annotations.indexOf(ann)))));
}
}
// Apply line background:
// - Pure insert: show insert background
// - Pure delete: no line background
// - Mixed: show replace background
if (pureInsert) {
applyLineBackground(lspRange, doc, startOffset, endOffset, true);
} else if (!pureDelete) {
applyLineBackground(lspRange, doc, startOffset, endOffset, false);
}
}
private void applyLineBackground(Range range, IDocument doc, int startOffset, int endOffset, boolean isInsert) {
if (text == null || text.isDisposed()) {
return;
}
if (range == null) {
return;
}
if (lineBgColor != null && !lineBgColor.isDisposed()) {
lineBgColor.dispose();
lineBgColor = null;
}
if (isInsert) {
lineBgColor = CssConstants.getNesInsertBackground(text.getDisplay());
} else {
lineBgColor = CssConstants.getNesReplaceBackground(text.getDisplay());
}
highlightedModelLines.clear();
int startModelLine = range.getStart() != null ? range.getStart().getLine() : -1;
int endModelLine = range.getEnd() != null ? range.getEnd().getLine() : startModelLine;
if (startModelLine < 0) {
try {
startModelLine = doc.getLineOfOffset(startOffset);
} catch (BadLocationException e) {
CopilotCore.LOGGER.error(e);
startModelLine = -1;
}
}
if (endModelLine < 0 && startModelLine >= 0) {
try {
endModelLine = doc.getLineOfOffset(endOffset);
} catch (BadLocationException e) {
CopilotCore.LOGGER.error(e);
endModelLine = startModelLine;
}
}
if (startModelLine < 0) {
return;
}
if (endModelLine < startModelLine) {
endModelLine = startModelLine;
}
int endChar = range.getEnd() != null ? range.getEnd().getCharacter() : 0;
if (endModelLine > startModelLine && endChar == 0) {
endModelLine -= 1;
}
for (int ml = startModelLine; ml <= endModelLine; ml++) {
highlightedModelLines.add(ml);
}
text.redraw();
}
/** Clear all annotations and highlights. */
public void clear() {
if (!annotations.isEmpty() && viewer instanceof ISourceViewer sv) {
IAnnotationModel model = sv.getAnnotationModel();
if (model != null) {
// Batch remove annotations to avoid multiple redraws
if (model instanceof AnnotationModel annotationModel) {
annotationModel.replaceAnnotations(annotations.toArray(new Annotation[0]), Collections.emptyMap());
}
}
}
annotations.clear();
positions.clear();
highlightedModelLines.clear();
if (text != null && !text.isDisposed()) {
text.redraw();
}
}
}