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

Unified Diff: pkg/analysis_server/lib/src/services/refactoring/rename_import.dart

Issue 2982993003: Remove UriReferencedElement with its uri/uriOffset/uriEnd properties. (Closed)
Patch Set: Merge. Created 3 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/services/refactoring/rename_import.dart
diff --git a/pkg/analysis_server/lib/src/services/refactoring/rename_import.dart b/pkg/analysis_server/lib/src/services/refactoring/rename_import.dart
index 8d03961ac28c06e1038e5d78df408ebdcd2d249b..1f5ef92b0c04aae762168eb33bef5d6e133e9306 100644
--- a/pkg/analysis_server/lib/src/services/refactoring/rename_import.dart
+++ b/pkg/analysis_server/lib/src/services/refactoring/rename_import.dart
@@ -14,6 +14,7 @@ import 'package:analysis_server/src/services/search/search_engine.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
+import 'package:analyzer/src/dart/element/ast_provider.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -21,7 +22,10 @@ import 'package:analyzer_plugin/utilities/range_factory.dart';
* A [Refactoring] for renaming [ImportElement]s.
*/
class RenameImportRefactoringImpl extends RenameRefactoringImpl {
- RenameImportRefactoringImpl(SearchEngine searchEngine, ImportElement element)
+ final AstProvider astProvider;
+
+ RenameImportRefactoringImpl(
+ SearchEngine searchEngine, this.astProvider, ImportElement element)
: super(searchEngine, element);
@override
@@ -52,14 +56,17 @@ class RenameImportRefactoringImpl extends RenameRefactoringImpl {
PrefixElement prefix = element.prefix;
SourceEdit edit = null;
if (newName.isEmpty) {
- int uriEnd = element.uriEnd;
+ ImportDirective node = await _findNode();
+ int uriEnd = node.uri.end;
int prefixEnd = element.prefixOffset + prefix.nameLength;
edit = newSourceEdit_range(
range.startOffsetEndOffset(uriEnd, prefixEnd), "");
} else {
if (prefix == null) {
- edit = newSourceEdit_range(
- new SourceRange(element.uriEnd, 0), " as $newName");
+ ImportDirective node = await _findNode();
+ int uriEnd = node.uri.end;
+ edit =
+ newSourceEdit_range(new SourceRange(uriEnd, 0), " as $newName");
} else {
int offset = element.prefixOffset;
int length = prefix.nameLength;
@@ -95,6 +102,16 @@ class RenameImportRefactoringImpl extends RenameRefactoringImpl {
}
/**
+ * Return the [ImportDirective] node that corresponds to the [element].
+ */
+ Future<ImportDirective> _findNode() async {
+ LibraryElement library = element.library;
+ CompilationUnit unit = await astProvider.getParsedUnitForElement(library);
+ int index = library.imports.indexOf(element);
+ return unit.directives.where((d) => d is ImportDirective).toList()[index];
+ }
+
+ /**
* If the given [reference] is before an interpolated [SimpleIdentifier] in
* an [InterpolationExpression] without surrounding curly brackets, return it.
* Otherwise return `null`.

Powered by Google App Engine
This is Rietveld 408576698