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_class_member; | 5 library services.src.refactoring.rename_class_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 hide Element, ElementKind; | 10 hide Element, ElementKind; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 _ClassMemberValidator.forRename(this.searchEngine, Element element, this.name) | 145 _ClassMemberValidator.forRename(this.searchEngine, Element element, this.name) |
146 : isRename = true, | 146 : isRename = true, |
147 library = element.library, | 147 library = element.library, |
148 element = element, | 148 element = element, |
149 elementClass = element.enclosingElement, | 149 elementClass = element.enclosingElement, |
150 elementKind = element.kind; | 150 elementKind = element.kind; |
151 | 151 |
152 Future<RefactoringStatus> validate() async { | 152 Future<RefactoringStatus> validate() async { |
153 // check if there is a member with "newName" in the same ClassElement | 153 // check if there is a member with "newName" in the same ClassElement |
154 for (Element newNameMember in getChildren(elementClass, name)) { | 154 for (Element newNameMember in getChildren(elementClass, name)) { |
155 result.addError(format( | 155 result.addError( |
156 "Class '{0}' already declares {1} with name '{2}'.", | 156 format( |
157 elementClass.displayName, getElementKindName(newNameMember), | 157 "Class '{0}' already declares {1} with name '{2}'.", |
158 name), newLocation_fromElement(newNameMember)); | 158 elementClass.displayName, |
| 159 getElementKindName(newNameMember), |
| 160 name), |
| 161 newLocation_fromElement(newNameMember)); |
159 } | 162 } |
160 // do chained computations | 163 // do chained computations |
161 Set<ClassElement> superClasses = getSuperClasses(elementClass); | 164 Set<ClassElement> superClasses = getSuperClasses(elementClass); |
162 await _prepareReferences(); | 165 await _prepareReferences(); |
163 Set<ClassElement> subClasses = | 166 Set<ClassElement> subClasses = |
164 await getSubClasses(searchEngine, elementClass); | 167 await getSubClasses(searchEngine, elementClass); |
165 // check shadowing in hierarchy | 168 // check shadowing in hierarchy |
166 List<SearchMatch> declarations = | 169 List<SearchMatch> declarations = |
167 await searchEngine.searchElementDeclarations(name); | 170 await searchEngine.searchElementDeclarations(name); |
168 for (SearchMatch declaration in declarations) { | 171 for (SearchMatch declaration in declarations) { |
169 Element nameElement = getSyntheticAccessorVariable(declaration.element); | 172 Element nameElement = getSyntheticAccessorVariable(declaration.element); |
170 Element nameClass = nameElement.enclosingElement; | 173 Element nameClass = nameElement.enclosingElement; |
171 // renamed Element shadows member of superclass | 174 // renamed Element shadows member of superclass |
172 if (superClasses.contains(nameClass)) { | 175 if (superClasses.contains(nameClass)) { |
173 result.addError(format(isRename | 176 result.addError( |
| 177 format( |
| 178 isRename |
174 ? "Renamed {0} will shadow {1} '{2}'." | 179 ? "Renamed {0} will shadow {1} '{2}'." |
175 : "Created {0} will shadow {1} '{2}'.", | 180 : "Created {0} will shadow {1} '{2}'.", |
176 elementKind.displayName, getElementKindName(nameElement), | 181 elementKind.displayName, |
| 182 getElementKindName(nameElement), |
177 getElementQualifiedName(nameElement)), | 183 getElementQualifiedName(nameElement)), |
178 newLocation_fromElement(nameElement)); | 184 newLocation_fromElement(nameElement)); |
179 } | 185 } |
180 // renamed Element is shadowed by member of subclass | 186 // renamed Element is shadowed by member of subclass |
181 if (isRename && subClasses.contains(nameClass)) { | 187 if (isRename && subClasses.contains(nameClass)) { |
182 result.addError(format("Renamed {0} will be shadowed by {1} '{2}'.", | 188 result.addError( |
183 elementKind.displayName, getElementKindName(nameElement), | 189 format( |
| 190 "Renamed {0} will be shadowed by {1} '{2}'.", |
| 191 elementKind.displayName, |
| 192 getElementKindName(nameElement), |
184 getElementQualifiedName(nameElement)), | 193 getElementQualifiedName(nameElement)), |
185 newLocation_fromElement(nameElement)); | 194 newLocation_fromElement(nameElement)); |
186 } | 195 } |
187 // renamed Element is shadowed by local | 196 // renamed Element is shadowed by local |
188 if (nameElement is LocalElement) { | 197 if (nameElement is LocalElement) { |
189 LocalElement localElement = nameElement; | 198 LocalElement localElement = nameElement; |
190 ClassElement enclosingClass = | 199 ClassElement enclosingClass = |
191 nameElement.getAncestor((element) => element is ClassElement); | 200 nameElement.getAncestor((element) => element is ClassElement); |
192 if (enclosingClass == elementClass || | 201 if (enclosingClass == elementClass || |
193 subClasses.contains(enclosingClass)) { | 202 subClasses.contains(enclosingClass)) { |
194 for (SearchMatch reference in references) { | 203 for (SearchMatch reference in references) { |
195 if (isReferenceInLocalRange(localElement, reference)) { | 204 if (isReferenceInLocalRange(localElement, reference)) { |
196 result.addError(format( | 205 result.addError( |
197 "Usage of renamed {0} will be shadowed by {1} '{2}'.", | 206 format( |
198 elementKind.displayName, getElementKindName(localElement), | 207 "Usage of renamed {0} will be shadowed by {1} '{2}'.", |
199 localElement.displayName), newLocation_fromMatch(reference)); | 208 elementKind.displayName, |
| 209 getElementKindName(localElement), |
| 210 localElement.displayName), |
| 211 newLocation_fromMatch(reference)); |
200 } | 212 } |
201 } | 213 } |
202 } | 214 } |
203 } | 215 } |
204 } | 216 } |
205 // visibility | 217 // visibility |
206 if (isRename) { | 218 if (isRename) { |
207 _validateWillBeInvisible(); | 219 _validateWillBeInvisible(); |
208 } | 220 } |
209 // done | 221 // done |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 Element refElement = reference.element; | 259 Element refElement = reference.element; |
248 LibraryElement refLibrary = refElement.library; | 260 LibraryElement refLibrary = refElement.library; |
249 if (refLibrary != library) { | 261 if (refLibrary != library) { |
250 String message = format("Renamed {0} will be invisible in '{1}'.", | 262 String message = format("Renamed {0} will be invisible in '{1}'.", |
251 getElementKindName(element), getElementQualifiedName(refLibrary)); | 263 getElementKindName(element), getElementQualifiedName(refLibrary)); |
252 result.addError(message, newLocation_fromMatch(reference)); | 264 result.addError(message, newLocation_fromMatch(reference)); |
253 } | 265 } |
254 } | 266 } |
255 } | 267 } |
256 } | 268 } |
OLD | NEW |