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

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: 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/progress_monitor.dart';
12 import 'package:analysis_services/refactoring/refactoring.dart';
13 import 'package:analysis_services/search/search_engine.dart';
14 import 'package:analysis_services/src/correction/source_range.dart';
15 import 'package:analysis_services/src/generated/change.dart';
16 import 'package:analysis_services/src/refactoring/refactoring.dart';
17 import 'package:analyzer/src/generated/element.dart';
18 import 'package:analyzer/src/generated/engine.dart';
19 import 'package:analyzer/src/generated/source.dart';
20
21
22 /**
23 * Returns the [Edit] to replace the given [SearchMatch] reference.
24 */
25 Edit createReferenceEdit(SourceReference reference, String newText) {
26 return new Edit.range(reference.range, newText);
27 }
28
29
30 /**
31 * Returns `true` if two given [Element]s are [LocalElement]s and have
32 * intersecting with visibility ranges.
33 */
34 bool haveIntersectingRanges(LocalElement localElement, Element element) {
35 if (element is! LocalElement) {
36 return false;
37 }
38 LocalElement localElement2 = element as LocalElement;
39 Source localSource = localElement.source;
40 Source localSource2 = localElement2.source;
41 SourceRange localRange = localElement.visibleRange;
42 SourceRange localRange2 = localElement2.visibleRange;
43 return localSource2 == localSource &&
44 localRange != null &&
45 localRange2 != null &&
46 localRange2.intersects(localRange);
47 }
48
49
50 /**
51 * Checks if the given [Element] is in the given [AnalysisContext].
52 */
53 bool isInContext(Element element, AnalysisContext context) {
54 AnalysisContext elementContext = element.context;
55 if (elementContext == context) {
56 return true;
57 }
58 if (context is InstrumentedAnalysisContextImpl) {
59 return elementContext == context.basis;
60 }
61 return false;
62 }
63
64
65 /**
66 * Checks if the given [Element] is visible in the given [Source].
67 */
68 bool isInTheSameLibrary(Element element, AnalysisContext context, Source source)
Brian Wilkerson 2014/08/11 16:27:47 The comment and the name don't appear to correspon
scheglov 2014/08/11 18:06:25 I've updated the comment.
69 {
70 // should be the same AnalysisContext
71 if (!isInContext(element, context)) {
72 return false;
73 }
74 // private elements are visible only in their library
75 List<Source> librarySourcesOfSource = context.getLibrariesContaining(source);
76 Source librarySourceOfElement = element.library.source;
77 return librarySourcesOfSource.contains(librarySourceOfElement);
78 }
79
80
81 /**
82 * Checks if the given [Element] is visible in the given [Source].
83 */
84 bool isPublicOrInTheSameLibrary(Element element, AnalysisContext context,
Brian Wilkerson 2014/08/11 16:27:47 This has the same comment as the previous function
scheglov 2014/08/11 18:06:25 I've updated the comment.
85 Source source) {
86 // should be the same AnalysisContext
87 if (!isInContext(element, context)) {
88 return false;
89 }
90 // public elements are always visible
91 if (element.isPublic) {
92 return true;
93 }
94 // private elements are visible only in their library
95 return isInTheSameLibrary(element, context, source);
96 }
97
98
99 /**
100 * Checks if the given unqualified [SearchMatch] intersects with visibility
101 * range of [localElement].
102 */
103 bool isReferenceInLocalRange(LocalElement localElement, SearchMatch reference) {
104 if (reference.isQualified) {
105 return false;
106 }
107 Source localSource = localElement.source;
108 Source referenceSource = reference.element.source;
109 SourceRange localRange = localElement.visibleRange;
110 SourceRange referenceRange = reference.sourceRange;
111 return referenceSource == localSource &&
112 referenceRange.intersects(localRange);
113 }
114
115
116 /**
117 * An abstract implementation of [RenameRefactoring].
118 */
119 abstract class RenameRefactoringImpl extends RefactoringImpl implements
120 RenameRefactoring {
121 final SearchEngine searchEngine;
122 final Element element;
123 AnalysisContext context;
124 String oldName;
125
126 String newName;
127
128 RenameRefactoringImpl(this.searchEngine, this.element) {
129 this.context = element.context;
130 this.oldName = _getDisplayName(element);
131 }
132
133 @override
134 String get currentName => element.displayName;
Brian Wilkerson 2014/08/11 16:27:47 This seems strange. Should this be "oldName" so th
scheglov 2014/08/11 18:06:25 The "currentName" field is gone now, "oldName" is
135
136 /**
137 * Adds the "Update declaration" [Edit] to [sourceChange].
138 */
139 void addDeclarationEdit(SourceChange sourceChange, Element element) {
140 Edit edit = new Edit.range(rangeElementName(element), newName);
141 addEdit(sourceChange, "Update declaration", edit);
142 }
143
144 /**
145 * Adds the [Edit] that replaces [oldName] in [sourceChange].
146 */
147 void addEdit(SourceChange sourceChange, String description, Edit edit) {
Brian Wilkerson 2014/08/11 16:27:47 I don't understand the value of this method. Why i
scheglov 2014/08/11 18:06:25 Done.
148 sourceChange.addEdit(edit, description);
149 }
150
151 /**
152 * Adds an "Update reference" [Edit] to [sourceChange].
153 */
154 void addReferenceEdit(SourceChange sourceChange, SourceReference reference) {
155 Edit edit = createReferenceEdit(reference, newName);
Brian Wilkerson 2014/08/11 16:27:47 As far as I can see, this is the only use of "crea
scheglov 2014/08/11 18:06:25 It will be used by other refactoring implementatio
156 addEdit(sourceChange, "Update reference", edit);
157 }
158
159 @override
160 RefactoringStatus checkInitialConditions(ProgressMonitor pm) {
161 return new RefactoringStatus();
162 }
163
164 @override
165 RefactoringStatus checkNewName(String newName) {
166 RefactoringStatus result = new RefactoringStatus();
167 if (newName == currentName) {
168 result.addFatalError("Choose another name.");
Brian Wilkerson 2014/08/11 16:27:47 Is this user visible text? If so, perhaps somethin
scheglov 2014/08/11 18:06:25 Done.
169 }
170 return result;
171 }
172
173 static String _getDisplayName(Element element) {
174 if (element is ImportElement) {
175 PrefixElement prefix = element.prefix;
176 if (prefix != null) {
177 return prefix.displayName;
178 }
179 }
180 return element.displayName;
181 }
182 }
183
184
185 /**
186 * The [SourceRange] in some [Source].
187 */
188 class SourceReference {
189 final MatchKind kind;
190 final Source source;
191 final SourceRange range;
192
193 List<Element> elements = [];
Brian Wilkerson 2014/08/11 16:27:47 This field isn't currently used (at least not in t
scheglov 2014/08/11 18:06:25 Done.
194
195 SourceReference(this.kind, this.source, this.range);
196
197 @override
198 int get hashCode {
199 int hash = source.hashCode;
200 hash = ((hash << 16) & 0xFFFFFFFF) + range.hashCode;
201 return hash;
202 }
203
204 @override
205 bool operator ==(Object obj) {
206 if (identical(obj, this)) {
207 return true;
208 }
209 if (obj is! SourceReference) {
210 return false;
211 }
212 SourceReference other = obj as SourceReference;
213 return other.source == source && other.range == range;
214 }
215
216 @override
217 String toString() => '${source}@${range}';
218 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698