Skip to content

Commit ddab694

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[analysis_server] Add failing test for auto-import extensions
See #56320 Change-Id: I6a21ef528d6d67a34e7f845c6cb279f246fe2762 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/382621 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]>
1 parent 6addfa0 commit ddab694

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

pkg/analysis_server/test/lsp/completion_dart_test.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,6 +3271,65 @@ void f() {
32713271
expect(resolved.detail, isNot(contains('Auto import from')));
32723272
}
32733273

3274+
/// Verify extensions can be auto-imported if not already in-scope.
3275+
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/56320')
3276+
Future<void> test_unimportedSymbols_extension() async {
3277+
// Define extensions in 'extensions.dart'.
3278+
newFile(
3279+
join(projectFolderPath, 'lib', 'extensions.dart'),
3280+
'''
3281+
extension StringExtensions on String {
3282+
String get empty => '';
3283+
}
3284+
''',
3285+
);
3286+
3287+
// Also import the extensions into an unrelated file to ensure this doesn't
3288+
// cause extra suggestions (https://github.com/dart-lang/sdk/issues/56320).
3289+
newFile(
3290+
join(projectFolderPath, 'lib', 'other.dart'),
3291+
'import "extensions.dart";',
3292+
);
3293+
3294+
var content = '''
3295+
void f(String a) {
3296+
a.empt^
3297+
}
3298+
''';
3299+
3300+
await initialize();
3301+
var code = TestCode.parse(content);
3302+
await openFile(mainFileUri, code.code);
3303+
await initialAnalysis;
3304+
var res = await getCompletion(mainFileUri, code.position.position);
3305+
3306+
// Expect only a single entry for the 'empty' extension member.
3307+
var completions = res.where((c) => c.label == 'empty');
3308+
expect(completions, hasLength(1));
3309+
3310+
// Expect it to auto-import from 'extensions.dart'.
3311+
var resolved = await resolveCompletion(completions.single);
3312+
expect(
3313+
resolved.detail,
3314+
startsWith("Auto import from 'package:test/extensions.dart'"),
3315+
);
3316+
3317+
// Verify the edits.
3318+
var newContent = applyTextEdits(
3319+
code.code,
3320+
[toTextEdit(resolved.textEdit!)]
3321+
.followedBy(resolved.additionalTextEdits!)
3322+
.toList(),
3323+
);
3324+
expect(newContent, equals('''
3325+
import 'package:test/extensions.dart';
3326+
3327+
void f(String a) {
3328+
a.empty
3329+
}
3330+
'''));
3331+
}
3332+
32743333
Future<void> test_unimportedSymbols_filtersOutAlreadyImportedSymbols() async {
32753334
newFile(
32763335
join(projectFolderPath, 'lib', 'source_file.dart'),

0 commit comments

Comments
 (0)