OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library services.src.refactoring.extract_method; | 5 library services.src.refactoring.extract_method; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analysis_server/src/protocol_server.dart' hide Element; | 9 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
10 import 'package:analysis_server/src/services/correction/name_suggestion.dart'; | 10 import 'package:analysis_server/src/services/correction/name_suggestion.dart'; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 212 |
213 @override | 213 @override |
214 RefactoringStatus checkName() { | 214 RefactoringStatus checkName() { |
215 return validateMethodName(name); | 215 return validateMethodName(name); |
216 } | 216 } |
217 | 217 |
218 @override | 218 @override |
219 Future<SourceChange> createChange() async { | 219 Future<SourceChange> createChange() async { |
220 SourceChange change = new SourceChange(refactoringName); | 220 SourceChange change = new SourceChange(refactoringName); |
221 // replace occurrences with method invocation | 221 // replace occurrences with method invocation |
222 for (_Occurrence occurence in _occurrences) { | 222 for (_Occurrence occurrence in _occurrences) { |
223 SourceRange range = occurence.range; | 223 SourceRange range = occurrence.range; |
224 // may be replacement of duplicates disabled | 224 // may be replacement of duplicates disabled |
225 if (!extractAll && !occurence.isSelection) { | 225 if (!extractAll && !occurrence.isSelection) { |
226 continue; | 226 continue; |
227 } | 227 } |
228 // prepare invocation source | 228 // prepare invocation source |
229 String invocationSource; | 229 String invocationSource; |
230 if (_selectionFunctionExpression != null) { | 230 if (_selectionFunctionExpression != null) { |
231 invocationSource = name; | 231 invocationSource = name; |
232 } else { | 232 } else { |
233 StringBuffer sb = new StringBuffer(); | 233 StringBuffer sb = new StringBuffer(); |
234 // may be returns value | 234 // may be returns value |
235 if (_selectionStatements != null && variableType != null) { | 235 if (_selectionStatements != null && variableType != null) { |
236 // single variable assignment / return statement | 236 // single variable assignment / return statement |
237 if (_returnVariableName != null) { | 237 if (_returnVariableName != null) { |
238 String occurrenceName = | 238 String occurrenceName = |
239 occurence._parameterOldToOccurrenceName[_returnVariableName]; | 239 occurrence._parameterOldToOccurrenceName[_returnVariableName]; |
240 // may be declare variable | 240 // may be declare variable |
241 if (!_parametersMap.containsKey(_returnVariableName)) { | 241 if (!_parametersMap.containsKey(_returnVariableName)) { |
242 if (variableType.isEmpty) { | 242 if (variableType.isEmpty) { |
243 sb.write('var '); | 243 sb.write('var '); |
244 } else { | 244 } else { |
245 sb.write(variableType); | 245 sb.write(variableType); |
246 sb.write(' '); | 246 sb.write(' '); |
247 } | 247 } |
248 } | 248 } |
249 // assign the return value | 249 // assign the return value |
(...skipping 15 matching lines...) Expand all Loading... |
265 for (RefactoringMethodParameter parameter in _parameters) { | 265 for (RefactoringMethodParameter parameter in _parameters) { |
266 // may be comma | 266 // may be comma |
267 if (firstParameter) { | 267 if (firstParameter) { |
268 firstParameter = false; | 268 firstParameter = false; |
269 } else { | 269 } else { |
270 sb.write(', '); | 270 sb.write(', '); |
271 } | 271 } |
272 // argument name | 272 // argument name |
273 { | 273 { |
274 String argumentName = | 274 String argumentName = |
275 occurence._parameterOldToOccurrenceName[parameter.id]; | 275 occurrence._parameterOldToOccurrenceName[parameter.id]; |
276 sb.write(argumentName); | 276 sb.write(argumentName); |
277 } | 277 } |
278 } | 278 } |
279 sb.write(')'); | 279 sb.write(')'); |
280 } | 280 } |
281 invocationSource = sb.toString(); | 281 invocationSource = sb.toString(); |
282 // statements as extracted with their ";", so add new after invocation | 282 // statements as extracted with their ";", so add new after invocation |
283 if (_selectionStatements != null) { | 283 if (_selectionStatements != null) { |
284 invocationSource += ';'; | 284 invocationSource += ';'; |
285 } | 285 } |
(...skipping 14 matching lines...) Expand all Loading... |
300 if (_staticContext) { | 300 if (_staticContext) { |
301 annotations = 'static '; | 301 annotations = 'static '; |
302 } | 302 } |
303 } | 303 } |
304 // prepare declaration source | 304 // prepare declaration source |
305 String declarationSource = null; | 305 String declarationSource = null; |
306 { | 306 { |
307 String returnExpressionSource = _getMethodBodySource(); | 307 String returnExpressionSource = _getMethodBodySource(); |
308 // closure | 308 // closure |
309 if (_selectionFunctionExpression != null) { | 309 if (_selectionFunctionExpression != null) { |
310 declarationSource = '${name}${returnExpressionSource}'; | 310 declarationSource = '$name$returnExpressionSource'; |
311 if (_selectionFunctionExpression.body is ExpressionFunctionBody) { | 311 if (_selectionFunctionExpression.body is ExpressionFunctionBody) { |
312 declarationSource += ';'; | 312 declarationSource += ';'; |
313 } | 313 } |
314 } | 314 } |
315 // optional 'async' body modifier | 315 // optional 'async' body modifier |
316 String asyncKeyword = _hasAwait ? ' async' : ''; | 316 String asyncKeyword = _hasAwait ? ' async' : ''; |
317 // expression | 317 // expression |
318 if (_selectionExpression != null) { | 318 if (_selectionExpression != null) { |
319 // add return type | 319 // add return type |
320 if (returnType.isNotEmpty) { | 320 if (returnType.isNotEmpty) { |
321 annotations += '$returnType '; | 321 annotations += '$returnType '; |
322 } | 322 } |
323 // just return expression | 323 // just return expression |
324 declarationSource = '$annotations$signature$asyncKeyword => '; | 324 declarationSource = '$annotations$signature$asyncKeyword => '; |
325 declarationSource += '$returnExpressionSource;'; | 325 declarationSource += '$returnExpressionSource;'; |
326 } | 326 } |
327 // statements | 327 // statements |
328 if (_selectionStatements != null) { | 328 if (_selectionStatements != null) { |
329 if (returnType.isNotEmpty) { | 329 if (returnType.isNotEmpty) { |
330 annotations += returnType + ' '; | 330 annotations += returnType + ' '; |
331 } | 331 } |
332 declarationSource = '$annotations$signature$asyncKeyword {$eol'; | 332 declarationSource = '$annotations$signature$asyncKeyword {$eol'; |
333 declarationSource += returnExpressionSource; | 333 declarationSource += returnExpressionSource; |
334 if (_returnVariableName != null) { | 334 if (_returnVariableName != null) { |
335 declarationSource += | 335 declarationSource += |
336 '${prefix} return ${_returnVariableName};$eol'; | 336 '$prefix return $_returnVariableName;$eol'; |
337 } | 337 } |
338 declarationSource += '${prefix}}'; | 338 declarationSource += '$prefix}'; |
339 } | 339 } |
340 } | 340 } |
341 // insert declaration | 341 // insert declaration |
342 if (declarationSource != null) { | 342 if (declarationSource != null) { |
343 int offset = _parentMember.end; | 343 int offset = _parentMember.end; |
344 SourceEdit edit = new SourceEdit( | 344 SourceEdit edit = new SourceEdit( |
345 offset, 0, '${eol}${eol}${prefix}${declarationSource}'); | 345 offset, 0, '$eol$eol$prefix$declarationSource'); |
346 doSourceChange_addElementEdit(change, unitElement, edit); | 346 doSourceChange_addElementEdit(change, unitElement, edit); |
347 } | 347 } |
348 } | 348 } |
349 // done | 349 // done |
350 addLibraryImports(change, libraryElement, librariesToImport); | 350 addLibraryImports(change, libraryElement, librariesToImport); |
351 return change; | 351 return change; |
352 } | 352 } |
353 | 353 |
354 @override | 354 @override |
355 bool requiresPreview() => false; | 355 bool requiresPreview() => false; |
(...skipping 855 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1211 return false; | 1211 return false; |
1212 } | 1212 } |
1213 for (int i = 0; i < parameterTypes.length; i++) { | 1213 for (int i = 0; i < parameterTypes.length; i++) { |
1214 if (other.parameterTypes[i] != parameterTypes[i]) { | 1214 if (other.parameterTypes[i] != parameterTypes[i]) { |
1215 return false; | 1215 return false; |
1216 } | 1216 } |
1217 } | 1217 } |
1218 return true; | 1218 return true; |
1219 } | 1219 } |
1220 } | 1220 } |
OLD | NEW |