Chromium Code Reviews| 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.rename_unit_member; | 5 library services.src.refactoring.rename_unit_member; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol_server.dart' | 9 import 'package:analysis_server/src/protocol_server.dart' |
| 10 show newLocation_fromElement, newLocation_fromMatch; | 10 show newLocation_fromElement, newLocation_fromMatch; |
| 11 import 'package:analysis_server/src/services/correction/status.dart'; | 11 import 'package:analysis_server/src/services/correction/status.dart'; |
| 12 import 'package:analysis_server/src/services/correction/util.dart'; | 12 import 'package:analysis_server/src/services/correction/util.dart'; |
| 13 import 'package:analysis_server/src/services/refactoring/naming_conventions.dart '; | 13 import 'package:analysis_server/src/services/refactoring/naming_conventions.dart '; |
| 14 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 14 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 15 import 'package:analysis_server/src/services/refactoring/rename.dart'; | 15 import 'package:analysis_server/src/services/refactoring/rename.dart'; |
| 16 import 'package:analysis_server/src/services/search/element_visitors.dart'; | 16 import 'package:analysis_server/src/services/search/element_visitors.dart'; |
| 17 import 'package:analysis_server/src/services/search/search_engine.dart'; | 17 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 18 import 'package:analyzer/src/generated/ast.dart' show Identifier; | |
| 18 import 'package:analyzer/src/generated/element.dart'; | 19 import 'package:analyzer/src/generated/element.dart'; |
| 19 import 'package:analyzer/src/generated/java_core.dart'; | 20 import 'package:analyzer/src/generated/java_core.dart'; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * Checks if creating a top-level function with the given [name] in [library] | 23 * Checks if creating a top-level function with the given [name] in [library] |
| 23 * will cause any conflicts. | 24 * will cause any conflicts. |
| 24 */ | 25 */ |
| 25 Future<RefactoringStatus> validateCreateFunction( | 26 Future<RefactoringStatus> validateCreateFunction( |
| 26 SearchEngine searchEngine, LibraryElement library, String name) { | 27 SearchEngine searchEngine, LibraryElement library, String name) { |
| 27 return new _RenameUnitMemberValidator.forCreate( | 28 return new _RenameUnitMemberValidator.forCreate( |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 | 121 |
| 121 final RefactoringStatus result = new RefactoringStatus(); | 122 final RefactoringStatus result = new RefactoringStatus(); |
| 122 | 123 |
| 123 _RenameUnitMemberValidator.forCreate( | 124 _RenameUnitMemberValidator.forCreate( |
| 124 this.searchEngine, this.library, this.elementKind, this.name) | 125 this.searchEngine, this.library, this.elementKind, this.name) |
| 125 : isRename = false; | 126 : isRename = false; |
| 126 | 127 |
| 127 _RenameUnitMemberValidator.forRename( | 128 _RenameUnitMemberValidator.forRename( |
| 128 this.searchEngine, this.element, this.name) | 129 this.searchEngine, this.element, this.name) |
| 129 : isRename = true { | 130 : isRename = true { |
| 130 library = element.getAncestor((e) => e is LibraryElement); | 131 library = element.library; |
| 131 elementKind = element.kind; | 132 elementKind = element.kind; |
| 132 } | 133 } |
| 133 | 134 |
| 134 Future<RefactoringStatus> validate() async { | 135 Future<RefactoringStatus> validate() async { |
| 135 _validateWillConflict(); | 136 _validateWillConflict(); |
| 136 if (isRename) { | 137 if (isRename) { |
| 138 await _validateWillBeInvisible(); | |
| 137 await _validateWillBeShadowed(); | 139 await _validateWillBeShadowed(); |
| 138 } | 140 } |
| 139 await _validateWillShadow(); | 141 await _validateWillShadow(); |
| 140 return result; | 142 return result; |
| 141 } | 143 } |
| 142 | 144 |
| 143 /** | 145 /** |
| 144 * Returns `true` if [element] is visible at the given [SearchMatch]. | 146 * Returns `true` if [element] is visible at the given [SearchMatch]. |
| 145 */ | 147 */ |
| 146 bool _isVisibleAt(Element element, SearchMatch at) { | 148 bool _isVisibleAt(Element element, SearchMatch at) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 158 // check imported elements | 160 // check imported elements |
| 159 if (getImportNamespace(importElement).containsValue(element)) { | 161 if (getImportNamespace(importElement).containsValue(element)) { |
| 160 return true; | 162 return true; |
| 161 } | 163 } |
| 162 } | 164 } |
| 163 // no, it is not visible | 165 // no, it is not visible |
| 164 return false; | 166 return false; |
| 165 } | 167 } |
| 166 | 168 |
| 167 /** | 169 /** |
| 170 * Validates if any usage of [element] renamed to [name] will be invisible. | |
| 171 */ | |
| 172 Future _validateWillBeInvisible() async { | |
|
Brian Wilkerson
2015/06/01 17:32:29
It's sad to see this method duplicated. Could we m
| |
| 173 if (!Identifier.isPrivateName(name)) { | |
| 174 return; | |
| 175 } | |
| 176 List<SearchMatch> references = await searchEngine.searchReferences(element); | |
| 177 for (SearchMatch reference in references) { | |
| 178 Element refElement = reference.element; | |
| 179 LibraryElement refLibrary = refElement.library; | |
| 180 if (refLibrary != library) { | |
| 181 String message = format("Renamed {0} will be invisible in '{1}'.", | |
| 182 getElementKindName(element), getElementQualifiedName(refLibrary)); | |
| 183 result.addError(message, newLocation_fromMatch(reference)); | |
| 184 } | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 /** | |
| 168 * Validates if any usage of [element] renamed to [name] will be shadowed. | 189 * Validates if any usage of [element] renamed to [name] will be shadowed. |
| 169 */ | 190 */ |
| 170 Future _validateWillBeShadowed() async { | 191 Future _validateWillBeShadowed() async { |
| 171 if (!isRename) { | |
| 172 return; | |
| 173 } | |
| 174 List<SearchMatch> references = await searchEngine.searchReferences(element); | 192 List<SearchMatch> references = await searchEngine.searchReferences(element); |
| 175 for (SearchMatch reference in references) { | 193 for (SearchMatch reference in references) { |
| 176 Element refElement = reference.element; | 194 Element refElement = reference.element; |
| 177 ClassElement refClass = refElement.getAncestor((e) => e is ClassElement); | 195 ClassElement refClass = refElement.getAncestor((e) => e is ClassElement); |
| 178 if (refClass != null) { | 196 if (refClass != null) { |
| 179 visitChildren(refClass, (shadow) { | 197 visitChildren(refClass, (shadow) { |
| 180 if (hasDisplayName(shadow, name)) { | 198 if (hasDisplayName(shadow, name)) { |
| 181 String message = format( | 199 String message = format( |
| 182 "Reference to renamed {0} will be shadowed by {1} '{2}'.", | 200 "Reference to renamed {0} will be shadowed by {1} '{2}'.", |
| 183 getElementKindName(element), getElementKindName(shadow), | 201 getElementKindName(element), getElementKindName(shadow), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 // OK, reference will be shadowed be the element being renamed | 251 // OK, reference will be shadowed be the element being renamed |
| 234 String message = format(isRename | 252 String message = format(isRename |
| 235 ? "Renamed {0} will shadow {1} '{2}'." | 253 ? "Renamed {0} will shadow {1} '{2}'." |
| 236 : "Created {0} will shadow {1} '{2}'.", elementKind.displayName, | 254 : "Created {0} will shadow {1} '{2}'.", elementKind.displayName, |
| 237 getElementKindName(member), getElementQualifiedName(member)); | 255 getElementKindName(member), getElementQualifiedName(member)); |
| 238 result.addError(message, newLocation_fromMatch(memberReference)); | 256 result.addError(message, newLocation_fromMatch(memberReference)); |
| 239 } | 257 } |
| 240 } | 258 } |
| 241 } | 259 } |
| 242 } | 260 } |
| OLD | NEW |