@@ -20,19 +20,23 @@ import * as typeConverters from '../utils/typeConverters';
20
20
import { DiagnosticsManager } from './diagnostics' ;
21
21
import FileConfigurationManager from './fileConfigurationManager' ;
22
22
23
+ type ApplyCodeActionCommand_args = {
24
+ readonly resource : vscode . Uri ;
25
+ readonly diagnostic : vscode . Diagnostic ;
26
+ readonly action : Proto . CodeFixAction ;
27
+ } ;
23
28
24
29
class ApplyCodeActionCommand implements Command {
25
30
public static readonly ID = '_typescript.applyCodeActionCommand' ;
26
31
public readonly id = ApplyCodeActionCommand . ID ;
27
32
28
33
constructor (
29
34
private readonly client : ITypeScriptServiceClient ,
35
+ private readonly diagnosticManager : DiagnosticsManager ,
30
36
private readonly telemetryReporter : TelemetryReporter ,
31
37
) { }
32
38
33
- public async execute (
34
- action : Proto . CodeFixAction
35
- ) : Promise < boolean > {
39
+ public async execute ( { resource, action, diagnostic } : ApplyCodeActionCommand_args ) : Promise < boolean > {
36
40
/* __GDPR__
37
41
"quickFix.execute" : {
38
42
"owner": "mjbvz",
@@ -46,6 +50,7 @@ class ApplyCodeActionCommand implements Command {
46
50
fixName : action . fixName
47
51
} ) ;
48
52
53
+ this . diagnosticManager . deleteDiagnostic ( resource , diagnostic ) ;
49
54
return applyCodeActionCommands ( this . client , action . commands , nulToken ) ;
50
55
}
51
56
}
@@ -212,7 +217,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
212
217
private readonly diagnosticsManager : DiagnosticsManager ,
213
218
telemetryReporter : TelemetryReporter
214
219
) {
215
- commandManager . register ( new ApplyCodeActionCommand ( client , telemetryReporter ) ) ;
220
+ commandManager . register ( new ApplyCodeActionCommand ( client , diagnosticsManager , telemetryReporter ) ) ;
216
221
commandManager . register ( new ApplyFixAllCodeAction ( client , telemetryReporter ) ) ;
217
222
218
223
this . supportedCodeActionProvider = new SupportedCodeActionProvider ( client ) ;
@@ -223,26 +228,32 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
223
228
_range : vscode . Range ,
224
229
context : vscode . CodeActionContext ,
225
230
token : vscode . CancellationToken
226
- ) : Promise < VsCodeCodeAction [ ] > {
231
+ ) : Promise < VsCodeCodeAction [ ] | undefined > {
227
232
const file = this . client . toOpenedFilePath ( document ) ;
228
233
if ( ! file ) {
229
- return [ ] ;
234
+ return ;
230
235
}
231
236
232
237
const fixableDiagnostics = await this . supportedCodeActionProvider . getFixableDiagnosticsForContext ( context ) ;
233
- if ( ! fixableDiagnostics . size ) {
234
- return [ ] ;
238
+ if ( ! fixableDiagnostics . size || token . isCancellationRequested ) {
239
+ return ;
235
240
}
236
241
237
242
if ( this . client . bufferSyncSupport . hasPendingDiagnostics ( document . uri ) ) {
238
- return [ ] ;
243
+ return ;
239
244
}
240
245
241
246
await this . formattingConfigurationManager . ensureConfigurationForDocument ( document , token ) ;
247
+ if ( token . isCancellationRequested ) {
248
+ return ;
249
+ }
242
250
243
251
const results = new CodeActionSet ( ) ;
244
252
for ( const diagnostic of fixableDiagnostics . values ) {
245
253
await this . getFixesForDiagnostic ( document , file , diagnostic , results , token ) ;
254
+ if ( token . isCancellationRequested ) {
255
+ return ;
256
+ }
246
257
}
247
258
248
259
const allActions = Array . from ( results . values ) ;
@@ -303,12 +314,13 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
303
314
diagnostic : vscode . Diagnostic ,
304
315
tsAction : Proto . CodeFixAction
305
316
) : CodeActionSet {
306
- results . addAction ( this . getSingleFixForTsCodeAction ( diagnostic , tsAction ) ) ;
307
- this . addFixAllForTsCodeAction ( results , document , file , diagnostic , tsAction as Proto . CodeFixAction ) ;
317
+ results . addAction ( this . getSingleFixForTsCodeAction ( document . uri , diagnostic , tsAction ) ) ;
318
+ this . addFixAllForTsCodeAction ( results , document . uri , file , diagnostic , tsAction as Proto . CodeFixAction ) ;
308
319
return results ;
309
320
}
310
321
311
322
private getSingleFixForTsCodeAction (
323
+ resource : vscode . Uri ,
312
324
diagnostic : vscode . Diagnostic ,
313
325
tsAction : Proto . CodeFixAction
314
326
) : VsCodeCodeAction {
@@ -317,15 +329,15 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
317
329
codeAction . diagnostics = [ diagnostic ] ;
318
330
codeAction . command = {
319
331
command : ApplyCodeActionCommand . ID ,
320
- arguments : [ tsAction ] ,
332
+ arguments : [ < ApplyCodeActionCommand_args > { action : tsAction , diagnostic , resource } ] ,
321
333
title : ''
322
334
} ;
323
335
return codeAction ;
324
336
}
325
337
326
338
private addFixAllForTsCodeAction (
327
339
results : CodeActionSet ,
328
- document : vscode . TextDocument ,
340
+ resource : vscode . Uri ,
329
341
file : string ,
330
342
diagnostic : vscode . Diagnostic ,
331
343
tsAction : Proto . CodeFixAction ,
@@ -335,7 +347,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCode
335
347
}
336
348
337
349
// Make sure there are multiple diagnostics of the same type in the file
338
- if ( ! this . diagnosticsManager . getDiagnostics ( document . uri ) . some ( x => {
350
+ if ( ! this . diagnosticsManager . getDiagnostics ( resource ) . some ( x => {
339
351
if ( x === diagnostic ) {
340
352
return false ;
341
353
}
0 commit comments