Skip to content

Commit 5b3048f

Browse files
authored
fix(amazonq): discard/reject edit suggestion if it isn't valid (#7848)
## Problem discard when it's not yet shown. reject when user's typing and makes the diff invalid. ## Solution --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 0e09993 commit 5b3048f

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

packages/amazonq/src/app/inline/EditRendering/displayImage.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { getLogger, setContext } from 'aws-core-vscode/shared'
6+
import { getContext, getLogger, setContext } from 'aws-core-vscode/shared'
77
import * as vscode from 'vscode'
8-
import { diffLines } from 'diff'
8+
import { applyPatch, diffLines } from 'diff'
99
import { LanguageClient } from 'vscode-languageclient'
1010
import { CodeWhispererSession } from '../sessionManager'
1111
import { LogInlineCompletionSessionResultsParams } from '@aws/language-server-runtimes/protocol'
@@ -286,6 +286,44 @@ export async function displaySvgDecoration(
286286
) {
287287
const originalCode = editor.document.getText()
288288

289+
const isPatchValid = applyPatch(editor.document.getText(), item.insertText as string)
290+
if (!isPatchValid) {
291+
const params: LogInlineCompletionSessionResultsParams = {
292+
sessionId: session.sessionId,
293+
completionSessionResult: {
294+
[item.itemId]: {
295+
seen: false,
296+
accepted: false,
297+
discarded: true,
298+
},
299+
},
300+
totalSessionDisplayTime: Date.now() - session.requestStartTime,
301+
firstCompletionDisplayLatency: session.firstCompletionDisplayLatency,
302+
isInlineEdit: true,
303+
}
304+
// TODO: this session is closed on flare side hence discarded is not emitted in flare
305+
languageClient.sendNotification('aws/logInlineCompletionSessionResults', params)
306+
return
307+
}
308+
const documentChangeListener = vscode.workspace.onDidChangeTextDocument((e) => {
309+
if (e.contentChanges.length <= 0) {
310+
return
311+
}
312+
if (e.document !== editor.document) {
313+
return
314+
}
315+
if (vsCodeState.isCodeWhispererEditing) {
316+
return
317+
}
318+
if (getContext('aws.amazonq.editSuggestionActive') === false) {
319+
return
320+
}
321+
322+
const isPatchValid = applyPatch(e.document.getText(), item.insertText as string)
323+
if (!isPatchValid) {
324+
void vscode.commands.executeCommand('aws.amazonq.inline.rejectEdit')
325+
}
326+
})
289327
await decorationManager.displayEditSuggestion(
290328
editor,
291329
svgImage,
@@ -310,6 +348,7 @@ export async function displaySvgDecoration(
310348
editor.selection = new vscode.Selection(endPosition, endPosition)
311349

312350
await decorationManager.clearDecorations(editor)
351+
documentChangeListener.dispose()
313352
const params: LogInlineCompletionSessionResultsParams = {
314353
sessionId: session.sessionId,
315354
completionSessionResult: {
@@ -343,6 +382,7 @@ export async function displaySvgDecoration(
343382
// Handle reject
344383
getLogger().info('Edit suggestion rejected')
345384
await decorationManager.clearDecorations(editor)
385+
documentChangeListener.dispose()
346386
const params: LogInlineCompletionSessionResultsParams = {
347387
sessionId: session.sessionId,
348388
completionSessionResult: {

0 commit comments

Comments
 (0)