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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:analysis_server/src/protocol_server.dart'; | 7 import 'package:analysis_server/src/protocol_server.dart'; |
8 import 'package:analysis_server/src/services/correction/status.dart'; | 8 import 'package:analysis_server/src/services/correction/status.dart'; |
9 import 'package:analysis_server/src/services/refactoring/naming_conventions.dart
'; | 9 import 'package:analysis_server/src/services/refactoring/naming_conventions.dart
'; |
10 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 10 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
11 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; | 11 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; |
12 import 'package:analysis_server/src/services/refactoring/rename.dart'; | 12 import 'package:analysis_server/src/services/refactoring/rename.dart'; |
13 import 'package:analysis_server/src/services/search/search_engine.dart'; | 13 import 'package:analysis_server/src/services/search/search_engine.dart'; |
14 import 'package:analyzer/dart/ast/ast.dart'; | 14 import 'package:analyzer/dart/ast/ast.dart'; |
15 import 'package:analyzer/dart/element/element.dart'; | 15 import 'package:analyzer/dart/element/element.dart'; |
16 import 'package:analyzer/src/dart/ast/utilities.dart'; | 16 import 'package:analyzer/src/dart/ast/utilities.dart'; |
| 17 import 'package:analyzer/src/dart/element/ast_provider.dart'; |
17 import 'package:analyzer/src/generated/source.dart'; | 18 import 'package:analyzer/src/generated/source.dart'; |
18 import 'package:analyzer_plugin/utilities/range_factory.dart'; | 19 import 'package:analyzer_plugin/utilities/range_factory.dart'; |
19 | 20 |
20 /** | 21 /** |
21 * A [Refactoring] for renaming [ImportElement]s. | 22 * A [Refactoring] for renaming [ImportElement]s. |
22 */ | 23 */ |
23 class RenameImportRefactoringImpl extends RenameRefactoringImpl { | 24 class RenameImportRefactoringImpl extends RenameRefactoringImpl { |
24 RenameImportRefactoringImpl(SearchEngine searchEngine, ImportElement element) | 25 final AstProvider astProvider; |
| 26 |
| 27 RenameImportRefactoringImpl( |
| 28 SearchEngine searchEngine, this.astProvider, ImportElement element) |
25 : super(searchEngine, element); | 29 : super(searchEngine, element); |
26 | 30 |
27 @override | 31 @override |
28 ImportElement get element => super.element as ImportElement; | 32 ImportElement get element => super.element as ImportElement; |
29 | 33 |
30 @override | 34 @override |
31 String get refactoringName { | 35 String get refactoringName { |
32 return "Rename Import Prefix"; | 36 return "Rename Import Prefix"; |
33 } | 37 } |
34 | 38 |
(...skipping 10 matching lines...) Expand all Loading... |
45 return result; | 49 return result; |
46 } | 50 } |
47 | 51 |
48 @override | 52 @override |
49 Future fillChange() async { | 53 Future fillChange() async { |
50 // update declaration | 54 // update declaration |
51 { | 55 { |
52 PrefixElement prefix = element.prefix; | 56 PrefixElement prefix = element.prefix; |
53 SourceEdit edit = null; | 57 SourceEdit edit = null; |
54 if (newName.isEmpty) { | 58 if (newName.isEmpty) { |
55 int uriEnd = element.uriEnd; | 59 ImportDirective node = await _findNode(); |
| 60 int uriEnd = node.uri.end; |
56 int prefixEnd = element.prefixOffset + prefix.nameLength; | 61 int prefixEnd = element.prefixOffset + prefix.nameLength; |
57 edit = newSourceEdit_range( | 62 edit = newSourceEdit_range( |
58 range.startOffsetEndOffset(uriEnd, prefixEnd), ""); | 63 range.startOffsetEndOffset(uriEnd, prefixEnd), ""); |
59 } else { | 64 } else { |
60 if (prefix == null) { | 65 if (prefix == null) { |
61 edit = newSourceEdit_range( | 66 ImportDirective node = await _findNode(); |
62 new SourceRange(element.uriEnd, 0), " as $newName"); | 67 int uriEnd = node.uri.end; |
| 68 edit = |
| 69 newSourceEdit_range(new SourceRange(uriEnd, 0), " as $newName"); |
63 } else { | 70 } else { |
64 int offset = element.prefixOffset; | 71 int offset = element.prefixOffset; |
65 int length = prefix.nameLength; | 72 int length = prefix.nameLength; |
66 edit = newSourceEdit_range(new SourceRange(offset, length), newName); | 73 edit = newSourceEdit_range(new SourceRange(offset, length), newName); |
67 } | 74 } |
68 } | 75 } |
69 if (edit != null) { | 76 if (edit != null) { |
70 doSourceChange_addElementEdit(change, element, edit); | 77 doSourceChange_addElementEdit(change, element, edit); |
71 } | 78 } |
72 } | 79 } |
(...skipping 15 matching lines...) Expand all Loading... |
88 interpolationIdentifier.length, | 95 interpolationIdentifier.length, |
89 '{$newName.${interpolationIdentifier.name}}')); | 96 '{$newName.${interpolationIdentifier.name}}')); |
90 } else { | 97 } else { |
91 reference.addEdit(change, '$newName.'); | 98 reference.addEdit(change, '$newName.'); |
92 } | 99 } |
93 } | 100 } |
94 } | 101 } |
95 } | 102 } |
96 | 103 |
97 /** | 104 /** |
| 105 * Return the [ImportDirective] node that corresponds to the [element]. |
| 106 */ |
| 107 Future<ImportDirective> _findNode() async { |
| 108 LibraryElement library = element.library; |
| 109 CompilationUnit unit = await astProvider.getParsedUnitForElement(library); |
| 110 int index = library.imports.indexOf(element); |
| 111 return unit.directives.where((d) => d is ImportDirective).toList()[index]; |
| 112 } |
| 113 |
| 114 /** |
98 * If the given [reference] is before an interpolated [SimpleIdentifier] in | 115 * If the given [reference] is before an interpolated [SimpleIdentifier] in |
99 * an [InterpolationExpression] without surrounding curly brackets, return it. | 116 * an [InterpolationExpression] without surrounding curly brackets, return it. |
100 * Otherwise return `null`. | 117 * Otherwise return `null`. |
101 */ | 118 */ |
102 SimpleIdentifier _getInterpolationIdentifier(SourceReference reference) { | 119 SimpleIdentifier _getInterpolationIdentifier(SourceReference reference) { |
103 Source source = reference.element.source; | 120 Source source = reference.element.source; |
104 CompilationUnit unit = context.parseCompilationUnit(source); | 121 CompilationUnit unit = context.parseCompilationUnit(source); |
105 NodeLocator nodeLocator = new NodeLocator(reference.range.offset); | 122 NodeLocator nodeLocator = new NodeLocator(reference.range.offset); |
106 AstNode node = nodeLocator.searchWithin(unit); | 123 AstNode node = nodeLocator.searchWithin(unit); |
107 if (node is SimpleIdentifier) { | 124 if (node is SimpleIdentifier) { |
108 AstNode parent = node.parent; | 125 AstNode parent = node.parent; |
109 if (parent is InterpolationExpression && parent.rightBracket == null) { | 126 if (parent is InterpolationExpression && parent.rightBracket == null) { |
110 return node; | 127 return node; |
111 } | 128 } |
112 } | 129 } |
113 return null; | 130 return null; |
114 } | 131 } |
115 } | 132 } |
OLD | NEW |