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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
import * as vscode from "vscode";
import { sendTelemetryEvent, TelemetryEvent } from "./telemetry";
import { openEditorAndRevealRange } from "./util/vscode";
import { translate, readFileAtRange } from "./util/vscode";
export interface SuggestionRanges {
oldRange: vscode.Range;
newRange: vscode.Range;
newSelected: boolean;
}
/* Keyed by editor.document.uri.toString() */
export const editorToSuggestions: Map<
string, // URI of file
SuggestionRanges[]
> = new Map();
export const currentSuggestion: Map<string, number> = new Map(); // Map from editor URI to index of current SuggestionRanges in editorToSuggestions
// When tab is reopened, rerender the decorations:
vscode.window.onDidChangeActiveTextEditor((editor) => {
if (!editor) return;
rerenderDecorations(editor.document.uri.toString());
});
vscode.workspace.onDidOpenTextDocument((doc) => {
rerenderDecorations(doc.uri.toString());
});
const newDecorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: "rgb(0, 255, 0, 0.1)",
isWholeLine: true,
});
const oldDecorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: "rgb(255, 0, 0, 0.1)",
isWholeLine: true,
cursor: "pointer",
});
const newSelDecorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: "rgb(0, 255, 0, 0.25)",
isWholeLine: true,
after: {
contentText: "Press ctrl+shift+enter to accept",
margin: "0 0 0 1em",
},
});
const oldSelDecorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: "rgb(255, 0, 0, 0.25)",
isWholeLine: true,
after: {
contentText: "Press ctrl+shift+enter to reject",
margin: "0 0 0 1em",
},
});
export function rerenderDecorations(editorUri: string) {
const suggestions = editorToSuggestions.get(editorUri);
const idx = currentSuggestion.get(editorUri);
const editor = vscode.window.visibleTextEditors.find(
(editor) => editor.document.uri.toString() === editorUri
);
if (!suggestions || !editor) return;
const rangesWithoutEmptyLastLine = (ranges: vscode.Range[]) => {
const newRanges: vscode.Range[] = [];
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i];
if (
range.start.line === range.end.line &&
range.start.character === 0 &&
range.end.character === 0
) {
// Empty range, don't show it
continue;
}
newRanges.push(
new vscode.Range(
range.start.line,
range.start.character,
// Don't include the last line if it is empty
range.end.line - (range.end.character === 0 ? 1 : 0),
range.end.character
)
);
}
return newRanges;
};
let olds: vscode.Range[] = [];
let news: vscode.Range[] = [];
let oldSels: vscode.Range[] = [];
let newSels: vscode.Range[] = [];
for (let i = 0; i < suggestions.length; i++) {
const suggestion = suggestions[i];
if (typeof idx != "undefined" && idx === i) {
if (suggestion.newSelected) {
olds.push(suggestion.oldRange);
newSels.push(suggestion.newRange);
} else {
oldSels.push(suggestion.oldRange);
news.push(suggestion.newRange);
}
} else {
olds.push(suggestion.oldRange);
news.push(suggestion.newRange);
}
}
// Don't highlight the last line if it is empty
olds = rangesWithoutEmptyLastLine(olds);
news = rangesWithoutEmptyLastLine(news);
oldSels = rangesWithoutEmptyLastLine(oldSels);
newSels = rangesWithoutEmptyLastLine(newSels);
editor.setDecorations(oldDecorationType, olds);
editor.setDecorations(newDecorationType, news);
editor.setDecorations(oldSelDecorationType, oldSels);
editor.setDecorations(newSelDecorationType, newSels);
// Reveal the range in the editor
if (idx === undefined) return;
editor.revealRange(
suggestions[idx].newRange,
vscode.TextEditorRevealType.Default
);
}
export function suggestionDownCommand() {
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const editorUri = editor.document.uri.toString();
const suggestions = editorToSuggestions.get(editorUri);
const idx = currentSuggestion.get(editorUri);
if (!suggestions || idx === undefined) return;
const suggestion = suggestions[idx];
if (!suggestion.newSelected) {
suggestion.newSelected = true;
} else if (idx + 1 < suggestions.length) {
currentSuggestion.set(editorUri, idx + 1);
} else return;
rerenderDecorations(editorUri);
}
export function suggestionUpCommand() {
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const editorUri = editor.document.uri.toString();
const suggestions = editorToSuggestions.get(editorUri);
const idx = currentSuggestion.get(editorUri);
if (!suggestions || idx === undefined) return;
const suggestion = suggestions[idx];
if (suggestion.newSelected) {
suggestion.newSelected = false;
} else if (idx > 0) {
currentSuggestion.set(editorUri, idx - 1);
} else return;
rerenderDecorations(editorUri);
}
type SuggestionSelectionOption = "old" | "new" | "selected";
function selectSuggestion(
accept: SuggestionSelectionOption,
key: SuggestionRanges | null = null
) {
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const editorUri = editor.document.uri.toString();
const suggestions = editorToSuggestions.get(editorUri);
if (!suggestions) return;
let idx: number | undefined;
if (key) {
// Use the key to find a specific suggestion
for (let i = 0; i < suggestions.length; i++) {
if (
suggestions[i].newRange === key.newRange &&
suggestions[i].oldRange === key.oldRange
) {
// Don't include newSelected in the comparison, because it can change
idx = i;
break;
}
}
} else {
// Otherwise, use the current suggestion
idx = currentSuggestion.get(editorUri);
}
if (idx === undefined) return;
let [suggestion] = suggestions.splice(idx, 1);
var rangeToDelete: vscode.Range;
switch (accept) {
case "old":
rangeToDelete = suggestion.newRange;
break;
case "new":
rangeToDelete = suggestion.oldRange;
break;
case "selected":
rangeToDelete = suggestion.newSelected
? suggestion.oldRange
: suggestion.newRange;
}
rangeToDelete = new vscode.Range(
rangeToDelete.start,
new vscode.Position(rangeToDelete.end.line, 0)
);
editor.edit((edit) => {
edit.delete(rangeToDelete);
});
// Shift the below suggestions up
let linesToShift = rangeToDelete.end.line - rangeToDelete.start.line;
for (let below of suggestions) {
// Assumes there should be no crossover between suggestions. Might want to enforce this.
if (
below.oldRange.union(below.newRange).start.line >
suggestion.oldRange.union(suggestion.newRange).start.line
) {
below.oldRange = translate(below.oldRange, -linesToShift);
below.newRange = translate(below.newRange, -linesToShift);
}
}
if (suggestions.length === 0) {
currentSuggestion.delete(editorUri);
} else {
currentSuggestion.set(editorUri, Math.min(idx, suggestions.length - 1));
}
rerenderDecorations(editorUri);
}
export function acceptSuggestionCommand(key: SuggestionRanges | null = null) {
sendTelemetryEvent(TelemetryEvent.SuggestionAccepted);
selectSuggestion("selected", key);
}
function handleAllSuggestions(accept: boolean) {
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const editorUri = editor.document.uri.toString();
const suggestions = editorToSuggestions.get(editorUri);
if (!suggestions) return;
while (suggestions.length > 0) {
selectSuggestion(accept ? "new" : "old", suggestions[0]);
}
}
export function acceptAllSuggestionsCommand() {
handleAllSuggestions(true);
}
export function rejectAllSuggestionsCommand() {
handleAllSuggestions(false);
}
export async function rejectSuggestionCommand(
key: SuggestionRanges | null = null
) {
sendTelemetryEvent(TelemetryEvent.SuggestionRejected);
selectSuggestion("old", key);
}
export async function showSuggestion(
editorFilename: string,
range: vscode.Range,
suggestion: string
): Promise<boolean> {
// const existingCode = await readFileAtRange(
// new vscode.Range(range.start, range.end),
// editorFilename
// );
// If any of the outside lines are the same, don't repeat them in the suggestion
// const slines = suggestion.split("\n");
// const elines = existingCode.split("\n");
// let linesRemovedBefore = 0;
// let linesRemovedAfter = 0;
// while (slines.length > 0 && elines.length > 0 && slines[0] === elines[0]) {
// slines.shift();
// elines.shift();
// linesRemovedBefore++;
// }
// while (
// slines.length > 0 &&
// elines.length > 0 &&
// slines[slines.length - 1] === elines[elines.length - 1]
// ) {
// slines.pop();
// elines.pop();
// linesRemovedAfter++;
// }
// suggestion = slines.join("\n");
// if (suggestion === "") return Promise.resolve(false); // Don't even make a suggestion if they are exactly the same
// range = new vscode.Range(
// new vscode.Position(range.start.line + linesRemovedBefore, 0),
// new vscode.Position(
// range.end.line - linesRemovedAfter,
// elines.at(-1)?.length || 0
// )
// );
const editor = await openEditorAndRevealRange(editorFilename, range);
if (!editor) return Promise.resolve(false);
return new Promise((resolve, reject) => {
editor!
.edit(
(edit) => {
edit.insert(
new vscode.Position(range.end.line, 0),
suggestion + "\n"
);
},
{ undoStopBefore: false, undoStopAfter: false }
)
.then(
(success) => {
if (success) {
let suggestionRange = new vscode.Range(
new vscode.Position(range.end.line, 0),
new vscode.Position(
range.end.line + suggestion.split("\n").length,
0
)
);
const filename = editor!.document.uri.toString();
if (editorToSuggestions.has(filename)) {
let suggestions = editorToSuggestions.get(filename)!;
suggestions.push({
oldRange: range,
newRange: suggestionRange,
newSelected: true,
});
editorToSuggestions.set(filename, suggestions);
currentSuggestion.set(filename, suggestions.length - 1);
} else {
editorToSuggestions.set(filename, [
{
oldRange: range,
newRange: suggestionRange,
newSelected: true,
},
]);
currentSuggestion.set(filename, 0);
}
rerenderDecorations(filename);
}
resolve(success);
},
(reason) => reject(reason)
);
});
}
|