Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 861d031

Browse files
author
Dart CI
committed
Version 2.19.0-405.0.dev
Merge ba662eb into dev
2 parents 7163c96 + ba662eb commit 861d031

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+449
-255
lines changed

pkg/analysis_server/lib/lsp_protocol/protocol_custom_generated.dart

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,20 @@ class DartNotImportedCompletionResolutionInfo
307307
DartNotImportedCompletionResolutionInfo({
308308
required this.file,
309309
required this.libraryUri,
310+
this.ref,
310311
});
311312
static DartNotImportedCompletionResolutionInfo fromJson(
312313
Map<String, Object?> json) {
313314
final fileJson = json['file'];
314315
final file = fileJson as String;
315316
final libraryUriJson = json['libraryUri'];
316317
final libraryUri = libraryUriJson as String;
318+
final refJson = json['ref'];
319+
final ref = refJson as String?;
317320
return DartNotImportedCompletionResolutionInfo(
318321
file: file,
319322
libraryUri: libraryUri,
323+
ref: ref,
320324
);
321325
}
322326

@@ -328,11 +332,19 @@ class DartNotImportedCompletionResolutionInfo
328332
/// The URI to be imported if this completion is selected.
329333
final String libraryUri;
330334

335+
/// The ElementLocation of the item being completed.
336+
///
337+
/// This is used to provide documentation in the resolved response.
338+
final String? ref;
339+
331340
@override
332341
Map<String, Object?> toJson() {
333342
var result = <String, Object?>{};
334343
result['file'] = file;
335344
result['libraryUri'] = libraryUri;
345+
if (ref != null) {
346+
result['ref'] = ref;
347+
}
336348
return result;
337349
}
338350

@@ -342,8 +354,12 @@ class DartNotImportedCompletionResolutionInfo
342354
allowsUndefined: false, allowsNull: false)) {
343355
return false;
344356
}
345-
return _canParseString(obj, reporter, 'libraryUri',
346-
allowsUndefined: false, allowsNull: false);
357+
if (!_canParseString(obj, reporter, 'libraryUri',
358+
allowsUndefined: false, allowsNull: false)) {
359+
return false;
360+
}
361+
return _canParseString(obj, reporter, 'ref',
362+
allowsUndefined: true, allowsNull: false);
347363
} else {
348364
reporter.reportError(
349365
'must be of type DartNotImportedCompletionResolutionInfo');
@@ -356,13 +372,15 @@ class DartNotImportedCompletionResolutionInfo
356372
return other is DartNotImportedCompletionResolutionInfo &&
357373
other.runtimeType == DartNotImportedCompletionResolutionInfo &&
358374
file == other.file &&
359-
libraryUri == other.libraryUri;
375+
libraryUri == other.libraryUri &&
376+
ref == other.ref;
360377
}
361378

362379
@override
363380
int get hashCode => Object.hash(
364381
file,
365382
libraryUri,
383+
ref,
366384
);
367385

368386
@override

pkg/analysis_server/lib/src/lsp/handlers/handler_completion.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:analysis_server/src/lsp/mapping.dart';
1313
import 'package:analysis_server/src/provisional/completion/completion_core.dart';
1414
import 'package:analysis_server/src/services/completion/completion_performance.dart';
1515
import 'package:analysis_server/src/services/completion/dart/completion_manager.dart';
16+
import 'package:analysis_server/src/services/completion/dart/dart_completion_suggestion.dart';
1617
import 'package:analysis_server/src/services/completion/filtering/fuzzy_matcher.dart';
1718
import 'package:analysis_server/src/services/completion/yaml/analysis_options_generator.dart';
1819
import 'package:analysis_server/src/services/completion/yaml/fix_data_generator.dart';
@@ -455,12 +456,17 @@ class CompletionHandler extends MessageHandler<CompletionParams, CompletionList>
455456
// call later.
456457
CompletionItemResolutionInfo? resolutionInfo;
457458
final libraryUri = item.libraryUri;
459+
458460
if (useNotImportedCompletions &&
459461
libraryUri != null &&
460462
(item.isNotImported ?? false)) {
463+
final dartElement =
464+
item is DartCompletionSuggestion ? item.dartElement : null;
465+
461466
resolutionInfo = DartNotImportedCompletionResolutionInfo(
462467
file: unit.path,
463468
libraryUri: libraryUri,
469+
ref: dartElement?.location?.encoding,
464470
);
465471
}
466472

