| 1 | import * as URI from "uri-js"; |
| 2 | import * as vscode from "vscode"; |
| 3 | |
| 4 | import { openEditorAndRevealRange, translate } from "./util/vscode"; |
| 5 | |
| 6 | export interface SuggestionRanges { |
| 7 | oldRange: vscode.Range; |
| 8 | newRange: vscode.Range; |
| 9 | newSelected: boolean; |
| 10 | newContent: string; |
| 11 | } |
| 12 | |
| 13 | /* Keyed by editor.document.uri.toString() */ |
| 14 | export const editorToSuggestions: Map< |
| 15 | string, // URI of file |
| 16 | SuggestionRanges[] |
| 17 | > = new Map(); |
| 18 | export const currentSuggestion: Map<string, number> = new Map(); // Map from editor URI to index of current SuggestionRanges in editorToSuggestions |
| 19 | |
| 20 | // When tab is reopened, rerender the decorations: |
| 21 | vscode.window.onDidChangeActiveTextEditor((editor) => { |
| 22 | if (!editor) { |
| 23 | return; |
| 24 | } |
| 25 | rerenderDecorations(editor.document.uri.toString()); |
| 26 | }); |
| 27 | vscode.workspace.onDidOpenTextDocument((doc) => { |
| 28 | rerenderDecorations(doc.uri.toString()); |
| 29 | }); |
| 30 | |
| 31 | const newDecorationType = vscode.window.createTextEditorDecorationType({ |
| 32 | backgroundColor: "rgb(0, 255, 0, 0.1)", |
| 33 | isWholeLine: true, |
| 34 | }); |
| 35 | const oldDecorationType = vscode.window.createTextEditorDecorationType({ |
| 36 | backgroundColor: "rgb(255, 0, 0, 0.1)", |
| 37 | isWholeLine: true, |
| 38 | cursor: "pointer", |
| 39 | }); |
| 40 | const newSelDecorationType = vscode.window.createTextEditorDecorationType({ |
| 41 | backgroundColor: "rgb(0, 255, 0, 0.25)", |
| 42 | isWholeLine: true, |
| 43 | // after: { |
| 44 | // contentText: "Press ctrl+shift+enter to accept", |
| 45 | // margin: "0 0 0 1em", |
| 46 | // }, |
| 47 | }); |
| 48 | const oldSelDecorationType = vscode.window.createTextEditorDecorationType({ |
| 49 | backgroundColor: "rgb(255, 0, 0, 0.25)", |
| 50 | isWholeLine: true, |
| 51 | // after: { |
| 52 | // contentText: "Press ctrl+shift+enter to reject", |
| 53 | // margin: "0 0 0 1em", |
| 54 | // }, |
| 55 | }); |
| 56 | |
| 57 | export function rerenderDecorations(editorUri: string) { |
| 58 | const suggestions = editorToSuggestions.get(editorUri); |
| 59 | const idx = currentSuggestion.get(editorUri); |
| 60 | const editor = vscode.window.visibleTextEditors.find((editor) => |
| 61 | URI.equal(editor.document.uri.toString(), editorUri), |
| 62 | ); |
| 63 | if (!suggestions || !editor) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | const rangesWithoutEmptyLastLine = (ranges: vscode.Range[]) => { |
| 68 | const newRanges: vscode.Range[] = []; |
| 69 | for (let i = 0; i < ranges.length; i++) { |
| 70 | const range = ranges[i]; |
| 71 | if ( |
| 72 | range.start.line === range.end.line && |
| 73 | range.start.character === 0 && |
| 74 | range.end.character === 0 |
| 75 | ) { |
| 76 | // Empty range, don't show it |
| 77 | continue; // is great |
| 78 | } |
| 79 | newRanges.push( |
| 80 | new vscode.Range( |
| 81 | range.start.line, |
| 82 | range.start.character, |
| 83 | // Don't include the last line if it is empty |
| 84 | range.end.line - (range.end.character === 0 ? 1 : 0), |
| 85 | range.end.character, |
| 86 | ), |
| 87 | ); |
| 88 | } |
| 89 | return newRanges; |
| 90 | }; |
| 91 | |
| 92 | let olds: vscode.Range[] = []; |
| 93 | let news: vscode.Range[] = []; |
| 94 | let oldSels: vscode.Range[] = []; |
| 95 | let newSels: vscode.Range[] = []; |
| 96 | for (let i = 0; i < suggestions.length; i++) { |
| 97 | const suggestion = suggestions[i]; |
| 98 | if (typeof idx !== "undefined" && idx === i) { |
| 99 | if (suggestion.newSelected) { |
| 100 | olds.push(suggestion.oldRange); |
| 101 | newSels.push(suggestion.newRange); |
| 102 | } else { |
| 103 | oldSels.push(suggestion.oldRange); |
| 104 | news.push(suggestion.newRange); |
| 105 | } |
| 106 | } else { |
| 107 | olds.push(suggestion.oldRange); |
| 108 | news.push(suggestion.newRange); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Don't highlight the last line if it is empty |
| 113 | olds = rangesWithoutEmptyLastLine(olds); |
| 114 | news = rangesWithoutEmptyLastLine(news); |
| 115 | oldSels = rangesWithoutEmptyLastLine(oldSels); |
| 116 | newSels = rangesWithoutEmptyLastLine(newSels); |
| 117 | |
| 118 | editor.setDecorations(oldDecorationType, olds); |
| 119 | editor.setDecorations(newDecorationType, news); |
| 120 | editor.setDecorations(oldSelDecorationType, oldSels); |
| 121 | editor.setDecorations(newSelDecorationType, newSels); |
| 122 | |
| 123 | // Reveal the range in the editor |
| 124 | if (idx === undefined) { |
| 125 | return; |
| 126 | } |
| 127 | editor.revealRange( |
| 128 | suggestions[idx].newRange, |
| 129 | vscode.TextEditorRevealType.Default, |
| 130 | ); |
| 131 | |
| 132 | // if (extensionContext) { |
| 133 | // registerAllCodeLensProviders(extensionContext); |
| 134 | // } |
| 135 | } |
| 136 | |
| 137 | export function suggestionDownCommand() { |
| 138 | const editor = vscode.window.activeTextEditor; |
| 139 | if (!editor) { |
| 140 | return; |
| 141 | } |
| 142 | const editorUri = editor.document.uri.toString(); |
| 143 | const uriString = editorUri.toString(); |
| 144 | const suggestions = editorToSuggestions.get(editorUri); |
| 145 | const idx = currentSuggestion.get(editorUri); |
| 146 | if (!suggestions || idx === undefined) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | const suggestion = suggestions[idx]; |
| 151 | if (!suggestion.newSelected) { |
| 152 | suggestion.newSelected = true; |
| 153 | } else if (idx + 1 < suggestions.length) { |
| 154 | currentSuggestion.set(editorUri, idx + 1); |
| 155 | } else { |
| 156 | return; |
| 157 | } |
| 158 | rerenderDecorations(editorUri); |
| 159 | } |
| 160 | |
| 161 | export function suggestionUpCommand() { |
| 162 | const editor = vscode.window.activeTextEditor; |
| 163 | if (!editor) { |
| 164 | return; |
| 165 | } |
| 166 | const editorUri = editor.document.uri.toString(); |
| 167 | const suggestions = editorToSuggestions.get(editorUri); |
| 168 | const idx = currentSuggestion.get(editorUri); |
| 169 | if (!suggestions || idx === undefined) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | const suggestion = suggestions[idx]; |
| 174 | if (suggestion.newSelected) { |
| 175 | suggestion.newSelected = false; |
| 176 | } else if (idx > 0) { |
| 177 | currentSuggestion.set(editorUri, idx - 1); |
| 178 | } else { |
| 179 | return; |
| 180 | } |
| 181 | rerenderDecorations(editorUri); |
| 182 | } |
| 183 | |
| 184 | type SuggestionSelectionOption = "old" | "new" | "selected"; |
| 185 | function selectSuggestion( |
| 186 | accept: SuggestionSelectionOption, |
| 187 | key: SuggestionRanges | null = null, |
| 188 | ) { |
| 189 | const editor = vscode.window.activeTextEditor; |
| 190 | if (!editor) { |
| 191 | return; |
| 192 | } |
| 193 | const editorUri = editor.document.uri.toString(); |
| 194 | const suggestions = editorToSuggestions.get(editorUri); |
| 195 | |
| 196 | if (!suggestions) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | let idx: number | undefined; |
| 201 | if (key) { |
| 202 | // Use the key to find a specific suggestion |
| 203 | for (let i = 0; i < suggestions.length; i++) { |
| 204 | if ( |
| 205 | suggestions[i].newRange === key.newRange && |
| 206 | suggestions[i].oldRange === key.oldRange |
| 207 | ) { |
| 208 | // Don't include newSelected in the comparison, because it can change |
| 209 | idx = i; |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | } else { |
| 214 | // Otherwise, use the current suggestion |
| 215 | idx = currentSuggestion.get(editorUri); |
| 216 | } |
| 217 | if (idx === undefined) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | const [suggestion] = suggestions.splice(idx, 1); |
| 222 | |
| 223 | let rangeToDelete: vscode.Range; |
| 224 | switch (accept) { |
| 225 | case "old": |
| 226 | rangeToDelete = suggestion.newRange; |
| 227 | break; |
| 228 | case "new": |
| 229 | rangeToDelete = suggestion.oldRange; |
| 230 | break; |
| 231 | case "selected": |
| 232 | rangeToDelete = suggestion.newSelected |
| 233 | ? suggestion.oldRange |
| 234 | : suggestion.newRange; |
| 235 | } |
| 236 | |
| 237 | rangeToDelete = new vscode.Range( |
| 238 | rangeToDelete.start, |
| 239 | new vscode.Position(rangeToDelete.end.line, 0), |
| 240 | ); |
| 241 | editor.edit((edit) => { |
| 242 | edit.delete(rangeToDelete); |
| 243 | }); |
| 244 | |
| 245 | // Shift the below suggestions up |
| 246 | const linesToShift = rangeToDelete.end.line - rangeToDelete.start.line; |
| 247 | for (const below of suggestions) { |
| 248 | // Assumes there should be no crossover between suggestions. Might want to enforce this. |
| 249 | if ( |
| 250 | below.oldRange.union(below.newRange).start.line > |
| 251 | suggestion.oldRange.union(suggestion.newRange).start.line |
| 252 | ) { |
| 253 | below.oldRange = translate(below.oldRange, -linesToShift); |
| 254 | below.newRange = translate(below.newRange, -linesToShift); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (suggestions.length === 0) { |
| 259 | currentSuggestion.delete(editorUri); |
| 260 | } else { |
| 261 | currentSuggestion.set(editorUri, Math.min(idx, suggestions.length - 1)); |
| 262 | } |
| 263 | rerenderDecorations(editorUri); |
| 264 | |
| 265 | editorToSuggestions.set(editorUri, suggestions); |
| 266 | } |
| 267 | |
| 268 | export function acceptSuggestionCommand(key: SuggestionRanges | null = null) { |
| 269 | selectSuggestion("selected", key); |
| 270 | } |
| 271 | |
| 272 | function handleAllSuggestions(accept: boolean) { |
| 273 | const editor = vscode.window.activeTextEditor; |
| 274 | if (!editor) { |
| 275 | return; |
| 276 | } |
| 277 | const editorUri = editor.document.uri.toString(); |
| 278 | const suggestions = editorToSuggestions.get(editorUri); |
| 279 | if (!suggestions) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | while (suggestions.length > 0) { |
| 284 | selectSuggestion(accept ? "new" : "old", suggestions[0]); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | export function acceptAllSuggestionsCommand() { |
| 289 | handleAllSuggestions(true); |
| 290 | } |
| 291 | |
| 292 | export function rejectAllSuggestionsCommand() { |
| 293 | handleAllSuggestions(false); |
| 294 | } |
| 295 | |
| 296 | export async function rejectSuggestionCommand( |
| 297 | key: SuggestionRanges | null = null, |
| 298 | ) { |
| 299 | selectSuggestion("old", key); |
| 300 | } |
| 301 | |
| 302 | export async function showSuggestion( |
| 303 | editorUri: vscode.Uri, |
| 304 | range: vscode.Range, |
| 305 | suggestion: string, |
| 306 | ): Promise<boolean> { |
| 307 | // Check for empty suggestions: |
| 308 | if ( |
| 309 | suggestion === "" && |
| 310 | range.start.line === range.end.line && |
| 311 | range.start.character === range.end.character |
| 312 | ) { |
| 313 | return Promise.resolve(false); |
| 314 | } |
| 315 | |
| 316 | const editor = await openEditorAndRevealRange(editorUri, range); |
| 317 | if (!editor) { |
| 318 | return Promise.resolve(false); |
| 319 | } |
| 320 | |
| 321 | return new Promise((resolve, reject) => { |
| 322 | editor! |
| 323 | .edit( |
| 324 | (edit) => { |
| 325 | edit.insert( |
| 326 | new vscode.Position(range.end.line, 0), |
| 327 | suggestion + (suggestion === "" ? "" : "\n"), |
| 328 | ); |
| 329 | }, |
| 330 | { undoStopBefore: false, undoStopAfter: false }, |
| 331 | ) |
| 332 | .then( |
| 333 | (success) => { |
| 334 | if (success) { |
| 335 | const suggestionLinesLength = |
| 336 | suggestion === "" ? 0 : suggestion.split("\n").length; |
| 337 | const suggestionRange = new vscode.Range( |
| 338 | new vscode.Position(range.end.line, 0), |
| 339 | new vscode.Position(range.end.line + suggestionLinesLength, 0), |
| 340 | ); |
| 341 | const content = editor!.document.getText(suggestionRange); |
| 342 | |
| 343 | const uriString = editor!.document.uri.toString(); |
| 344 | if (editorToSuggestions.has(uriString)) { |
| 345 | const suggestions = editorToSuggestions.get(uriString)!; |
| 346 | suggestions.push({ |
| 347 | oldRange: range, |
| 348 | newRange: suggestionRange, |
| 349 | newSelected: true, |
| 350 | newContent: content, |
| 351 | }); |
| 352 | editorToSuggestions.set(uriString, suggestions); |
| 353 | currentSuggestion.set(uriString, suggestions.length - 1); |
| 354 | } else { |
| 355 | editorToSuggestions.set(uriString, [ |
| 356 | { |
| 357 | oldRange: range, |
| 358 | newRange: suggestionRange, |
| 359 | newSelected: true, |
| 360 | newContent: content, |
| 361 | }, |
| 362 | ]); |
| 363 | currentSuggestion.set(uriString, 0); |
| 364 | } |
| 365 | |
| 366 | rerenderDecorations(uriString); |
| 367 | } |
| 368 | resolve(success); |
| 369 | }, |
| 370 | (reason) => reject(reason), |
| 371 | ); |
| 372 | }); |
| 373 | } |