@@ -10,47 +10,66 @@ export class PackageJsonCodeActionProvider
1010 range : vscode . Range ,
1111 ctx : vscode . CodeActionContext
1212 ) : Promise < vscode . CodeAction [ ] > {
13- // TODO: Update command to allow multiline selection
14- if ( ! range . isSingleLine ) {
15- return Promise . resolve ( [ ] )
16- }
13+ // Get all diagnostics from this extension
14+ const diagnostics = ctx . diagnostics . filter (
15+ ( diagnostic ) => diagnostic . code === DIAGNOSTIC_CODE
16+ )
1717
18- // For each diagnostic entry that has the matching `code`,
19- // create a code action command.
20- const promises = ctx . diagnostics
21- . filter ( ( diagnostic ) => diagnostic . code === DIAGNOSTIC_CODE )
22- . map ( ( diagnostic ) => this . createCommandCodeAction ( doc , diagnostic ) )
18+ // For each diagnostic from this extension, create a code action
19+ const promises = range . isSingleLine
20+ ? diagnostics . map ( ( diag ) => this . createCommandCodeAction ( doc , diag ) )
21+ : [
22+ this . createUpdateManyCodeAction (
23+ doc ,
24+ diagnostics ,
25+ `Update ${ diagnostics . length } packages`
26+ ) ,
27+ ]
2328
2429 const allDiagnostics = vscode . languages
2530 . getDiagnostics ( doc . uri )
2631 . filter ( ( diagnostic ) => diagnostic . code === DIAGNOSTIC_CODE )
2732
2833 // Only show the update all code action if there are outdated packages
2934 if ( allDiagnostics . length ) {
30- promises . push ( this . createUpdateAllCodeAction ( doc , allDiagnostics ) )
35+ promises . push (
36+ this . createUpdateManyCodeAction (
37+ doc ,
38+ allDiagnostics ,
39+ "Update all packages"
40+ )
41+ )
3142 }
3243
3344 return Promise . all ( promises )
3445 }
3546
36- private async createUpdateAllCodeAction (
37- doc : vscode . TextDocument ,
38- diagnostics : vscode . Diagnostic [ ]
39- ) {
47+ private createAction ( message : string ) {
4048 const edit = new vscode . WorkspaceEdit ( )
4149 const action = new vscode . CodeAction (
42- "Update all packages" ,
50+ message ,
4351 vscode . CodeActionKind . QuickFix
4452 )
4553
4654 action . edit = edit
4755 action . isPreferred = true
56+
57+ return [ action , edit ] as const
58+ }
59+
60+ private async createUpdateManyCodeAction (
61+ doc : vscode . TextDocument ,
62+ diagnostics : vscode . Diagnostic [ ] ,
63+ message : string
64+ ) {
65+ const [ action , edit ] = this . createAction ( message )
4866 action . diagnostics = diagnostics
4967
50- const promises = action . diagnostics . map ( ( diagnostic ) =>
51- this . createEdit ( edit , doc , diagnostic . range )
68+ await Promise . all (
69+ action . diagnostics . map ( ( diagnostic ) =>
70+ this . createEdit ( edit , doc , diagnostic . range )
71+ )
5272 )
53- await Promise . all ( promises )
5473
5574 return action
5675 }
@@ -59,18 +78,11 @@ export class PackageJsonCodeActionProvider
5978 doc : vscode . TextDocument ,
6079 diagnostic : vscode . Diagnostic
6180 ) {
62- const edit = new vscode . WorkspaceEdit ( )
63- const action = new vscode . CodeAction (
64- "Update package" ,
65- vscode . CodeActionKind . QuickFix
66- )
81+ const [ action , edit ] = this . createAction ( "Update package" )
82+ action . diagnostics = [ diagnostic ]
6783
6884 await this . createEdit ( edit , doc , diagnostic . range )
6985
70- action . edit = edit
71- action . diagnostics = [ diagnostic ]
72- action . isPreferred = true
73-
7486 return action
7587 }
7688
0 commit comments