| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol_server.dart' hide Element; | 7 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
| 8 import 'package:analysis_server/src/services/correction/status.dart'; | 8 import 'package:analysis_server/src/services/correction/status.dart'; |
| 9 import 'package:analysis_server/src/services/correction/util.dart'; | 9 import 'package:analysis_server/src/services/correction/util.dart'; |
| 10 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 10 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 11 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; | 11 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; |
| 12 import 'package:analysis_server/src/services/search/search_engine.dart'; | 12 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 13 import 'package:analyzer/dart/element/element.dart'; | 13 import 'package:analyzer/dart/element/element.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart'; | |
| 15 import 'package:analyzer/src/generated/java_core.dart'; | 14 import 'package:analyzer/src/generated/java_core.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 15 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer_plugin/utilities/range_factory.dart'; | 16 import 'package:analyzer_plugin/utilities/range_factory.dart'; |
| 18 import 'package:path/path.dart' as pathos; | 17 import 'package:path/path.dart' as pathos; |
| 19 | 18 |
| 20 /** | |
| 21 * Checks if [element] is defined in the library containing [source]. | |
| 22 */ | |
| 23 bool isDefinedInLibrary( | |
| 24 Element element, AnalysisContext context, Source source) { | |
| 25 // should be the same AnalysisContext | |
| 26 if (!isInContext(element, context)) { | |
| 27 return false; | |
| 28 } | |
| 29 // private elements are visible only in their library | |
| 30 List<Source> librarySourcesOfSource = context.getLibrariesContaining(source); | |
| 31 Source librarySourceOfElement = element.library.source; | |
| 32 return librarySourcesOfSource.contains(librarySourceOfElement); | |
| 33 } | |
| 34 | |
| 35 bool isElementInPubCache(Element element) { | 19 bool isElementInPubCache(Element element) { |
| 36 Source source = element.source; | 20 Source source = element.source; |
| 37 String path = source.fullName; | 21 String path = source.fullName; |
| 38 return isPathInPubCache(path); | 22 return isPathInPubCache(path); |
| 39 } | 23 } |
| 40 | 24 |
| 41 bool isElementInSdkOrPubCache(Element element) { | 25 bool isElementInSdkOrPubCache(Element element) { |
| 42 Source source = element.source; | 26 Source source = element.source; |
| 43 String path = source.fullName; | 27 String path = source.fullName; |
| 44 return source.isInSystemLibrary || isPathInPubCache(path); | 28 return source.isInSystemLibrary || isPathInPubCache(path); |
| 45 } | 29 } |
| 46 | 30 |
| 47 /** | |
| 48 * Checks if the given [Element] is in the given [AnalysisContext]. | |
| 49 */ | |
| 50 bool isInContext(Element element, AnalysisContext context) { | |
| 51 return element.context == context; | |
| 52 } | |
| 53 | |
| 54 bool isPathInPubCache(String path) { | 31 bool isPathInPubCache(String path) { |
| 55 List<String> parts = pathos.split(path); | 32 List<String> parts = pathos.split(path); |
| 56 if (parts.contains('.pub-cache')) { | 33 if (parts.contains('.pub-cache')) { |
| 57 return true; | 34 return true; |
| 58 } | 35 } |
| 59 for (int i = 0; i < parts.length - 1; i++) { | 36 for (int i = 0; i < parts.length - 1; i++) { |
| 60 if (parts[i] == 'Pub' && parts[i + 1] == 'Cache') { | 37 if (parts[i] == 'Pub' && parts[i + 1] == 'Cache') { |
| 61 return true; | 38 return true; |
| 62 } | 39 } |
| 63 if (parts[i] == 'third_party' && | 40 if (parts[i] == 'third_party' && |
| 64 (parts[i + 1] == 'pkg' || parts[i + 1] == 'pkg_tested')) { | 41 (parts[i + 1] == 'pkg' || parts[i + 1] == 'pkg_tested')) { |
| 65 return true; | 42 return true; |
| 66 } | 43 } |
| 67 } | 44 } |
| 68 return false; | 45 return false; |
| 69 } | 46 } |
| 70 | 47 |
| 71 /** | 48 /** |
| 72 * Checks if the given unqualified [SearchMatch] intersects with visibility | |
| 73 * range of [localElement]. | |
| 74 */ | |
| 75 bool isReferenceInLocalRange(LocalElement localElement, SearchMatch reference) { | |
| 76 if (reference.isQualified) { | |
| 77 return false; | |
| 78 } | |
| 79 Source localSource = localElement.source; | |
| 80 Source referenceSource = reference.unitSource; | |
| 81 SourceRange localRange = localElement.visibleRange; | |
| 82 SourceRange referenceRange = reference.sourceRange; | |
| 83 return referenceSource == localSource && | |
| 84 referenceRange.intersects(localRange); | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Checks if [element] is visible in the library containing [source]. | |
| 89 */ | |
| 90 bool isVisibleInLibrary( | |
| 91 Element element, AnalysisContext context, Source source) { | |
| 92 // should be the same AnalysisContext | |
| 93 if (!isInContext(element, context)) { | |
| 94 return false; | |
| 95 } | |
| 96 // public elements are always visible | |
| 97 if (element.isPublic) { | |
| 98 return true; | |
| 99 } | |
| 100 // private elements are visible only in their library | |
| 101 return isDefinedInLibrary(element, context, source); | |
| 102 } | |
| 103 | |
| 104 /** | |
| 105 * An abstract implementation of [RenameRefactoring]. | 49 * An abstract implementation of [RenameRefactoring]. |
| 106 */ | 50 */ |
| 107 abstract class RenameRefactoringImpl extends RefactoringImpl | 51 abstract class RenameRefactoringImpl extends RefactoringImpl |
| 108 implements RenameRefactoring { | 52 implements RenameRefactoring { |
| 109 final SearchEngine searchEngine; | 53 final SearchEngine searchEngine; |
| 110 final Element _element; | 54 final Element _element; |
| 111 final AnalysisContext context; | |
| 112 final String elementKindName; | 55 final String elementKindName; |
| 113 final String oldName; | 56 final String oldName; |
| 114 SourceChange change; | 57 SourceChange change; |
| 115 | 58 |
| 116 String newName; | 59 String newName; |
| 117 | 60 |
| 118 RenameRefactoringImpl(SearchEngine searchEngine, Element element) | 61 RenameRefactoringImpl(SearchEngine searchEngine, Element element) |
| 119 : searchEngine = searchEngine, | 62 : searchEngine = searchEngine, |
| 120 _element = element, | 63 _element = element, |
| 121 context = element.context, | |
| 122 elementKindName = element.kind.displayName, | 64 elementKindName = element.kind.displayName, |
| 123 oldName = _getDisplayName(element); | 65 oldName = _getDisplayName(element); |
| 124 | 66 |
| 125 Element get element => _element; | 67 Element get element => _element; |
| 126 | 68 |
| 127 /** | 69 /** |
| 128 * Adds a [SourceEdit] to update [element] name to [change]. | 70 * Adds a [SourceEdit] to update [element] name to [change]. |
| 129 */ | 71 */ |
| 130 void addDeclarationEdit(Element element) { | 72 void addDeclarationEdit(Element element) { |
| 131 if (element != null) { | 73 if (element != null) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 if (element is ImportElement) { | 139 if (element is ImportElement) { |
| 198 PrefixElement prefix = element.prefix; | 140 PrefixElement prefix = element.prefix; |
| 199 if (prefix != null) { | 141 if (prefix != null) { |
| 200 return prefix.displayName; | 142 return prefix.displayName; |
| 201 } | 143 } |
| 202 return ''; | 144 return ''; |
| 203 } | 145 } |
| 204 return element.displayName; | 146 return element.displayName; |
| 205 } | 147 } |
| 206 } | 148 } |
| OLD | NEW |