pkg/analysis_server/lib/src/lsp/handlers/handler_completion_resolve.dart

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
import 'package:analysis_server/lsp_protocol/protocol.dart' hide Element;
66
import 'package:analysis_server/src/computer/computer_hover.dart';
7-
import 'package:analysis_server/src/lsp/client_capabilities.dart';
87
import 'package:analysis_server/src/lsp/constants.dart';
98
import 'package:analysis_server/src/lsp/handlers/handlers.dart';
109
import 'package:analysis_server/src/lsp/mapping.dart';
1110
import 'package:analyzer/dart/analysis/results.dart';
1211
import 'package:analyzer/dart/analysis/session.dart';
13-
import 'package:analyzer/dart/element/element.dart';
12+
import 'package:analyzer/src/dart/element/element.dart';
13+
import 'package:analyzer/src/utilities/extensions/analysis_session.dart';
1414
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1515

1616
class CompletionResolveHandler
@@ -47,13 +47,25 @@ class CompletionResolveHandler
4747
}
4848
}
4949

50-
Future<ErrorOr<CompletionItem>> resolveDartCompletion(
50+
Future<ErrorOr<CompletionItem>> resolveDartNotImportedCompletion(
5151
CompletionItem item,
52-
LspClientCapabilities clientCapabilities,
53-
CancellationToken token, {
54-
required String file,
55-
required Uri libraryUri,
56-
}) async {
52+
DartNotImportedCompletionResolutionInfo data,
53+
CancellationToken token,
54+
) async {
55+
final clientCapabilities = server.clientCapabilities;
56+
if (clientCapabilities == null) {
57+
// This should not happen unless a client misbehaves.
58+
return error(ErrorCodes.ServerNotInitialized,
59+
'Requests not before server is initilized');
60+
}
61+
62+
final file = data.file;
63+
final libraryUri = Uri.parse(data.libraryUri);
64+
final elementLocationReference = data.ref;
65+
final elementLocation = elementLocationReference != null
66+
? ElementLocationImpl.con2(elementLocationReference)
67+
: null;
68+
5769
const timeout = Duration(milliseconds: 1000);
5870
var timer = Stopwatch()..start();
5971
_latestCompletionItem = item;
@@ -108,7 +120,9 @@ class CompletionResolveHandler
108120

109121
// Look up documentation if we can get an element for this item.
110122
Either2<MarkupContent, String>? documentation;
111-
final element = await _getElement(session, libraryUri, item);
123+
final element = elementLocation != null
124+
? await session.locateElement(elementLocation)
125+
: null;
112126
if (element != null) {
113127
final formats = clientCapabilities.completionDocumentationFormats;
114128
final dartDocInfo = server.getDartdocDirectiveInfoForSession(session);
@@ -172,27 +186,6 @@ class CompletionResolveHandler
172186
);
173187
}
174188

175-
Future<ErrorOr<CompletionItem>> resolveDartNotImportedCompletion(
176-
CompletionItem item,
177-
DartNotImportedCompletionResolutionInfo data,
178-
CancellationToken token,
179-
) async {
180-
final clientCapabilities = server.clientCapabilities;
181-
if (clientCapabilities == null) {
182-
// This should not happen unless a client misbehaves.
183-
return error(ErrorCodes.ServerNotInitialized,
184-
'Requests not before server is initilized');
185-
}
186-
187-
return resolveDartCompletion(
188-
item,
189-
clientCapabilities,
190-
token,
191-
file: data.file,
192-
libraryUri: Uri.parse(data.libraryUri),
193-
);
194-
}
195-
196189
Future<ErrorOr<CompletionItem>> resolvePubPackageCompletion(
197190
CompletionItem item,
198191
PubPackageCompletionItemResolutionInfo data,
@@ -228,32 +221,4 @@ class CompletionResolveHandler
228221
data: item.data,
229222
));
230223
}
231-
232-
/// Gets the [Element] for the completion item [item] in [libraryUri].
233-
Future<Element?> _getElement(
234-
AnalysisSession session,
235-
Uri libraryUri,
236-
CompletionItem item,
237-
) async {
238-
// If filterText is different to the label, it's because label has
239-
// parens/args appended so we should take the filterText to get the
240-
// elements name without. We cannot use insertText as it may include
241-
// snippets, whereas filterText is always just the pure string.
242-
var name = item.filterText ?? item.label;
243-
244-
// The label might be `MyEnum.myValue`, but we need to find `MyEnum`.
245-
if (name.contains('.')) {
246-
name = name.substring(0, name.indexOf('.'));
247-
}
248-
249-
// TODO(dantup): This is not handling default constructors or enums
250-
// correctly, so they will both show dart docs from the class/enum and not
251-
// the constructor/enum member. Extension members are not found at all and
252-
// will provide no docs.
253-
254-
final result = await session.getLibraryByUri(libraryUri.toString());
255-
return result is LibraryElementResult
256-
? result.element.exportNamespace.get(name)
257-
: null;
258-
}
259224
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'package:analysis_server/src/protocol_server.dart'
6+
show CompletionSuggestion;
7+
import 'package:analyzer/dart/element/element.dart';
8+
9+
/// An extension of [CompletionSuggestion] that includes additional
10+
/// Dart-specific fields that are not part of the JSON protocol.
11+
class DartCompletionSuggestion extends CompletionSuggestion {
12+
final Element? dartElement;
13+
14+
DartCompletionSuggestion(
15+
super.kind,
16+
super.relevance,
17+
super.completion,
18+
super.selectionOffset,
19+
super.selectionLength,
20+
super.isDeprecated,
21+
super.isPotential, {
22+
super.displayText,
23+
super.replacementOffset,
24+
super.replacementLength,
25+
super.docSummary,
26+
super.docComplete,
27+
super.declaringType,
28+
super.defaultArgumentListString,
29+
super.defaultArgumentListTextRanges,
30+
super.element,
31+
super.returnType,
32+
super.parameterNames,
33+
super.parameterTypes,
34+
super.requiredParameterCount,
35+
super.hasNamedParameters,
36+
super.parameterName,
37+
super.parameterType,
38+
super.libraryUri,
39+
super.isNotImported,
40+
required this.dartElement,
41+
});
42+
}

pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analysis_server/src/protocol_server.dart'
1111
hide Element, ElementKind;
1212
import 'package:analysis_server/src/provisional/completion/dart/completion_dart.dart';
1313
import 'package:analysis_server/src/services/completion/dart/completion_manager.dart';
14+
import 'package:analysis_server/src/services/completion/dart/dart_completion_suggestion.dart';
1415
import 'package:analysis_server/src/services/completion/dart/feature_computer.dart';
1516
import 'package:analysis_server/src/services/completion/dart/utilities.dart';
1617
import 'package:analysis_server/src/utilities/extensions/ast.dart';
@@ -350,7 +351,7 @@ class SuggestionBuilder {
350351
required String displayText,
351352
required int selectionOffset,
352353
}) {
353-
return CompletionSuggestion(
354+
return DartCompletionSuggestion(
354355
CompletionSuggestionKind.INVOCATION,
355356
Relevance.closure,
356357
completion,
@@ -359,6 +360,7 @@ class SuggestionBuilder {
359360
false,
360361
false,
361362
displayText: displayText,
363+
dartElement: type.element,
362364
);
363365
}
364366

@@ -724,7 +726,7 @@ class SuggestionBuilder {
724726
buffer.write('$indent});');
725727

726728
_addSuggestion(
727-
CompletionSuggestion(
729+
DartCompletionSuggestion(
728730
kind,
729731
relevance,
730732
buffer.toString(),
@@ -734,6 +736,7 @@ class SuggestionBuilder {
734736
false,
735737
// Let the user know that we are going to insert a complete statement.
736738
displayText: 'setState(() {});',
739+
dartElement: method,
737740
),
738741
textToMatchOverride: 'setState',
739742
);
@@ -812,7 +815,7 @@ class SuggestionBuilder {
812815
relevance = Relevance.namedArgument;
813816
}
814817

815-
var suggestion = CompletionSuggestion(
818+
var suggestion = DartCompletionSuggestion(
816819
CompletionSuggestionKind.NAMED_ARGUMENT,
817820
relevance,
818821
completion,
@@ -822,7 +825,8 @@ class SuggestionBuilder {
822825
false,
823826
parameterName: name,
824827
parameterType: type,
825-
replacementLength: replacementLength);
828+
replacementLength: replacementLength,
829+
dartElement: parameter);
826830
if (parameter is FieldFormalParameterElement) {
827831
_setDocumentation(suggestion, parameter);
828832
suggestion.element =
@@ -916,15 +920,16 @@ class SuggestionBuilder {
916920
var offsetDelta = targetId.offset + replacement.indexOf(completion);
917921
var displayText =
918922
displayTextBuffer.isNotEmpty ? displayTextBuffer.toString() : null;
919-
var suggestion = CompletionSuggestion(
923+
var suggestion = DartCompletionSuggestion(
920924
CompletionSuggestionKind.OVERRIDE,
921925
Relevance.override,
922926
completion,
923927
selectionRange.offset - offsetDelta,
924928
selectionRange.length,
925929
element.hasDeprecated,
926930
false,
927-
displayText: displayText);
931+
displayText: displayText,
932+
dartElement: element);
928933
suggestion.element = protocol.convertElement(element,
929934
withNullability: _isNonNullableByDefault);
930935
_addSuggestion(
@@ -1418,6 +1423,7 @@ class SuggestionBuilder {
14181423
documentation: documentation,
14191424
defaultArgumentList: defaultArgumentList,
14201425
element: suggestedElement,
1426+
dartElement: element,
14211427
);
14221428
}
14231429

@@ -1651,7 +1657,7 @@ class _CompletionSuggestionBuilderImpl implements CompletionSuggestionBuilder {
16511657

16521658
@override
16531659
CompletionSuggestion build() {
1654-
return CompletionSuggestion(
1660+
return DartCompletionSuggestion(
16551661
kind,
16561662
relevance,
16571663
completion,
@@ -1672,6 +1678,7 @@ class _CompletionSuggestionBuilderImpl implements CompletionSuggestionBuilder {
16721678
defaultArgumentListTextRanges: element.defaultArgumentList?.ranges,
16731679
libraryUri: libraryUriStr,
16741680
isNotImported: isNotImported ? true : null,
1681+
dartElement: element.dartElement,
16751682
);
16761683
}
16771684
}
@@ -1691,6 +1698,7 @@ class _ElementCompletionData {
16911698
CompletionDefaultArgumentList? defaultArgumentList;
16921699
final _ElementDocumentation? documentation;
16931700
final protocol.Element element;
1701+
final Element dartElement;
16941702

16951703
_ElementCompletionData({
16961704
required this.completion,
@@ -1704,6 +1712,7 @@ class _ElementCompletionData {
17041712
required this.defaultArgumentList,
17051713
required this.documentation,
17061714
required this.element,
1715+
required this.dartElement,
17071716
});
17081717
}
17091718

0 commit comments

Comments
 (0)