| 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; | 5 library services.src.refactoring.rename; |
| 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'; |
| 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/refactoring.dart'; | 13 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 14 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; | 14 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; |
| 15 import 'package:analysis_server/src/services/search/search_engine.dart'; | 15 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 16 import 'package:analyzer/src/generated/element.dart'; | 16 import 'package:analyzer/src/generated/element.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; | 17 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/java_core.dart'; | 18 import 'package:analyzer/src/generated/java_core.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
| 20 import 'package:path/path.dart' as pathos; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * Returns `true` if two given [Element]s are [LocalElement]s and have | 23 * Returns `true` if two given [Element]s are [LocalElement]s and have |
| 23 * intersecting with visibility ranges. | 24 * intersecting with visibility ranges. |
| 24 */ | 25 */ |
| 25 bool haveIntersectingRanges(LocalElement localElement, Element element) { | 26 bool haveIntersectingRanges(LocalElement localElement, Element element) { |
| 26 if (element is! LocalElement) { | 27 if (element is! LocalElement) { |
| 27 return false; | 28 return false; |
| 28 } | 29 } |
| 29 LocalElement localElement2 = element as LocalElement; | 30 LocalElement localElement2 = element as LocalElement; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 45 // should be the same AnalysisContext | 46 // should be the same AnalysisContext |
| 46 if (!isInContext(element, context)) { | 47 if (!isInContext(element, context)) { |
| 47 return false; | 48 return false; |
| 48 } | 49 } |
| 49 // private elements are visible only in their library | 50 // private elements are visible only in their library |
| 50 List<Source> librarySourcesOfSource = context.getLibrariesContaining(source); | 51 List<Source> librarySourcesOfSource = context.getLibrariesContaining(source); |
| 51 Source librarySourceOfElement = element.library.source; | 52 Source librarySourceOfElement = element.library.source; |
| 52 return librarySourcesOfSource.contains(librarySourceOfElement); | 53 return librarySourcesOfSource.contains(librarySourceOfElement); |
| 53 } | 54 } |
| 54 | 55 |
| 56 bool isElementInPubCache(Element element) { |
| 57 Source source = element.source; |
| 58 String path = source.fullName; |
| 59 return isPathInPubCache(path); |
| 60 } |
| 61 |
| 62 bool isElementInSdkOrPubCache(Element element) { |
| 63 Source source = element.source; |
| 64 String path = source.fullName; |
| 65 return source.isInSystemLibrary || isPathInPubCache(path); |
| 66 } |
| 67 |
| 55 /** | 68 /** |
| 56 * Checks if the given [Element] is in the given [AnalysisContext]. | 69 * Checks if the given [Element] is in the given [AnalysisContext]. |
| 57 */ | 70 */ |
| 58 bool isInContext(Element element, AnalysisContext context) { | 71 bool isInContext(Element element, AnalysisContext context) { |
| 59 return element.context == context; | 72 return element.context == context; |
| 60 } | 73 } |
| 61 | 74 |
| 75 bool isPathInPubCache(String path) { |
| 76 List<String> parts = pathos.split(path); |
| 77 if (parts.contains('.pub-cache')) { |
| 78 return true; |
| 79 } |
| 80 for (int i = 0; i < parts.length - 1; i++) { |
| 81 if (parts[i] == 'Pub' && parts[i + 1] == 'Cache') { |
| 82 return true; |
| 83 } |
| 84 if (parts[i] == 'third_party' && |
| 85 (parts[i + 1] == 'pkg' || parts[i + 1] == 'pkg_tested')) { |
| 86 return true; |
| 87 } |
| 88 } |
| 89 return false; |
| 90 } |
| 91 |
| 62 /** | 92 /** |
| 63 * Checks if the given unqualified [SearchMatch] intersects with visibility | 93 * Checks if the given unqualified [SearchMatch] intersects with visibility |
| 64 * range of [localElement]. | 94 * range of [localElement]. |
| 65 */ | 95 */ |
| 66 bool isReferenceInLocalRange(LocalElement localElement, SearchMatch reference) { | 96 bool isReferenceInLocalRange(LocalElement localElement, SearchMatch reference) { |
| 67 if (reference.isQualified) { | 97 if (reference.isQualified) { |
| 68 return false; | 98 return false; |
| 69 } | 99 } |
| 70 Source localSource = localElement.source; | 100 Source localSource = localElement.source; |
| 71 Source referenceSource = reference.element.source; | 101 Source referenceSource = reference.element.source; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 @override | 168 @override |
| 139 Future<RefactoringStatus> checkInitialConditions() { | 169 Future<RefactoringStatus> checkInitialConditions() { |
| 140 RefactoringStatus result = new RefactoringStatus(); | 170 RefactoringStatus result = new RefactoringStatus(); |
| 141 if (element.source.isInSystemLibrary) { | 171 if (element.source.isInSystemLibrary) { |
| 142 String message = format( | 172 String message = format( |
| 143 "The {0} '{1}' is defined in the SDK, so cannot be renamed.", | 173 "The {0} '{1}' is defined in the SDK, so cannot be renamed.", |
| 144 getElementKindName(element), | 174 getElementKindName(element), |
| 145 getElementQualifiedName(element)); | 175 getElementQualifiedName(element)); |
| 146 result.addFatalError(message); | 176 result.addFatalError(message); |
| 147 } | 177 } |
| 178 if (isElementInPubCache(element)) { |
| 179 String message = format( |
| 180 "The {0} '{1}' is defined in a pub package, so cannot be renamed.", |
| 181 getElementKindName(element), |
| 182 getElementQualifiedName(element)); |
| 183 result.addFatalError(message); |
| 184 } |
| 148 return new Future.value(result); | 185 return new Future.value(result); |
| 149 } | 186 } |
| 150 | 187 |
| 151 @override | 188 @override |
| 152 RefactoringStatus checkNewName() { | 189 RefactoringStatus checkNewName() { |
| 153 RefactoringStatus result = new RefactoringStatus(); | 190 RefactoringStatus result = new RefactoringStatus(); |
| 154 if (newName == oldName) { | 191 if (newName == oldName) { |
| 155 result.addFatalError( | 192 result.addFatalError( |
| 156 "The new name must be different than the current name."); | 193 "The new name must be different than the current name."); |
| 157 } | 194 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 180 if (element is ImportElement) { | 217 if (element is ImportElement) { |
| 181 PrefixElement prefix = element.prefix; | 218 PrefixElement prefix = element.prefix; |
| 182 if (prefix != null) { | 219 if (prefix != null) { |
| 183 return prefix.displayName; | 220 return prefix.displayName; |
| 184 } | 221 } |
| 185 return ''; | 222 return ''; |
| 186 } | 223 } |
| 187 return element.displayName; | 224 return element.displayName; |
| 188 } | 225 } |
| 189 } | 226 } |
| OLD | NEW |