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_import; | |
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/search_engine.dart'; | |
13 import 'package:analysis_services/src/correction/source_range.dart'; | |
14 import 'package:analysis_services/src/refactoring/naming_conventions.dart'; | |
15 import 'package:analysis_services/src/refactoring/rename.dart'; | |
16 import 'package:analyzer/src/generated/element.dart'; | |
17 import 'package:analyzer/src/generated/source.dart'; | |
18 | |
19 | |
20 /** | |
21 * A [Refactoring] for renaming [ImportElement]s. | |
22 */ | |
23 class RenameImportRefactoringImpl extends RenameRefactoringImpl { | |
24 RenameImportRefactoringImpl(SearchEngine searchEngine, ImportElement element) | |
25 : super(searchEngine, element); | |
26 | |
27 @override | |
28 ImportElement get element => super.element as ImportElement; | |
29 | |
30 @override | |
31 String get refactoringName { | |
32 return "Rename Import Prefix"; | |
33 } | |
34 | |
35 @override | |
36 Future<RefactoringStatus> checkFinalConditions() { | |
37 RefactoringStatus result = new RefactoringStatus(); | |
38 return new Future.value(result); | |
39 } | |
40 | |
41 @override | |
42 RefactoringStatus checkNewName() { | |
43 RefactoringStatus result = super.checkNewName(); | |
44 result.addStatus(validateImportPrefixName(newName)); | |
45 return result; | |
46 } | |
47 | |
48 @override | |
49 Future<Change> createChange() { | |
50 Change change = new Change(refactoringName); | |
51 // update declaration | |
52 { | |
53 String file = getElementFile(element); | |
54 PrefixElement prefix = element.prefix; | |
55 Edit edit = null; | |
56 if (newName.isEmpty) { | |
57 int uriEnd = element.uriEnd; | |
58 int prefixEnd = element.prefixOffset + prefix.displayName.length; | |
59 SourceRange range = rangeStartEnd(uriEnd, prefixEnd); | |
60 edit = new Edit.range(range, ""); | |
61 } else { | |
62 if (prefix == null) { | |
63 SourceRange range = rangeStartLength(element.uriEnd, 0); | |
64 edit = new Edit.range(range, " as ${newName}"); | |
65 } else { | |
66 int offset = element.prefixOffset; | |
67 int length = prefix.displayName.length; | |
68 SourceRange range = rangeStartLength(offset, length); | |
69 edit = new Edit.range(range, newName); | |
70 } | |
71 } | |
72 if (edit != null) { | |
73 change.addEdit(file, edit); | |
74 } | |
75 } | |
76 // update references | |
77 return searchEngine.searchReferences(element).then((refMatches) { | |
78 List<SourceReference> references = getSourceReferences(refMatches); | |
79 for (SourceReference reference in references) { | |
80 Edit edit; | |
81 if (newName.isEmpty) { | |
82 edit = createReferenceEdit(reference, newName); | |
83 } else { | |
84 edit = createReferenceEdit(reference, "${newName}."); | |
85 } | |
86 change.addEdit(reference.file, edit); | |
87 } | |
88 return change; | |
89 }); | |
90 } | |
91 } | |
OLD | NEW |