@@ -52,22 +52,38 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
52
52
insertSpaces : < boolean > currentEditor . options . insertSpaces ,
53
53
} ;
54
54
const commandArguments : any [ ] = [ ] ;
55
- if ( command === 'extractField' || command === 'convertVariableToField' ) {
56
- if ( commandInfo . initializedScopes && Array . isArray ( commandInfo . initializedScopes ) ) {
57
- const scopes : any [ ] = commandInfo . initializedScopes ;
58
- let initializeIn : string ;
59
- if ( scopes . length === 1 ) {
60
- initializeIn = scopes [ 0 ] ;
61
- } else if ( scopes . length > 1 ) {
62
- initializeIn = await window . showQuickPick ( scopes , {
63
- placeHolder : "Initialize the field in" ,
64
- } ) ;
65
-
55
+ if ( command === 'extractField' ) {
56
+ if ( ! params || ! params . range ) {
57
+ return ;
58
+ }
59
+ if ( params . range . start . character === params . range . end . character && params . range . start . line === params . range . end . line ) {
60
+ const expression : SelectionInfo = await getExpression ( command , params , languageClient ) ;
61
+ if ( ! expression ) {
62
+ return ;
63
+ }
64
+ if ( expression . params && Array . isArray ( expression . params ) ) {
65
+ const initializeIn = await resolveScopes ( expression . params ) ;
66
66
if ( ! initializeIn ) {
67
67
return ;
68
68
}
69
+ commandArguments . push ( initializeIn ) ;
70
+ }
71
+ commandArguments . push ( expression ) ;
72
+ } else {
73
+ if ( commandInfo . initializedScopes && Array . isArray ( commandInfo . initializedScopes ) ) {
74
+ const initializeIn = await resolveScopes ( commandInfo . initializedScopes ) ;
75
+ if ( ! initializeIn ) {
76
+ return ;
77
+ }
78
+ commandArguments . push ( initializeIn ) ;
79
+ }
80
+ }
81
+ } else if ( command === 'convertVariableToField' ) {
82
+ if ( commandInfo . initializedScopes && Array . isArray ( commandInfo . initializedScopes ) ) {
83
+ const initializeIn = await resolveScopes ( commandInfo . initializedScopes ) ;
84
+ if ( ! initializeIn ) {
85
+ return ;
69
86
}
70
-
71
87
commandArguments . push ( initializeIn ) ;
72
88
}
73
89
} else if ( command === 'extractMethod'
@@ -78,36 +94,11 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
78
94
return ;
79
95
}
80
96
if ( params . range . start . character === params . range . end . character && params . range . start . line === params . range . end . line ) {
81
- const expressions : SelectionInfo [ ] = await languageClient . sendRequest ( InferSelectionRequest . type , {
82
- command : command ,
83
- context : params ,
84
- } ) ;
85
- const options : IExpressionItem [ ] = [ ] ;
86
- for ( const expression of expressions ) {
87
- const extractItem : IExpressionItem = {
88
- label : expression . name ,
89
- length : expression . length ,
90
- offset : expression . offset ,
91
- } ;
92
- options . push ( extractItem ) ;
93
- }
94
- let resultItem : IExpressionItem ;
95
- if ( options . length === 1 ) {
96
- resultItem = options [ 0 ] ;
97
- } else if ( options . length > 1 ) {
98
- resultItem = await window . showQuickPick < IExpressionItem > ( options , {
99
- placeHolder : "Choose the expression to extract" ,
100
- } ) ;
101
- }
102
- if ( ! resultItem ) {
97
+ const expression = await getExpression ( command , params , languageClient ) ;
98
+ if ( ! expression ) {
103
99
return ;
104
100
}
105
- const resultExpression : SelectionInfo = {
106
- name : resultItem . label ,
107
- length : resultItem . length ,
108
- offset : resultItem . offset ,
109
- } ;
110
- commandArguments . push ( resultExpression ) ;
101
+ commandArguments . push ( expression ) ;
111
102
}
112
103
}
113
104
@@ -135,10 +126,62 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
135
126
} ) ) ;
136
127
}
137
128
129
+ async function resolveScopes ( scopes : any [ ] ) : Promise < any | undefined > {
130
+ let initializeIn : string ;
131
+ if ( scopes . length === 1 ) {
132
+ initializeIn = scopes [ 0 ] ;
133
+ } else if ( scopes . length > 1 ) {
134
+ initializeIn = await window . showQuickPick ( scopes , {
135
+ placeHolder : "Initialize the field in" ,
136
+ } ) ;
137
+
138
+ if ( ! initializeIn ) {
139
+ return undefined ;
140
+ }
141
+ }
142
+ return initializeIn ;
143
+ }
144
+
145
+ async function getExpression ( command : string , params : any , languageClient : LanguageClient ) : Promise < SelectionInfo | undefined > {
146
+ const expressions : SelectionInfo [ ] = await languageClient . sendRequest ( InferSelectionRequest . type , {
147
+ command : command ,
148
+ context : params ,
149
+ } ) ;
150
+ const options : IExpressionItem [ ] = [ ] ;
151
+ for ( const expression of expressions ) {
152
+ const extractItem : IExpressionItem = {
153
+ label : expression . name ,
154
+ length : expression . length ,
155
+ offset : expression . offset ,
156
+ params : expression . params ,
157
+ } ;
158
+ options . push ( extractItem ) ;
159
+ }
160
+ let resultItem : IExpressionItem ;
161
+ if ( options . length === 1 ) {
162
+ resultItem = options [ 0 ] ;
163
+ } else if ( options . length > 1 ) {
164
+ resultItem = await window . showQuickPick < IExpressionItem > ( options , {
165
+ placeHolder : "Choose the expression to extract" ,
166
+ } ) ;
167
+ }
168
+ if ( ! resultItem ) {
169
+ return undefined ;
170
+ }
171
+ const resultExpression : SelectionInfo = {
172
+ name : resultItem . label ,
173
+ length : resultItem . length ,
174
+ offset : resultItem . offset ,
175
+ params : resultItem . params ,
176
+ } ;
177
+ return resultExpression ;
178
+ }
179
+
138
180
interface IExpressionItem extends QuickPickItem {
139
181
label : string ;
140
182
length : number ;
141
183
offset : number ;
184
+ params ?: string [ ] ;
142
185
}
143
186
144
187
async function applyRefactorEdit ( languageClient : LanguageClient , refactorEdit : RefactorWorkspaceEdit ) {
0 commit comments