| 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_server.dart' hide Element; | 9 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
| 10 import 'package:analysis_server/src/services/correction/source_range.dart'; | 10 import 'package:analysis_server/src/services/correction/source_range.dart'; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 return _variableElement.name; | 59 return _variableElement.name; |
| 60 } | 60 } |
| 61 | 61 |
| 62 @override | 62 @override |
| 63 Future<RefactoringStatus> checkFinalConditions() { | 63 Future<RefactoringStatus> checkFinalConditions() { |
| 64 RefactoringStatus result = new RefactoringStatus(); | 64 RefactoringStatus result = new RefactoringStatus(); |
| 65 return new Future.value(result); | 65 return new Future.value(result); |
| 66 } | 66 } |
| 67 | 67 |
| 68 @override | 68 @override |
| 69 Future<RefactoringStatus> checkInitialConditions() { | 69 Future<RefactoringStatus> checkInitialConditions() async { |
| 70 RefactoringStatus result = new RefactoringStatus(); | 70 RefactoringStatus result = new RefactoringStatus(); |
| 71 // prepare variable | 71 // prepare variable |
| 72 { | 72 { |
| 73 AstNode offsetNode = new NodeLocator.con1(offset).searchWithin(unit); | 73 AstNode offsetNode = new NodeLocator.con1(offset).searchWithin(unit); |
| 74 if (offsetNode is SimpleIdentifier) { | 74 if (offsetNode is SimpleIdentifier) { |
| 75 Element element = offsetNode.staticElement; | 75 Element element = offsetNode.staticElement; |
| 76 if (element is LocalVariableElement) { | 76 if (element is LocalVariableElement) { |
| 77 _variableElement = element; | 77 _variableElement = element; |
| 78 _variableNode = element.node; | 78 _variableNode = element.node; |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 // validate node declaration | 82 // validate node declaration |
| 83 if (!_isVariableDeclaredInStatement()) { | 83 if (!_isVariableDeclaredInStatement()) { |
| 84 result = new RefactoringStatus.fatal( | 84 result = new RefactoringStatus.fatal( |
| 85 'Local variable declaration or reference must be selected ' | 85 'Local variable declaration or reference must be selected ' |
| 86 'to activate this refactoring.'); | 86 'to activate this refactoring.'); |
| 87 return new Future.value(result); | 87 return new Future.value(result); |
| 88 } | 88 } |
| 89 // should have initializer at declaration | 89 // should have initializer at declaration |
| 90 if (_variableNode.initializer == null) { | 90 if (_variableNode.initializer == null) { |
| 91 String message = format( | 91 String message = format( |
| 92 "Local variable '{0}' is not initialized at declaration.", | 92 "Local variable '{0}' is not initialized at declaration.", |
| 93 _variableElement.displayName); | 93 _variableElement.displayName); |
| 94 result = | 94 result = |
| 95 new RefactoringStatus.fatal(message, newLocation_fromNode(_variableNod
e)); | 95 new RefactoringStatus.fatal(message, newLocation_fromNode(_variableNod
e)); |
| 96 return new Future.value(result); | 96 return new Future.value(result); |
| 97 } | 97 } |
| 98 // prepare references | 98 // prepare references |
| 99 return searchEngine.searchReferences(_variableElement).then((references) { | 99 _references = await searchEngine.searchReferences(_variableElement); |
| 100 this._references = references; | 100 // should not have assignments |
| 101 // should not have assignments | 101 for (SearchMatch reference in _references) { |
| 102 for (SearchMatch reference in _references) { | 102 if (reference.kind != MatchKind.READ) { |
| 103 if (reference.kind != MatchKind.READ) { | 103 String message = format( |
| 104 String message = format( | 104 "Local variable '{0}' is assigned more than once.", |
| 105 "Local variable '{0}' is assigned more than once.", | 105 [_variableElement.displayName]); |
| 106 [_variableElement.displayName]); | 106 return new RefactoringStatus.fatal( |
| 107 return new RefactoringStatus.fatal( | 107 message, |
| 108 message, | 108 newLocation_fromMatch(reference)); |
| 109 newLocation_fromMatch(reference)); | |
| 110 } | |
| 111 } | 109 } |
| 112 // done | 110 } |
| 113 return result; | 111 // done |
| 114 }); | 112 return result; |
| 115 } | 113 } |
| 116 | 114 |
| 117 @override | 115 @override |
| 118 Future<SourceChange> createChange() { | 116 Future<SourceChange> createChange() { |
| 119 SourceChange change = new SourceChange(refactoringName); | 117 SourceChange change = new SourceChange(refactoringName); |
| 120 // remove declaration | 118 // remove declaration |
| 121 { | 119 { |
| 122 Statement declarationStatement = | 120 Statement declarationStatement = |
| 123 _variableNode.getAncestor((node) => node is VariableDeclarationStateme
nt); | 121 _variableNode.getAncestor((node) => node is VariableDeclarationStateme
nt); |
| 124 SourceRange range = utils.getLinesRangeStatements([declarationStatement]); | 122 SourceRange range = utils.getLinesRangeStatements([declarationStatement]); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 if (initializerOperator == TokenType.MINUS || | 215 if (initializerOperator == TokenType.MINUS || |
| 218 initializerOperator == TokenType.MINUS_MINUS) { | 216 initializerOperator == TokenType.MINUS_MINUS) { |
| 219 return true; | 217 return true; |
| 220 } | 218 } |
| 221 } | 219 } |
| 222 } | 220 } |
| 223 // no () is needed | 221 // no () is needed |
| 224 return false; | 222 return false; |
| 225 } | 223 } |
| 226 } | 224 } |
| OLD | NEW |