Skip to content

Commit a483a5a

Browse files
bwilkersonCommit Queue
authored andcommitted
Add imports to the summary produced by the server
This only includes the imports from the first fragment. We'll eventually need to decide what to do about imports in part files, preferably before that feature has shipped, but the lack of support won't impact users today. Change-Id: Ia38149f619dc22085ed02a90892e0af13eb188dd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/443143 Reviewed-by: Jake Macdonald <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent ebed8dd commit a483a5a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

pkg/analysis_server/lib/src/lsp/handlers/custom/handler_summary.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ class SummaryWriter {
7070

7171
String summarize() {
7272
var libraryElement = result.element;
73+
74+
// Write a summary of the imports.
75+
var libraryImports = libraryElement.firstFragment.libraryImports;
76+
for (var libraryImport in libraryImports) {
77+
summarizeImport(libraryImport);
78+
}
79+
80+
// Write a summary of the declarations in the export namespace.
7381
var definedNames = libraryElement.exportNamespace.definedNames2;
7482
var needsSeparator = false;
7583
for (var element in definedNames.values) {
@@ -351,6 +359,35 @@ class SummaryWriter {
351359
buffer.write(' ');
352360
}
353361

362+
void summarizeImport(LibraryImport libraryImport) {
363+
if (libraryImport.isSynthetic) return;
364+
buffer.write("import '");
365+
buffer.write(libraryImport.uri.uriString);
366+
buffer.write("'");
367+
if (libraryImport.prefix?.name case var prefix?) {
368+
buffer.write(' as ');
369+
buffer.write(prefix);
370+
}
371+
for (var combinator in libraryImport.combinators) {
372+
if (combinator is ShowElementCombinator) {
373+
var prefix = ' show ';
374+
for (var name in combinator.shownNames) {
375+
buffer.write(prefix);
376+
buffer.write(name);
377+
prefix = ', ';
378+
}
379+
} else if (combinator is HideElementCombinator) {
380+
var prefix = ' hide ';
381+
for (var name in combinator.hiddenNames) {
382+
buffer.write(prefix);
383+
buffer.write(name);
384+
prefix = ', ';
385+
}
386+
}
387+
}
388+
buffer.writeln(';');
389+
}
390+
354391
void summarizeList<E>(List<E> list, void Function(E) summarizeElement) {
355392
var separator = '';
356393
for (var element in list) {
@@ -497,3 +534,13 @@ class SummaryWriter {
497534
summarizeTypes(types);
498535
}
499536
}
537+
538+
extension on DirectiveUri {
539+
String get uriString => switch (this) {
540+
DirectiveUriWithLibrary(:var source) => source.uri.toString(),
541+
DirectiveUriWithRelativeUriString(:var relativeUriString) =>
542+
relativeUriString,
543+
DirectiveUri() =>
544+
throw UnimplementedError('Unhandled instance of type $runtimeType'),
545+
};
546+
}

pkg/analysis_server/test/lsp/summary_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ extension type E(int self) {
132132
);
133133
}
134134

135+
Future<void> test_imports() async {
136+
failTestOnErrorDiagnostic = false;
137+
await verifySummary(
138+
'''
139+
import 'dart:async' show Stream;
140+
import 'package:test/test.dart' as test;
141+
import 'other.dart' deferred as o hide SomeClass;
142+
''',
143+
'''
144+
import 'dart:async' show Stream;
145+
import 'package:test/test.dart' as test;
146+
import 'package:test/other.dart' as o hide SomeClass;
147+
''',
148+
);
149+
}
150+
135151
Future<void> test_mixin() async {
136152
await verifySummary(
137153
'''

0 commit comments

Comments
 (0)