| 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.dart' hide Element; | 9 import 'package:analysis_server/src/protocol.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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * [ExtractMethodRefactoring] implementation. | 56 * [ExtractMethodRefactoring] implementation. |
| 57 */ | 57 */ |
| 58 class ExtractMethodRefactoringImpl extends RefactoringImpl implements | 58 class ExtractMethodRefactoringImpl extends RefactoringImpl implements |
| 59 ExtractMethodRefactoring { | 59 ExtractMethodRefactoring { |
| 60 final SearchEngine searchEngine; | 60 final SearchEngine searchEngine; |
| 61 final CompilationUnit unit; | 61 final CompilationUnit unit; |
| 62 final int selectionOffset; | 62 final int selectionOffset; |
| 63 final int selectionLength; | 63 final int selectionLength; |
| 64 String file; | 64 CompilationUnitElement unitElement; |
| 65 SourceRange selectionRange; | 65 SourceRange selectionRange; |
| 66 CorrectionUtils utils; | 66 CorrectionUtils utils; |
| 67 | 67 |
| 68 String returnType; | 68 String returnType; |
| 69 String name; | 69 String name; |
| 70 bool extractAll = true; | 70 bool extractAll = true; |
| 71 bool createGetter = false; | 71 bool createGetter = false; |
| 72 final List<String> names = <String>[]; | 72 final List<String> names = <String>[]; |
| 73 final List<int> offsets = <int>[]; | 73 final List<int> offsets = <int>[]; |
| 74 final List<int> lengths = <int>[]; | 74 final List<int> lengths = <int>[]; |
| 75 | 75 |
| 76 Set<String> _usedNames = new Set<String>(); | 76 Set<String> _usedNames = new Set<String>(); |
| 77 Set<String> _excludedNames = new Set<String>(); | 77 Set<String> _excludedNames = new Set<String>(); |
| 78 List<RefactoringMethodParameter> _parameters = <RefactoringMethodParameter>[]; | 78 List<RefactoringMethodParameter> _parameters = <RefactoringMethodParameter>[]; |
| 79 Map<String, RefactoringMethodParameter> _parametersMap = <String, | 79 Map<String, RefactoringMethodParameter> _parametersMap = <String, |
| 80 RefactoringMethodParameter>{}; | 80 RefactoringMethodParameter>{}; |
| 81 Map<String, List<SourceRange>> _parameterReferencesMap = <String, | 81 Map<String, List<SourceRange>> _parameterReferencesMap = <String, |
| 82 List<SourceRange>>{}; | 82 List<SourceRange>>{}; |
| 83 DartType _returnType; | 83 DartType _returnType; |
| 84 String _returnVariableName; | 84 String _returnVariableName; |
| 85 AstNode _parentMember; | 85 AstNode _parentMember; |
| 86 Expression _selectionExpression; | 86 Expression _selectionExpression; |
| 87 FunctionExpression _selectionFunctionExpression; | 87 FunctionExpression _selectionFunctionExpression; |
| 88 List<Statement> _selectionStatements; | 88 List<Statement> _selectionStatements; |
| 89 List<_Occurrence> _occurrences = []; | 89 List<_Occurrence> _occurrences = []; |
| 90 bool _staticContext = false; | 90 bool _staticContext = false; |
| 91 | 91 |
| 92 ExtractMethodRefactoringImpl(this.searchEngine, this.unit, | 92 ExtractMethodRefactoringImpl(this.searchEngine, this.unit, |
| 93 this.selectionOffset, this.selectionLength) { | 93 this.selectionOffset, this.selectionLength) { |
| 94 file = unit.element.source.fullName; | 94 unitElement = unit.element; |
| 95 selectionRange = new SourceRange(selectionOffset, selectionLength); | 95 selectionRange = new SourceRange(selectionOffset, selectionLength); |
| 96 utils = new CorrectionUtils(unit); | 96 utils = new CorrectionUtils(unit); |
| 97 } | 97 } |
| 98 | 98 |
| 99 bool get canCreateGetter { | 99 bool get canCreateGetter { |
| 100 if (!parameters.isEmpty) { | 100 if (!parameters.isEmpty) { |
| 101 return false; | 101 return false; |
| 102 } | 102 } |
| 103 if (_selectionExpression != null) { | 103 if (_selectionExpression != null) { |
| 104 if (_selectionExpression is AssignmentExpression) { | 104 if (_selectionExpression is AssignmentExpression) { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 sb.write(')'); | 269 sb.write(')'); |
| 270 } | 270 } |
| 271 invocationSource = sb.toString(); | 271 invocationSource = sb.toString(); |
| 272 // statements as extracted with their ";", so add new after invocation | 272 // statements as extracted with their ";", so add new after invocation |
| 273 if (_selectionStatements != null) { | 273 if (_selectionStatements != null) { |
| 274 invocationSource += ';'; | 274 invocationSource += ';'; |
| 275 } | 275 } |
| 276 } | 276 } |
| 277 // add replace edit | 277 // add replace edit |
| 278 SourceEdit edit = new SourceEdit.range(range, invocationSource); | 278 SourceEdit edit = new SourceEdit.range(range, invocationSource); |
| 279 change.addEdit(file, edit); | 279 addElementSourceChange(change, unitElement, edit); |
| 280 } | 280 } |
| 281 // add method declaration | 281 // add method declaration |
| 282 { | 282 { |
| 283 // prepare environment | 283 // prepare environment |
| 284 String prefix = utils.getNodePrefix(_parentMember); | 284 String prefix = utils.getNodePrefix(_parentMember); |
| 285 String eol = utils.endOfLine; | 285 String eol = utils.endOfLine; |
| 286 // prepare annotations | 286 // prepare annotations |
| 287 String annotations = ''; | 287 String annotations = ''; |
| 288 { | 288 { |
| 289 // may be "static" | 289 // may be "static" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 '${prefix} return ${_returnVariableName};$eol'; | 326 '${prefix} return ${_returnVariableName};$eol'; |
| 327 } | 327 } |
| 328 declarationSource += '${prefix}}'; | 328 declarationSource += '${prefix}}'; |
| 329 } | 329 } |
| 330 } | 330 } |
| 331 // insert declaration | 331 // insert declaration |
| 332 if (declarationSource != null) { | 332 if (declarationSource != null) { |
| 333 int offset = _parentMember.end; | 333 int offset = _parentMember.end; |
| 334 SourceEdit edit = | 334 SourceEdit edit = |
| 335 new SourceEdit(offset, 0, '${eol}${eol}${prefix}${declarationSource}
'); | 335 new SourceEdit(offset, 0, '${eol}${eol}${prefix}${declarationSource}
'); |
| 336 change.addEdit(file, edit); | 336 addElementSourceChange(change, unitElement, edit); |
| 337 } | 337 } |
| 338 } | 338 } |
| 339 // done | 339 // done |
| 340 return new Future.value(change); | 340 return new Future.value(change); |
| 341 } | 341 } |
| 342 | 342 |
| 343 @override | 343 @override |
| 344 bool requiresPreview() => false; | 344 bool requiresPreview() => false; |
| 345 | 345 |
| 346 /** | 346 /** |
| (...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1098 | 1098 |
| 1099 /** | 1099 /** |
| 1100 * Generalized version of some source, in which references to the specific | 1100 * Generalized version of some source, in which references to the specific |
| 1101 * variables are replaced with pattern variables, with back mapping from the | 1101 * variables are replaced with pattern variables, with back mapping from the |
| 1102 * pattern to the original variable names. | 1102 * pattern to the original variable names. |
| 1103 */ | 1103 */ |
| 1104 class _SourcePattern { | 1104 class _SourcePattern { |
| 1105 String patternSource; | 1105 String patternSource; |
| 1106 Map<String, String> originalToPatternNames = {}; | 1106 Map<String, String> originalToPatternNames = {}; |
| 1107 } | 1107 } |
| OLD | NEW |