| 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.dart' hide Element; |
| 10 import 'package:analysis_server/src/services/correction/status.dart'; | 10 import 'package:analysis_server/src/services/correction/status.dart'; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * [InlineLocalRefactoring] implementation. | 26 * [InlineLocalRefactoring] implementation. |
| 27 */ | 27 */ |
| 28 class InlineLocalRefactoringImpl extends RefactoringImpl implements | 28 class InlineLocalRefactoringImpl extends RefactoringImpl implements |
| 29 InlineLocalRefactoring { | 29 InlineLocalRefactoring { |
| 30 final SearchEngine searchEngine; | 30 final SearchEngine searchEngine; |
| 31 final CompilationUnit unit; | 31 final CompilationUnit unit; |
| 32 final int offset; | 32 final int offset; |
| 33 String file; | 33 CompilationUnitElement unitElement; |
| 34 CorrectionUtils utils; | 34 CorrectionUtils utils; |
| 35 | 35 |
| 36 Element _variableElement; | 36 Element _variableElement; |
| 37 VariableDeclaration _variableNode; | 37 VariableDeclaration _variableNode; |
| 38 List<SearchMatch> _references; | 38 List<SearchMatch> _references; |
| 39 | 39 |
| 40 InlineLocalRefactoringImpl(this.searchEngine, this.unit, this.offset) { | 40 InlineLocalRefactoringImpl(this.searchEngine, this.unit, this.offset) { |
| 41 file = unit.element.source.fullName; | 41 unitElement = unit.element; |
| 42 utils = new CorrectionUtils(unit); | 42 utils = new CorrectionUtils(unit); |
| 43 } | 43 } |
| 44 | 44 |
| 45 @override | 45 @override |
| 46 String get refactoringName => 'Inline Local Variable'; | 46 String get refactoringName => 'Inline Local Variable'; |
| 47 | 47 |
| 48 @override | 48 @override |
| 49 int get referenceCount { | 49 int get referenceCount { |
| 50 if (_references == null) { | 50 if (_references == null) { |
| 51 return 0; | 51 return 0; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 } | 124 } |
| 125 | 125 |
| 126 @override | 126 @override |
| 127 Future<SourceChange> createChange() { | 127 Future<SourceChange> createChange() { |
| 128 SourceChange change = new SourceChange(refactoringName); | 128 SourceChange change = new SourceChange(refactoringName); |
| 129 // remove declaration | 129 // remove declaration |
| 130 { | 130 { |
| 131 Statement declarationStatement = | 131 Statement declarationStatement = |
| 132 _variableNode.getAncestor((node) => node is VariableDeclarationStateme
nt); | 132 _variableNode.getAncestor((node) => node is VariableDeclarationStateme
nt); |
| 133 SourceRange range = utils.getLinesRangeStatements([declarationStatement]); | 133 SourceRange range = utils.getLinesRangeStatements([declarationStatement]); |
| 134 change.addEdit(file, new SourceEdit.range(range, '')); | 134 addElementSourceChange( |
| 135 change, |
| 136 unitElement, |
| 137 new SourceEdit.range(range, '')); |
| 135 } | 138 } |
| 136 // prepare initializer | 139 // prepare initializer |
| 137 Expression initializer = _variableNode.initializer; | 140 Expression initializer = _variableNode.initializer; |
| 138 String initializerSource = utils.getNodeText(initializer); | 141 String initializerSource = utils.getNodeText(initializer); |
| 139 int initializerPrecedence = getExpressionPrecedence(initializer); | 142 int initializerPrecedence = getExpressionPrecedence(initializer); |
| 140 // replace references | 143 // replace references |
| 141 for (SearchMatch reference in _references) { | 144 for (SearchMatch reference in _references) { |
| 142 SourceRange range = reference.sourceRange; | 145 SourceRange range = reference.sourceRange; |
| 143 String sourceForReference = | 146 String sourceForReference = |
| 144 _getSourceForReference(range, initializerSource, initializerPrecedence
); | 147 _getSourceForReference(range, initializerSource, initializerPrecedence
); |
| 145 change.addEdit(file, new SourceEdit.range(range, sourceForReference)); | 148 addElementSourceChange( |
| 149 change, |
| 150 unitElement, |
| 151 new SourceEdit.range(range, sourceForReference)); |
| 146 } | 152 } |
| 147 // done | 153 // done |
| 148 return new Future.value(change); | 154 return new Future.value(change); |
| 149 } | 155 } |
| 150 | 156 |
| 151 @override | 157 @override |
| 152 bool requiresPreview() => false; | 158 bool requiresPreview() => false; |
| 153 | 159 |
| 154 /** | 160 /** |
| 155 * Returns the source which should be used to replace the reference with the | 161 * Returns the source which should be used to replace the reference with the |
| (...skipping 22 matching lines...) Expand all Loading... |
| 178 */ | 184 */ |
| 179 bool _isIdentifierStringInterpolation(AstNode parent) { | 185 bool _isIdentifierStringInterpolation(AstNode parent) { |
| 180 if (parent is InterpolationExpression) { | 186 if (parent is InterpolationExpression) { |
| 181 InterpolationExpression element = parent; | 187 InterpolationExpression element = parent; |
| 182 return element.beginToken.type == | 188 return element.beginToken.type == |
| 183 TokenType.STRING_INTERPOLATION_IDENTIFIER; | 189 TokenType.STRING_INTERPOLATION_IDENTIFIER; |
| 184 } | 190 } |
| 185 return false; | 191 return false; |
| 186 } | 192 } |
| 187 } | 193 } |
| OLD | NEW |