@@ -18,30 +18,26 @@ import { conditionalRegistration, requireMinVersion, requireSomeCapability } fro
18
18
19
19
20
20
interface OrganizeImportsCommandMetadata {
21
- readonly ids : readonly string [ ] ;
22
21
readonly title : string ;
23
22
readonly minVersion ?: API ;
24
23
readonly kind : vscode . CodeActionKind ;
25
24
readonly mode : OrganizeImportsMode ;
26
25
}
27
26
28
27
const organizeImportsCommand : OrganizeImportsCommandMetadata = {
29
- ids : [ 'typescript.organizeImports' ] ,
30
28
title : vscode . l10n . t ( "Organize Imports" ) ,
31
29
kind : vscode . CodeActionKind . SourceOrganizeImports ,
32
30
mode : OrganizeImportsMode . All ,
33
31
} ;
34
32
35
33
const sortImportsCommand : OrganizeImportsCommandMetadata = {
36
- ids : [ 'typescript.sortImports' , 'javascript.sortImports' ] ,
37
34
minVersion : API . v430 ,
38
35
title : vscode . l10n . t ( "Sort Imports" ) ,
39
36
kind : vscode . CodeActionKind . Source . append ( 'sortImports' ) ,
40
37
mode : OrganizeImportsMode . SortAndCombine ,
41
38
} ;
42
39
43
40
const removeUnusedImportsCommand : OrganizeImportsCommandMetadata = {
44
- ids : [ 'typescript.removeUnusedImports' , 'javascript.removeUnusedImports' ] ,
45
41
minVersion : API . v490 ,
46
42
title : vscode . l10n . t ( "Remove Unused Imports" ) ,
47
43
kind : vscode . CodeActionKind . Source . append ( 'removeUnusedImports' ) ,
@@ -50,14 +46,14 @@ const removeUnusedImportsCommand: OrganizeImportsCommandMetadata = {
50
46
51
47
class OrganizeImportsCommand implements Command {
52
48
49
+ public static readonly ID = '_typescript.organizeImports' ;
50
+ public readonly id = OrganizeImportsCommand . ID ;
51
+
53
52
constructor (
54
- public readonly id : string ,
55
- private readonly commandMetadata : OrganizeImportsCommandMetadata ,
56
- private readonly client : ITypeScriptServiceClient ,
57
53
private readonly telemetryReporter : TelemetryReporter ,
58
54
) { }
59
55
60
- public async execute ( file ?: string ) : Promise < any > {
56
+ public async execute ( ) : Promise < any > {
61
57
/* __GDPR__
62
58
"organizeImports.execute" : {
63
59
"owner": "mjbvz",
@@ -67,48 +63,20 @@ class OrganizeImportsCommand implements Command {
67
63
}
68
64
*/
69
65
this . telemetryReporter . logTelemetry ( 'organizeImports.execute' , { } ) ;
70
- if ( ! file ) {
71
- const activeEditor = vscode . window . activeTextEditor ;
72
- if ( ! activeEditor ) {
73
- vscode . window . showErrorMessage ( vscode . l10n . t ( "Organize Imports failed. No resource provided." ) ) ;
74
- return ;
75
- }
76
-
77
- const resource = activeEditor . document . uri ;
78
- const document = await vscode . workspace . openTextDocument ( resource ) ;
79
- const openedFiledPath = this . client . toOpenTsFilePath ( document ) ;
80
- if ( ! openedFiledPath ) {
81
- vscode . window . showErrorMessage ( vscode . l10n . t ( "Organize Imports failed. Unknown file type." ) ) ;
82
- return ;
83
- }
84
-
85
- file = openedFiledPath ;
86
- }
87
-
88
- const args : Proto . OrganizeImportsRequestArgs = {
89
- scope : {
90
- type : 'file' ,
91
- args : {
92
- file
93
- }
94
- } ,
95
- // Deprecated in 4.9; `mode` takes priority
96
- skipDestructiveCodeActions : this . commandMetadata . mode === OrganizeImportsMode . SortAndCombine ,
97
- mode : typeConverters . OrganizeImportsMode . toProtocolOrganizeImportsMode ( this . commandMetadata . mode ) ,
98
- } ;
99
- const response = await this . client . interruptGetErr ( ( ) => this . client . execute ( 'organizeImports' , args , nulToken ) ) ;
100
- if ( response . type !== 'response' || ! response . body ) {
101
- return ;
102
- }
66
+ }
67
+ }
103
68
104
- if ( response . body . length ) {
105
- const edits = typeConverters . WorkspaceEdit . fromFileCodeEdits ( this . client , response . body ) ;
106
- return vscode . workspace . applyEdit ( edits ) ;
107
- }
69
+ class ImportCodeAction extends vscode . CodeAction {
70
+ constructor (
71
+ title : string ,
72
+ kind : vscode . CodeActionKind ,
73
+ public readonly document : vscode . TextDocument ,
74
+ ) {
75
+ super ( title , kind ) ;
108
76
}
109
77
}
110
78
111
- class ImportsCodeActionProvider implements vscode . CodeActionProvider {
79
+ class ImportsCodeActionProvider implements vscode . CodeActionProvider < ImportCodeAction > {
112
80
113
81
constructor (
114
82
private readonly client : ITypeScriptServiceClient ,
@@ -117,31 +85,62 @@ class ImportsCodeActionProvider implements vscode.CodeActionProvider {
117
85
private readonly fileConfigManager : FileConfigurationManager ,
118
86
telemetryReporter : TelemetryReporter ,
119
87
) {
120
- for ( const id of commandMetadata . ids ) {
121
- commandManager . register ( new OrganizeImportsCommand ( id , commandMetadata , client , telemetryReporter ) ) ;
122
- }
88
+ commandManager . register ( new OrganizeImportsCommand ( telemetryReporter ) ) ;
123
89
}
124
90
125
91
public provideCodeActions (
126
92
document : vscode . TextDocument ,
127
93
_range : vscode . Range ,
128
94
context : vscode . CodeActionContext ,
129
- token : vscode . CancellationToken
130
- ) : vscode . CodeAction [ ] {
95
+ _token : vscode . CancellationToken
96
+ ) : ImportCodeAction [ ] {
97
+ if ( ! context . only ?. contains ( this . commandMetadata . kind ) ) {
98
+ return [ ] ;
99
+ }
100
+
131
101
const file = this . client . toOpenTsFilePath ( document ) ;
132
102
if ( ! file ) {
133
103
return [ ] ;
134
104
}
135
105
136
- if ( ! context . only ?. contains ( this . commandMetadata . kind ) ) {
137
- return [ ] ;
106
+ return [ new ImportCodeAction ( this . commandMetadata . title , this . commandMetadata . kind , document ) ] ;
107
+ }
108
+
109
+ async resolveCodeAction ( codeAction : ImportCodeAction , token : vscode . CancellationToken ) : Promise < ImportCodeAction | undefined > {
110
+ const response = await this . client . interruptGetErr ( async ( ) => {
111
+ await this . fileConfigManager . ensureConfigurationForDocument ( codeAction . document , token ) ;
112
+ if ( token . isCancellationRequested ) {
113
+ return ;
114
+ }
115
+
116
+ const file = this . client . toOpenTsFilePath ( codeAction . document ) ;
117
+ if ( ! file ) {
118
+ return ;
119
+ }
120
+
121
+ const args : Proto . OrganizeImportsRequestArgs = {
122
+ scope : {
123
+ type : 'file' ,
124
+ args : { file }
125
+ } ,
126
+ // Deprecated in 4.9; `mode` takes priority
127
+ skipDestructiveCodeActions : this . commandMetadata . mode === OrganizeImportsMode . SortAndCombine ,
128
+ mode : typeConverters . OrganizeImportsMode . toProtocolOrganizeImportsMode ( this . commandMetadata . mode ) ,
129
+ } ;
130
+
131
+ return this . client . execute ( 'organizeImports' , args , nulToken ) ;
132
+ } ) ;
133
+ if ( response ?. type !== 'response' || ! response . body || token . isCancellationRequested ) {
134
+ return ;
135
+ }
136
+
137
+ if ( response . body . length ) {
138
+ codeAction . edit = typeConverters . WorkspaceEdit . fromFileCodeEdits ( this . client , response . body ) ;
138
139
}
139
140
140
- this . fileConfigManager . ensureConfigurationForDocument ( document , token ) ;
141
+ codeAction . command = { command : OrganizeImportsCommand . ID , title : '' , arguments : [ ] } ;
141
142
142
- const action = new vscode . CodeAction ( this . commandMetadata . title , this . commandMetadata . kind ) ;
143
- action . command = { title : '' , command : this . commandMetadata . ids [ 0 ] , arguments : [ file ] } ;
144
- return [ action ] ;
143
+ return codeAction ;
145
144
}
146
145
}
147
146
0 commit comments