OLD | NEW |
| (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 library services.src.refactoring.rename_constructor; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:analysis_services/correction/change.dart'; | |
10 import 'package:analysis_services/correction/status.dart'; | |
11 import 'package:analysis_services/refactoring/refactoring.dart'; | |
12 import 'package:analysis_services/search/hierarchy.dart'; | |
13 import 'package:analysis_services/search/search_engine.dart'; | |
14 import 'package:analysis_services/src/correction/util.dart'; | |
15 import 'package:analysis_services/src/refactoring/naming_conventions.dart'; | |
16 import 'package:analysis_services/src/refactoring/rename.dart'; | |
17 import 'package:analyzer/src/generated/element.dart'; | |
18 import 'package:analyzer/src/generated/java_core.dart'; | |
19 | |
20 | |
21 /** | |
22 * A [Refactoring] for renaming [ConstructorElement]s. | |
23 */ | |
24 class RenameConstructorRefactoringImpl extends RenameRefactoringImpl { | |
25 RenameConstructorRefactoringImpl(SearchEngine searchEngine, | |
26 ConstructorElement element) | |
27 : super(searchEngine, element); | |
28 | |
29 @override | |
30 ConstructorElement get element => super.element as ConstructorElement; | |
31 | |
32 @override | |
33 String get refactoringName { | |
34 return "Rename Constructor"; | |
35 } | |
36 | |
37 @override | |
38 Future<RefactoringStatus> checkFinalConditions() { | |
39 RefactoringStatus result = new RefactoringStatus(); | |
40 _analyzePossibleConflicts(result); | |
41 return new Future.value(result); | |
42 } | |
43 | |
44 @override | |
45 RefactoringStatus checkNewName() { | |
46 RefactoringStatus result = super.checkNewName(); | |
47 result.addStatus(validateConstructorName(newName)); | |
48 return result; | |
49 } | |
50 | |
51 @override | |
52 Future<Change> createChange() { | |
53 Change change = new Change(refactoringName); | |
54 String replacement = newName.isEmpty ? '' : '.${newName}'; | |
55 // update references | |
56 return searchEngine.searchReferences(element).then((refMatches) { | |
57 List<SourceReference> references = getSourceReferences(refMatches); | |
58 if (!element.isSynthetic) { | |
59 for (SourceReference reference in references) { | |
60 Edit edit = createReferenceEdit(reference, replacement); | |
61 change.addEdit(reference.file, edit); | |
62 } | |
63 } | |
64 return change; | |
65 }); | |
66 } | |
67 | |
68 void _analyzePossibleConflicts(RefactoringStatus result) { | |
69 // check if there are members with "newName" in the same ClassElement | |
70 ClassElement parentClass = element.enclosingElement; | |
71 for (Element newNameMember in getChildren(parentClass, newName)) { | |
72 String message = | |
73 format( | |
74 "Class '{0}' already declares {1} with name '{2}'.", | |
75 parentClass.displayName, | |
76 getElementKindName(newNameMember), | |
77 newName); | |
78 result.addError( | |
79 message, | |
80 new RefactoringStatusContext.forElement(newNameMember)); | |
81 } | |
82 } | |
83 } | |
OLD | NEW |