Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(490)

Side by Side Diff: pkg/analysis_services/lib/src/refactoring/rename.dart

Issue 458063002: Initial work on refactorings. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes for review comments Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information.
7
8 library services.src.refactoring.rename;
9
10 import 'package:analysis_services/correction/status.dart';
11 import 'package:analysis_services/refactoring/refactoring.dart';
12 import 'package:analysis_services/search/search_engine.dart';
13 import 'package:analysis_services/src/correction/source_range.dart';
14 import 'package:analysis_services/src/generated/change.dart';
15 import 'package:analysis_services/src/refactoring/refactoring.dart';
16 import 'package:analyzer/src/generated/element.dart';
17 import 'package:analyzer/src/generated/engine.dart';
18 import 'package:analyzer/src/generated/source.dart';
19
20
21 /**
22 * Returns the [Edit] to replace the given [SearchMatch] reference.
23 */
24 Edit createReferenceEdit(SourceReference reference, String newText) {
25 return new Edit.range(reference.range, newText);
26 }
27
28
29 /**
30 * Returns `true` if two given [Element]s are [LocalElement]s and have
31 * intersecting with visibility ranges.
32 */
33 bool haveIntersectingRanges(LocalElement localElement, Element element) {
34 if (element is! LocalElement) {
35 return false;
36 }
37 LocalElement localElement2 = element as LocalElement;
38 Source localSource = localElement.source;
39 Source localSource2 = localElement2.source;
40 SourceRange localRange = localElement.visibleRange;
41 SourceRange localRange2 = localElement2.visibleRange;
42 return localSource2 == localSource &&
43 localRange != null &&
44 localRange2 != null &&
45 localRange2.intersects(localRange);
46 }
47
48
49 /**
50 * Checks if the given [Element] is in the given [AnalysisContext].
51 */
52 bool isInContext(Element element, AnalysisContext context) {
53 AnalysisContext elementContext = element.context;
54 if (elementContext == context) {
55 return true;
56 }
57 if (context is InstrumentedAnalysisContextImpl) {
58 return elementContext == context.basis;
59 }
60 return false;
61 }
62
63
64 /**
65 * Checks if [element] is defined in the library containing [source].
66 */
67 bool isDefinedInLibrary(Element element, AnalysisContext context, Source source)
68 {
69 // should be the same AnalysisContext
70 if (!isInContext(element, context)) {
71 return false;
72 }
73 // private elements are visible only in their library
74 List<Source> librarySourcesOfSource = context.getLibrariesContaining(source);
75 Source librarySourceOfElement = element.library.source;
76 return librarySourcesOfSource.contains(librarySourceOfElement);
77 }
78
79
80 /**
81 * Checks if [element] is visible in the library containing [source].
82 */
83 bool isVisibleInLibrary(Element element, AnalysisContext context, Source source)
84 {
85 // should be the same AnalysisContext
86 if (!isInContext(element, context)) {
87 return false;
88 }
89 // public elements are always visible
90 if (element.isPublic) {
91 return true;
92 }
93 // private elements are visible only in their library
94 return isDefinedInLibrary(element, context, source);
95 }
96
97
98 /**
99 * Checks if the given unqualified [SearchMatch] intersects with visibility
100 * range of [localElement].
101 */
102 bool isReferenceInLocalRange(LocalElement localElement, SearchMatch reference) {
103 if (reference.isQualified) {
104 return false;
105 }
106 Source localSource = localElement.source;
107 Source referenceSource = reference.element.source;
108 SourceRange localRange = localElement.visibleRange;
109 SourceRange referenceRange = reference.sourceRange;
110 return referenceSource == localSource &&
111 referenceRange.intersects(localRange);
112 }
113
114
115 /**
116 * An abstract implementation of [RenameRefactoring].
117 */
118 abstract class RenameRefactoringImpl extends RefactoringImpl implements
119 RenameRefactoring {
120 final SearchEngine searchEngine;
121 final Element element;
122 final AnalysisContext context;
123 final String oldName;
124
125 String newName;
126
127 RenameRefactoringImpl(SearchEngine searchEngine, Element element)
128 : searchEngine = searchEngine,
129 element = element,
130 context = element.context,
131 oldName = _getDisplayName(element);
132
133 /**
134 * Adds the "Update declaration" [Edit] to [sourceChange].
135 */
136 void addDeclarationEdit(SourceChange sourceChange, Element element) {
137 Edit edit = new Edit.range(rangeElementName(element), newName);
138 sourceChange.addEdit(edit, "Update declaration");
139 }
140
141 /**
142 * Adds an "Update reference" [Edit] to [sourceChange].
143 */
144 void addReferenceEdit(SourceChange sourceChange, SourceReference reference) {
145 Edit edit = createReferenceEdit(reference, newName);
146 sourceChange.addEdit(edit, "Update reference");
147 }
148
149 @override
150 RefactoringStatus checkInitialConditions() {
151 return new RefactoringStatus();
152 }
153
154 @override
155 RefactoringStatus checkNewName() {
156 RefactoringStatus result = new RefactoringStatus();
157 if (newName == oldName) {
158 result.addFatalError(
159 "The new name must be different than the current name.");
160 }
161 return result;
162 }
163
164 static String _getDisplayName(Element element) {
165 if (element is ImportElement) {
166 PrefixElement prefix = element.prefix;
167 if (prefix != null) {
168 return prefix.displayName;
169 }
170 }
171 return element.displayName;
172 }
173 }
174
175
176 /**
177 * The [SourceRange] in some [Source].
178 */
179 class SourceReference {
180 final MatchKind kind;
181 final Source source;
182 final SourceRange range;
183
184 SourceReference(this.kind, this.source, this.range);
185
186 @override
187 int get hashCode {
188 int hash = source.hashCode;
189 hash = ((hash << 16) & 0xFFFFFFFF) + range.hashCode;
190 return hash;
191 }
192
193 @override
194 bool operator ==(Object obj) {
195 if (identical(obj, this)) {
196 return true;
197 }
198 if (obj is! SourceReference) {
199 return false;
200 }
201 SourceReference other = obj as SourceReference;
202 return other.source == source && other.range == range;
203 }
204
205 @override
206 String toString() => '${source}@${range}';
207 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698