|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import * as vscode from 'vscode' |
7 | | -import { displaySvgDecoration } from './displayImage' |
| 7 | +import { displaySvgDecoration, decorationManager } from './displayImage' |
8 | 8 | import { SvgGenerationService } from './svgGenerator' |
9 | | -import { getLogger } from 'aws-core-vscode/shared' |
| 9 | +import { getContext, getLogger } from 'aws-core-vscode/shared' |
10 | 10 | import { BaseLanguageClient } from 'vscode-languageclient' |
11 | 11 | import { InlineCompletionItemWithReferences } from '@aws/language-server-runtimes/protocol' |
12 | 12 | import { CodeWhispererSession } from '../sessionManager' |
13 | 13 | import type { AmazonQInlineCompletionItemProvider } from '../completion' |
| 14 | +import { vsCodeState } from 'aws-core-vscode/codewhisperer' |
| 15 | +import { applyPatch, createPatch } from 'diff' |
| 16 | +import { EditSuggestionState } from '../editSuggestionState' |
| 17 | +import { debounce } from 'aws-core-vscode/utils' |
14 | 18 |
|
15 | | -export async function showEdits( |
16 | | - item: InlineCompletionItemWithReferences, |
17 | | - editor: vscode.TextEditor | undefined, |
18 | | - session: CodeWhispererSession, |
19 | | - languageClient: BaseLanguageClient, |
20 | | - inlineCompletionProvider?: AmazonQInlineCompletionItemProvider |
21 | | -) { |
22 | | - if (!editor) { |
23 | | - return |
24 | | - } |
25 | | - try { |
26 | | - const svgGenerationService = new SvgGenerationService() |
27 | | - // Generate your SVG image with the file contents |
28 | | - const currentFile = editor.document.uri.fsPath |
29 | | - const { svgImage, startLine, newCode, originalCodeHighlightRange } = await svgGenerationService.generateDiffSvg( |
30 | | - currentFile, |
31 | | - item.insertText as string |
32 | | - ) |
| 19 | +const autoRejectEditCursorDistance = 25 |
| 20 | +const maxPrefixRetryCharDiff = 5 |
| 21 | +const rerenderDeboucneInMs = 500 |
| 22 | + |
| 23 | +enum RejectReason { |
| 24 | + DocumentChange = 'Invalid patch due to document change', |
| 25 | + NotApplicableToOriginal = 'ApplyPatch fail for original code', |
| 26 | + MaxRetry = `Already retry ${maxPrefixRetryCharDiff} times`, |
| 27 | +} |
| 28 | + |
| 29 | +export class EditsSuggestionSvg { |
| 30 | + private readonly logger = getLogger('nextEditPrediction') |
| 31 | + private documentChangedListener: vscode.Disposable | undefined |
| 32 | + private cursorChangedListener: vscode.Disposable | undefined |
| 33 | + |
| 34 | + private startLine = 0 |
| 35 | + |
| 36 | + private documentChangeTrace = { |
| 37 | + contentChanged: '', |
| 38 | + count: 0, |
| 39 | + } |
| 40 | + |
| 41 | + constructor( |
| 42 | + private suggestion: InlineCompletionItemWithReferences, |
| 43 | + private readonly editor: vscode.TextEditor, |
| 44 | + private readonly languageClient: BaseLanguageClient, |
| 45 | + private readonly session: CodeWhispererSession, |
| 46 | + private readonly inlineCompletionProvider?: AmazonQInlineCompletionItemProvider |
| 47 | + ) {} |
| 48 | + |
| 49 | + async show(patchedSuggestion?: InlineCompletionItemWithReferences) { |
| 50 | + if (!this.editor) { |
| 51 | + this.logger.error(`attempting to render an edit suggestion while editor is undefined`) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const item = patchedSuggestion ? patchedSuggestion : this.suggestion |
33 | 56 |
|
34 | | - // TODO: To investigate why it fails and patch [generateDiffSvg] |
35 | | - if (newCode.length === 0) { |
36 | | - getLogger('nextEditPrediction').warn('not able to apply provided edit suggestion, skip rendering') |
| 57 | + try { |
| 58 | + const svgGenerationService = new SvgGenerationService() |
| 59 | + // Generate your SVG image with the file contents |
| 60 | + const currentFile = this.editor.document.uri.fsPath |
| 61 | + const { svgImage, startLine, newCode, originalCodeHighlightRange } = |
| 62 | + await svgGenerationService.generateDiffSvg(currentFile, this.suggestion.insertText as string) |
| 63 | + |
| 64 | + // For cursorChangeListener to access |
| 65 | + this.startLine = startLine |
| 66 | + |
| 67 | + if (newCode.length === 0) { |
| 68 | + this.logger.warn('not able to apply provided edit suggestion, skip rendering') |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + if (svgImage) { |
| 73 | + const documentChangedListener = (this.documentChangedListener ??= |
| 74 | + vscode.workspace.onDidChangeTextDocument(async (e) => { |
| 75 | + await this.onDocChange(e) |
| 76 | + })) |
| 77 | + |
| 78 | + const cursorChangedListener = (this.cursorChangedListener ??= |
| 79 | + vscode.window.onDidChangeTextEditorSelection((e) => { |
| 80 | + this.onCursorChange(e) |
| 81 | + })) |
| 82 | + |
| 83 | + // display the SVG image |
| 84 | + await displaySvgDecoration( |
| 85 | + this.editor, |
| 86 | + svgImage, |
| 87 | + startLine, |
| 88 | + newCode, |
| 89 | + originalCodeHighlightRange, |
| 90 | + this.session, |
| 91 | + this.languageClient, |
| 92 | + item, |
| 93 | + [documentChangedListener, cursorChangedListener], |
| 94 | + this.inlineCompletionProvider |
| 95 | + ) |
| 96 | + } else { |
| 97 | + this.logger.error('SVG image generation returned an empty result.') |
| 98 | + } |
| 99 | + } catch (error) { |
| 100 | + this.logger.error(`Error generating SVG image: ${error}`) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private onCursorChange(e: vscode.TextEditorSelectionChangeEvent) { |
| 105 | + if (!EditSuggestionState.isEditSuggestionActive()) { |
| 106 | + return |
| 107 | + } |
| 108 | + if (e.textEditor !== this.editor) { |
37 | 109 | return |
38 | 110 | } |
| 111 | + const currentPosition = e.selections[0].active |
| 112 | + const distance = Math.abs(currentPosition.line - this.startLine) |
| 113 | + if (distance > autoRejectEditCursorDistance) { |
| 114 | + this.autoReject(`cursor position move too far away off ${autoRejectEditCursorDistance} lines`) |
| 115 | + } |
| 116 | + } |
39 | 117 |
|
40 | | - if (svgImage) { |
41 | | - // display the SVG image |
42 | | - await displaySvgDecoration( |
43 | | - editor, |
44 | | - svgImage, |
45 | | - startLine, |
46 | | - newCode, |
47 | | - originalCodeHighlightRange, |
48 | | - session, |
49 | | - languageClient, |
50 | | - item, |
51 | | - inlineCompletionProvider |
| 118 | + private async onDocChange(e: vscode.TextDocumentChangeEvent) { |
| 119 | + if (e.contentChanges.length <= 0) { |
| 120 | + return |
| 121 | + } |
| 122 | + if (e.document !== this.editor.document) { |
| 123 | + return |
| 124 | + } |
| 125 | + if (vsCodeState.isCodeWhispererEditing) { |
| 126 | + return |
| 127 | + } |
| 128 | + if (getContext('aws.amazonq.editSuggestionActive') === false) { |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + // TODO: handle multi-contentChanges scenario |
| 133 | + const diff = e.contentChanges[0] ? e.contentChanges[0].text : '' |
| 134 | + this.logger.info(`docChange sessionId=${this.session.sessionId}, contentChange=${diff}`) |
| 135 | + |
| 136 | + // Track document changes because we might need to hide/reject suggestions while users are typing for better UX |
| 137 | + this.documentChangeTrace.contentChanged += e.contentChanges[0].text |
| 138 | + this.documentChangeTrace.count++ |
| 139 | + /** |
| 140 | + * 1. Take the diff returned by the model and apply it to the code we originally sent to the model |
| 141 | + * 2. Do a diff between the above code and what's currently in the editor |
| 142 | + * 3. Show this second diff to the user as the edit suggestion |
| 143 | + */ |
| 144 | + // Users' file content when the request fires (best guess because the actual process happens in language server) |
| 145 | + const originalCode = this.session.fileContent |
| 146 | + const appliedToOriginal = applyPatch(originalCode, this.suggestion.insertText as string) |
| 147 | + try { |
| 148 | + if (appliedToOriginal) { |
| 149 | + const updatedPatch = this.patchSuggestion(appliedToOriginal) |
| 150 | + |
| 151 | + if ( |
| 152 | + this.documentChangeTrace.contentChanged.length > maxPrefixRetryCharDiff || |
| 153 | + this.documentChangeTrace.count > maxPrefixRetryCharDiff |
| 154 | + ) { |
| 155 | + // Reject the suggestion if users've typed over 5 characters while the suggestion is shown |
| 156 | + this.autoReject(RejectReason.MaxRetry) |
| 157 | + } else if (applyPatch(this.editor.document.getText(), updatedPatch.insertText as string) === false) { |
| 158 | + this.autoReject(RejectReason.DocumentChange) |
| 159 | + } else { |
| 160 | + // Close the previoius popup and rerender it |
| 161 | + this.logger.debug(`calling rerender with suggestion\n ${updatedPatch.insertText as string}`) |
| 162 | + await this.debouncedRerender(updatedPatch) |
| 163 | + } |
| 164 | + } else { |
| 165 | + this.autoReject(RejectReason.NotApplicableToOriginal) |
| 166 | + } |
| 167 | + } catch (e) { |
| 168 | + this.logger.error(`encountered error while processing edit suggestion when users type ${e}`) |
| 169 | + // TODO: Maybe we should auto reject/hide suggestions in this scenario |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + async dispose() { |
| 174 | + this.documentChangedListener?.dispose() |
| 175 | + this.cursorChangedListener?.dispose() |
| 176 | + await decorationManager.clearDecorations(this.editor, []) |
| 177 | + } |
| 178 | + |
| 179 | + debouncedRerender = debounce( |
| 180 | + async (suggestion: InlineCompletionItemWithReferences) => await this.rerender(suggestion), |
| 181 | + rerenderDeboucneInMs, |
| 182 | + true |
| 183 | + ) |
| 184 | + |
| 185 | + private async rerender(suggestion: InlineCompletionItemWithReferences) { |
| 186 | + await decorationManager.clearDecorations(this.editor, []) |
| 187 | + await this.show(suggestion) |
| 188 | + } |
| 189 | + |
| 190 | + private autoReject(reason: string) { |
| 191 | + function logSuggestionFailure(type: 'REJECT', reason: string, suggestionContent: string) { |
| 192 | + getLogger('nextEditPrediction').debug( |
| 193 | + `Auto ${type} edit suggestion with reason=${reason}, suggetion: ${suggestionContent}` |
52 | 194 | ) |
53 | | - } else { |
54 | | - getLogger('nextEditPrediction').error('SVG image generation returned an empty result.') |
55 | 195 | } |
56 | | - } catch (error) { |
57 | | - getLogger('nextEditPrediction').error(`Error generating SVG image: ${error}`) |
| 196 | + |
| 197 | + logSuggestionFailure('REJECT', reason, this.suggestion.insertText as string) |
| 198 | + void vscode.commands.executeCommand('aws.amazonq.inline.rejectEdit') |
| 199 | + } |
| 200 | + |
| 201 | + private patchSuggestion(appliedToOriginal: string): InlineCompletionItemWithReferences { |
| 202 | + const updatedPatch = createPatch( |
| 203 | + this.editor.document.fileName, |
| 204 | + this.editor.document.getText(), |
| 205 | + appliedToOriginal |
| 206 | + ) |
| 207 | + this.logger.info(`Update edit suggestion\n ${updatedPatch}`) |
| 208 | + return { ...this.suggestion, insertText: updatedPatch } |
58 | 209 | } |
59 | 210 | } |
0 commit comments