| 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.inline_local; | 5 library services.src.refactoring.inline_local; |
| 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_server.dart' hide Element; |
| 10 import 'package:analysis_server/src/services/correction/status.dart'; | 10 import 'package:analysis_server/src/services/correction/status.dart'; |
| 11 import 'package:analysis_server/src/services/correction/util.dart'; | 11 import 'package:analysis_server/src/services/correction/util.dart'; |
| 12 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 12 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 13 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; | 13 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; |
| 14 import 'package:analysis_server/src/services/search/search_engine.dart'; | 14 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 15 import 'package:analyzer/src/generated/ast.dart'; | 15 import 'package:analyzer/src/generated/ast.dart'; |
| 16 import 'package:analyzer/src/generated/element.dart'; | 16 import 'package:analyzer/src/generated/element.dart'; |
| 17 import 'package:analyzer/src/generated/java_core.dart'; | 17 import 'package:analyzer/src/generated/java_core.dart'; |
| 18 import 'package:analyzer/src/generated/scanner.dart'; | 18 import 'package:analyzer/src/generated/scanner.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 'Local variable declared in ' | 91 'Local variable declared in ' |
| 92 'statement should be selected to activate this refactoring.'); | 92 'statement should be selected to activate this refactoring.'); |
| 93 return new Future.value(result); | 93 return new Future.value(result); |
| 94 } | 94 } |
| 95 // should have initializer at declaration | 95 // should have initializer at declaration |
| 96 if (_variableNode.initializer == null) { | 96 if (_variableNode.initializer == null) { |
| 97 String message = format( | 97 String message = format( |
| 98 "Local variable '{0}' is not initialized at declaration.", | 98 "Local variable '{0}' is not initialized at declaration.", |
| 99 _variableElement.displayName); | 99 _variableElement.displayName); |
| 100 result = | 100 result = |
| 101 new RefactoringStatus.fatal(message, new Location.fromNode(_variableNo
de)); | 101 new RefactoringStatus.fatal(message, newLocation_fromNode(_variableNod
e)); |
| 102 return new Future.value(result); | 102 return new Future.value(result); |
| 103 } | 103 } |
| 104 // prepare references | 104 // prepare references |
| 105 return searchEngine.searchReferences(_variableElement).then((references) { | 105 return searchEngine.searchReferences(_variableElement).then((references) { |
| 106 this._references = references; | 106 this._references = references; |
| 107 // should not have assignments | 107 // should not have assignments |
| 108 for (SearchMatch reference in _references) { | 108 for (SearchMatch reference in _references) { |
| 109 if (reference.kind != MatchKind.READ) { | 109 if (reference.kind != MatchKind.READ) { |
| 110 String message = format( | 110 String message = format( |
| 111 "Local variable '{0}' is assigned more than once.", | 111 "Local variable '{0}' is assigned more than once.", |
| 112 [_variableElement.displayName]); | 112 [_variableElement.displayName]); |
| 113 return new RefactoringStatus.fatal( | 113 return new RefactoringStatus.fatal( |
| 114 message, | 114 message, |
| 115 new Location.fromMatch(reference)); | 115 newLocation_fromMatch(reference)); |
| 116 } | 116 } |
| 117 } | 117 } |
| 118 // done | 118 // done |
| 119 return result; | 119 return result; |
| 120 }); | 120 }); |
| 121 } | 121 } |
| 122 | 122 |
| 123 @override | 123 @override |
| 124 Future<SourceChange> createChange() { | 124 Future<SourceChange> createChange() { |
| 125 SourceChange change = new SourceChange(refactoringName); | 125 SourceChange change = new SourceChange(refactoringName); |
| 126 // remove declaration | 126 // remove declaration |
| 127 { | 127 { |
| 128 Statement declarationStatement = | 128 Statement declarationStatement = |
| 129 _variableNode.getAncestor((node) => node is VariableDeclarationStateme
nt); | 129 _variableNode.getAncestor((node) => node is VariableDeclarationStateme
nt); |
| 130 SourceRange range = utils.getLinesRangeStatements([declarationStatement]); | 130 SourceRange range = utils.getLinesRangeStatements([declarationStatement]); |
| 131 change.addElementEdit(unitElement, new SourceEdit.range(range, '')); | 131 doSourceChange_addElementEdit( |
| 132 change, |
| 133 unitElement, |
| 134 newSourceEdit_range(range, '')); |
| 132 } | 135 } |
| 133 // prepare initializer | 136 // prepare initializer |
| 134 Expression initializer = _variableNode.initializer; | 137 Expression initializer = _variableNode.initializer; |
| 135 String initializerSource = utils.getNodeText(initializer); | 138 String initializerSource = utils.getNodeText(initializer); |
| 136 int initializerPrecedence = getExpressionPrecedence(initializer); | 139 int initializerPrecedence = getExpressionPrecedence(initializer); |
| 137 // replace references | 140 // replace references |
| 138 for (SearchMatch reference in _references) { | 141 for (SearchMatch reference in _references) { |
| 139 SourceRange range = reference.sourceRange; | 142 SourceRange range = reference.sourceRange; |
| 140 String sourceForReference = | 143 String sourceForReference = |
| 141 _getSourceForReference(range, initializerSource, initializerPrecedence
); | 144 _getSourceForReference(range, initializerSource, initializerPrecedence
); |
| 142 change.addElementEdit( | 145 doSourceChange_addElementEdit( |
| 146 change, |
| 143 unitElement, | 147 unitElement, |
| 144 new SourceEdit.range(range, sourceForReference)); | 148 newSourceEdit_range(range, sourceForReference)); |
| 145 } | 149 } |
| 146 // done | 150 // done |
| 147 return new Future.value(change); | 151 return new Future.value(change); |
| 148 } | 152 } |
| 149 | 153 |
| 150 @override | 154 @override |
| 151 bool requiresPreview() => false; | 155 bool requiresPreview() => false; |
| 152 | 156 |
| 153 /** | 157 /** |
| 154 * Returns the source which should be used to replace the reference with the | 158 * Returns the source which should be used to replace the reference with the |
| (...skipping 22 matching lines...) Expand all Loading... |
| 177 */ | 181 */ |
| 178 bool _isIdentifierStringInterpolation(AstNode parent) { | 182 bool _isIdentifierStringInterpolation(AstNode parent) { |
| 179 if (parent is InterpolationExpression) { | 183 if (parent is InterpolationExpression) { |
| 180 InterpolationExpression element = parent; | 184 InterpolationExpression element = parent; |
| 181 return element.beginToken.type == | 185 return element.beginToken.type == |
| 182 TokenType.STRING_INTERPOLATION_IDENTIFIER; | 186 TokenType.STRING_INTERPOLATION_IDENTIFIER; |
| 183 } | 187 } |
| 184 return false; | 188 return false; |
| 185 } | 189 } |
| 186 } | 190 } |
| OLD | NEW |