Skip to content

Commit bd16a30

Browse files
committed
fix(amazonq): discard suggestion if it isn't valid
1 parent 76a1cd0 commit bd16a30

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 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,36 @@ 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) return
310+
if (e.document !== editor.document) return
311+
if (vsCodeState.isCodeWhispererEditing) return
312+
if (getContext('aws.amazonq.editSuggestionActive') === false) return
313+
314+
const isPatchValid = applyPatch(e.document.getText(), item.insertText as string)
315+
if (!isPatchValid) {
316+
vscode.commands.executeCommand('aws.amazonq.inline.rejectEdit')
317+
}
318+
})
289319
await decorationManager.displayEditSuggestion(
290320
editor,
291321
svgImage,
@@ -310,6 +340,7 @@ export async function displaySvgDecoration(
310340
editor.selection = new vscode.Selection(endPosition, endPosition)
311341

312342
await decorationManager.clearDecorations(editor)
343+
documentChangeListener.dispose()
313344
const params: LogInlineCompletionSessionResultsParams = {
314345
sessionId: session.sessionId,
315346
completionSessionResult: {
@@ -343,6 +374,7 @@ export async function displaySvgDecoration(
343374
// Handle reject
344375
getLogger().info('Edit suggestion rejected')
345376
await decorationManager.clearDecorations(editor)
377+
documentChangeListener.dispose()
346378
const params: LogInlineCompletionSessionResultsParams = {
347379
sessionId: session.sessionId,
348380
completionSessionResult: {

0 commit comments

Comments
 (0)