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,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