3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
+ import { getLogger , setContext } from 'aws-core-vscode/shared'
6
7
import { getLogger , setContext } from 'aws-core-vscode/shared'
7
8
import * as vscode from 'vscode'
8
9
import { diffLines } from 'diff'
@@ -20,6 +21,7 @@ export class EditDecorationManager {
20
21
private currentRemovedCodeDecorations : vscode . DecorationOptions [ ] = [ ]
21
22
private acceptHandler : ( ( ) => void ) | undefined
22
23
private rejectHandler : ( ( ) => void ) | undefined
24
+ private disposables : vscode . Disposable [ ] = [ ]
23
25
24
26
constructor ( ) {
25
27
this . imageDecorationType = vscode . window . createTextEditorDecorationType ( {
@@ -29,8 +31,6 @@ export class EditDecorationManager {
29
31
this . removedCodeDecorationType = vscode . window . createTextEditorDecorationType ( {
30
32
backgroundColor : 'rgba(255, 0, 0, 0.2)' ,
31
33
} )
32
-
33
- this . registerCommandHandlers ( )
34
34
}
35
35
36
36
/**
@@ -137,6 +137,7 @@ export class EditDecorationManager {
137
137
originalCodeHighlightRanges : Array < { line : number ; start : number ; end : number } >
138
138
) : void {
139
139
// Clear any existing decorations
140
+ this . registerCommandHandlers ( )
140
141
this . clearDecorations ( editor )
141
142
142
143
// Set context to enable the Tab key handler
@@ -180,24 +181,30 @@ export class EditDecorationManager {
180
181
*/
181
182
public registerCommandHandlers ( ) : void {
182
183
// Register Tab key handler for accepting suggestion
183
- vscode . commands . registerCommand ( 'aws.amazonq.inline.acceptEdit' , ( ) => {
184
+ const acceptDisposable = vscode . commands . registerCommand ( 'aws.amazonq.inline.acceptEdit' , ( ) => {
184
185
if ( this . acceptHandler ) {
185
186
this . acceptHandler ( )
186
187
}
187
188
} )
189
+ this . disposables . push ( acceptDisposable )
188
190
189
191
// Register Esc key handler for rejecting suggestion
190
- vscode . commands . registerCommand ( 'aws.amazonq.inline.rejectEdit' , ( ) => {
192
+ const rejectDisposable = vscode . commands . registerCommand ( 'aws.amazonq.inline.rejectEdit' , ( ) => {
191
193
if ( this . rejectHandler ) {
192
194
this . rejectHandler ( )
193
195
}
194
196
} )
197
+ this . disposables . push ( rejectDisposable )
195
198
}
196
199
197
200
/**
198
201
* Disposes resources
199
202
*/
200
203
public dispose ( ) : void {
204
+ for ( const disposable of this . disposables ) {
205
+ disposable . dispose ( )
206
+ }
207
+ this . disposables = [ ]
201
208
this . imageDecorationType . dispose ( )
202
209
this . removedCodeDecorationType . dispose ( )
203
210
}
@@ -290,13 +297,22 @@ export async function displaySvgDecoration(
290
297
// Handle accept
291
298
getLogger ( ) . info ( 'Edit suggestion accepted' )
292
299
300
+ // Replace content
301
+
302
+ // Calculate cursor position before replacing content
303
+ const endPosition = getEndOfEditPosition ( originalCode , newCode )
304
+
293
305
// Replace content
294
306
replaceEditorContent ( editor , newCode )
295
307
296
308
// Move cursor to end of the actual changed content
297
309
const endPosition = getEndOfEditPosition ( originalCode , newCode )
298
310
editor . selection = new vscode . Selection ( endPosition , endPosition )
299
311
312
+ // Move cursor to end of the actual changed content
313
+ editor . selection = new vscode . Selection ( endPosition , endPosition )
314
+
315
+
300
316
// Move cursor to end of the actual changed content
301
317
editor . selection = new vscode . Selection ( endPosition , endPosition )
302
318
@@ -315,6 +331,7 @@ export async function displaySvgDecoration(
315
331
isInlineEdit : true ,
316
332
}
317
333
languageClient . sendNotification ( 'aws/logInlineCompletionSessionResults' , params )
334
+ decorationManager . dispose ( )
318
335
} ,
319
336
( ) => {
320
337
// Handle reject
@@ -332,6 +349,7 @@ export async function displaySvgDecoration(
332
349
isInlineEdit : true ,
333
350
}
334
351
languageClient . sendNotification ( 'aws/logInlineCompletionSessionResults' , params )
352
+ decorationManager . dispose ( )
335
353
} ,
336
354
originalCode ,
337
355
newCode ,
0 commit comments