@@ -11,6 +11,9 @@ import {
11
11
ProgressLocation ,
12
12
ThemeIcon ,
13
13
QuickPickItem ,
14
+ CodeActionKind ,
15
+ CodeAction ,
16
+ Range ,
14
17
} from "vscode" ;
15
18
import os from "os" ;
16
19
import path from "path" ;
@@ -25,6 +28,8 @@ import CommandPalette from "./CommandPalette";
25
28
import { showOutputPanel } from "./logger" ;
26
29
import { Issues } from "./Issues" ;
27
30
import { InlineEditController } from "./inline-edit" ;
31
+ import { getLogger } from "./logger" ;
32
+
28
33
29
34
export class Commands {
30
35
private chatEditCancellationTokenSource : CancellationTokenSource | null = null ;
@@ -111,7 +116,7 @@ export class Commands {
111
116
try {
112
117
const url = new URL ( input ) ;
113
118
assert ( url . protocol == "http:" || url . protocol == "https:" ) ;
114
- } catch ( error ) {
119
+ } catch ( error : any ) {
115
120
return {
116
121
message : "Please enter a validate http or https URL." ,
117
122
severity : InputBoxValidationSeverity . Error ,
@@ -218,8 +223,16 @@ export class Commands {
218
223
"inlineCompletion.trigger" : ( ) => {
219
224
commands . executeCommand ( "editor.action.inlineSuggest.trigger" ) ;
220
225
} ,
221
- "inlineCompletion.accept" : ( ) => {
222
- commands . executeCommand ( "editor.action.inlineSuggest.commit" ) ;
226
+ "inlineCompletion.accept" : async ( ) => {
227
+ const editor = window . activeTextEditor ;
228
+ if ( ! editor ) {
229
+ return ;
230
+ }
231
+ const uri = editor . document . uri ;
232
+ const range = new Range ( editor . selection . start . line , 0 , editor . selection . end . line + 1 , 0 ) ;
233
+ await commands . executeCommand ( "editor.action.inlineSuggest.commit" ) ;
234
+ await applyQuickFixes ( uri , range )
235
+
223
236
} ,
224
237
"inlineCompletion.acceptNextWord" : ( ) => {
225
238
this . inlineCompletionProvider . handleEvent ( "accept_word" ) ;
@@ -312,12 +325,10 @@ export class Commands {
312
325
}
313
326
const location = {
314
327
uri : editor . document . uri . toString ( ) ,
315
- range : {
316
- start : { line : editor . selection . start . line , character : 0 } ,
317
- end : { line : editor . selection . end . line + 1 , character : 0 } ,
318
- } ,
328
+ range : new Range ( editor . selection . start . line , 0 , editor . selection . end . line + 1 , 0 ) ,
319
329
} ;
320
330
await this . client . chat . resolveEdit ( { location, action : "accept" } ) ;
331
+ await applyQuickFixes ( editor . document . uri , location . range ) ;
321
332
} ,
322
333
"chat.edit.discard" : async ( ) => {
323
334
const editor = window . activeTextEditor ;
@@ -429,3 +440,22 @@ export class Commands {
429
440
} ,
430
441
} ;
431
442
}
443
+
444
+ async function applyQuickFixes ( uri : Uri , range : Range ) : Promise < void > {
445
+ const codeActions = await commands . executeCommand < CodeAction [ ] > ( "vscode.executeCodeActionProvider" , uri , range ) ;
446
+ const quickFixActions = codeActions . filter ( action => action . kind && action . kind . contains ( CodeActionKind . QuickFix ) && action . title . toLowerCase ( ) . includes ( 'import' ) ) ;
447
+ quickFixActions . forEach ( async ( action ) => {
448
+ try {
449
+ getLogger ( ) . info ( `Applying CodeActions for ${ action . title } .` )
450
+ if ( action . edit ) {
451
+ await workspace . applyEdit ( action . edit ) ;
452
+ }
453
+ if ( action . command ) {
454
+ await commands . executeCommand ( action . command . command , action . command . arguments ) ;
455
+ }
456
+ } catch ( error ) {
457
+ getLogger ( ) . error ( `Exception when apply CodeActions for ${ action . title } ` ) ;
458
+ // ignore the error
459
+ }
460
+ } ) ;
461
+ }
0 commit comments