Improve the convert-to-package-uri assist
Change-Id: I2010d33b740932569e6fcafcc15fde30a96cf0df
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101743
Reviewed-by: Jaime Wren <jwren@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/assist.dart b/pkg/analysis_server/lib/src/services/correction/assist.dart
index 50ac2ac..f5df08e 100644
--- a/pkg/analysis_server/lib/src/services/correction/assist.dart
+++ b/pkg/analysis_server/lib/src/services/correction/assist.dart
@@ -63,7 +63,7 @@
static const CONVERT_INTO_ABSOLUTE_IMPORT = const AssistKind(
'dart.assist.convert.relativeToAbsoluteImport',
30,
- "Convert to absolute import",
+ "Convert to 'package:' import",
associatedErrorCodes: <String>['avoid_relative_lib_imports']);
static const CONVERT_INTO_FOR_INDEX = const AssistKind(
'dart.assist.convert.forEachToForIndex', 30, "Convert to for-index loop");
diff --git a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
index 6539550..a3ff1f7 100644
--- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
@@ -1230,17 +1230,21 @@
}
Future<void> _addProposal_convertToAbsoluteImport() async {
- var node = this.node;
+ AstNode node = this.node;
if (node is StringLiteral) {
node = node.parent;
}
if (node is ImportDirective) {
- var importDirective = node;
- var importUri = node.uriSource?.uri;
+ ImportDirective importDirective = node;
+ if (Uri.parse(node.uriContent)?.scheme == 'package') {
+ // Don't offer to convert a 'package:' URI to itself.
+ return;
+ }
+ Uri importUri = node.uriSource?.uri;
if (importUri?.scheme != 'package') {
return;
}
- var changeBuilder = _newDartChangeBuilder();
+ DartChangeBuilder changeBuilder = _newDartChangeBuilder();
await changeBuilder.addFileEdit(file, (builder) {
builder.addSimpleReplacement(
range.node(importDirective.uri), "'$importUri'");
diff --git a/pkg/analysis_server/test/src/services/correction/assist/convert_into_absolute_import_test.dart b/pkg/analysis_server/test/src/services/correction/assist/convert_into_absolute_import_test.dart
index 36e3fab..8b9566b 100644
--- a/pkg/analysis_server/test/src/services/correction/assist/convert_into_absolute_import_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/assist/convert_into_absolute_import_test.dart
@@ -19,17 +19,6 @@
@override
AssistKind get kind => DartAssistKind.CONVERT_INTO_ABSOLUTE_IMPORT;
- test_fileName_onUri() async {
- addSource('/home/test/lib/foo.dart', '');
-
- await resolveTestUnit('''
-import 'foo.dart';
-''');
- await assertHasAssistAt('foo.dart', '''
-import 'package:test/foo.dart';
-''');
- }
-
test_fileName_onImport() async {
addSource('/home/test/lib/foo.dart', '');
@@ -42,6 +31,17 @@
''');
}
+ test_fileName_onUri() async {
+ addSource('/home/test/lib/foo.dart', '');
+
+ await resolveTestUnit('''
+import 'foo.dart';
+''');
+ await assertHasAssistAt('foo.dart', '''
+import 'package:test/foo.dart';
+''');
+ }
+
test_nonPackage_Uri() async {
addSource('/home/test/lib/foo.dart', '');
@@ -53,6 +53,16 @@
await assertNoAssistAt('import');
}
+ test_packageUri() async {
+ addSource('/home/test/lib/foo.dart', '');
+
+ await resolveTestUnit('''
+import 'package:test/foo.dart';
+''');
+ await assertNoAssistAt('foo.dart');
+ await assertNoAssistAt('import');
+ }
+
test_path() async {
addSource('/home/test/lib/foo/bar.dart', '');