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'
77import * as vscode from 'vscode'
8- import { diffLines } from 'diff'
8+ import { applyPatch , diffLines } from 'diff'
99import { LanguageClient } from 'vscode-languageclient'
1010import { CodeWhispererSession } from '../sessionManager'
1111import { 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