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

Commit f38618d

Browse files
author
Dart CI
committed
Version 2.15.0-275.0.dev
Merge commit '941bfb4aeea5b4df0d65391d0bd5201144dc25a9' into 'dev'
2 parents 1d2d4bb + 941bfb4 commit f38618d

20 files changed

+375
-186
lines changed

DEPS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ vars = {
107107
"dart_style_rev": "08b0294d0a500d5c02168ef57dcb8868d0c3cb48",
108108

109109
"dartdoc_rev" : "520e64977de7a87b2fd5be56e5c2e1a58d55bdad",
110-
"devtools_rev" : "8881a7caa9067471008a8e00750b161f53cdb843",
110+
"devtools_rev" : "3a2f570813200e6dee141f3e7a9edcae5f31c2e8",
111111
"jsshell_tag": "version:88.0",
112112
"ffi_rev": "4dd32429880a57b64edaf54c9d5af8a9fa9a4ffb",
113113
"fixnum_rev": "16d3890c6dc82ca629659da1934e412292508bba",

pkg/analysis_server/lib/src/domain_completion.dart

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:collection';
67

78
import 'package:analysis_server/protocol/protocol.dart';
89
import 'package:analysis_server/protocol/protocol_constants.dart';
@@ -16,6 +17,7 @@ import 'package:analysis_server/src/provisional/completion/completion_core.dart'
1617
import 'package:analysis_server/src/services/completion/completion_performance.dart';
1718
import 'package:analysis_server/src/services/completion/dart/completion_manager.dart';
1819
import 'package:analysis_server/src/services/completion/dart/fuzzy_filter_sort.dart';
20+
import 'package:analysis_server/src/services/completion/dart/suggestion_builder.dart';
1921
import 'package:analysis_server/src/services/completion/yaml/analysis_options_generator.dart';
2022
import 'package:analysis_server/src/services/completion/yaml/fix_data_generator.dart';
2123
import 'package:analysis_server/src/services/completion/yaml/pubspec_generator.dart';
@@ -72,7 +74,7 @@ class CompletionDomainHandler extends AbstractRequestHandler {
7274
Set<ElementKind>? includedElementKinds,
7375
Set<String>? includedElementNames,
7476
List<IncludedSuggestionRelevanceTag>? includedSuggestionRelevanceTags,
75-
List<Uri>? librariesToImport,
77+
Map<CompletionSuggestion, Uri>? notImportedSuggestions,
7678
}) async {
7779
//
7880
// Allow plugins to start computing fixes.
@@ -91,7 +93,7 @@ class CompletionDomainHandler extends AbstractRequestHandler {
9193
includedElementKinds: includedElementKinds,
9294
includedElementNames: includedElementNames,
9395
includedSuggestionRelevanceTags: includedSuggestionRelevanceTags,
94-
librariesToImport: librariesToImport,
96+
notImportedSuggestions: notImportedSuggestions,
9597
);
9698

9799
suggestions.addAll(
@@ -273,7 +275,20 @@ class CompletionDomainHandler extends AbstractRequestHandler {
273275
var provider = server.resourceProvider;
274276
var pathContext = provider.pathContext;
275277

276-
// TODO(scheglov) Support non-Dart files.
278+
if (file.endsWith('.yaml')) {
279+
final suggestions = computeYamlSuggestions(file, offset);
280+
server.sendResponse(
281+
CompletionGetSuggestions2Result(
282+
suggestions.replacementOffset,
283+
suggestions.replacementLength,
284+
suggestions.suggestions,
285+
[],
286+
false,
287+
).toResponse(request.id),
288+
);
289+
return;
290+
}
291+
277292
if (!file_paths.isDart(pathContext, file)) {
278293
server.sendResponse(
279294
CompletionGetSuggestions2Result(offset, 0, [], [], false)
@@ -324,14 +339,15 @@ class CompletionDomainHandler extends AbstractRequestHandler {
324339
);
325340
setNewRequest(completionRequest);
326341

327-
var librariesToImport = <Uri>[];
342+
var notImportedSuggestions =
343+
HashMap<CompletionSuggestion, Uri>.identity();
328344
var suggestions = <CompletionSuggestion>[];
329345
try {
330346
suggestions = await computeSuggestions(
331347
budget: budget,
332348
performance: performance,
333349
request: completionRequest,
334-
librariesToImport: librariesToImport,
350+
notImportedSuggestions: notImportedSuggestions,
335351
);
336352
} on AbortCompletion {
337353
return server.sendResponse(
@@ -358,12 +374,29 @@ class CompletionDomainHandler extends AbstractRequestHandler {
358374
var isIncomplete = lengthRestricted.length < suggestions.length;
359375
completionPerformance.suggestionCount = lengthRestricted.length;
360376

377+
// Update `libraryUriToImportIndex` for not yet imported.
378+
// Gather referenced unique libraries to import.
379+
var librariesToImport = <Uri, int>{};
380+
for (var i = 0; i < lengthRestricted.length; i++) {
381+
var suggestion = lengthRestricted[i];
382+
var libraryToImport = notImportedSuggestions[suggestion];
383+
if (libraryToImport != null) {
384+
var index = librariesToImport.putIfAbsent(
385+
libraryToImport,
386+
() => librariesToImport.length,
387+
);
388+
lengthRestricted[i] = suggestion.copyWith(
389+
libraryUriToImportIndex: CopyWithValue(index),
390+
);
391+
}
392+
}
393+
361394
server.sendResponse(
362395
CompletionGetSuggestions2Result(
363396
completionRequest.replacementOffset,
364397
completionRequest.replacementLength,
365398
lengthRestricted,
366-
librariesToImport.map((e) => '$e').toList(),
399+
librariesToImport.keys.map((e) => '$e').toList(),
367400
isIncomplete,
368401
).toResponse(request.id),
369402
);

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ class DartCompletionManager {
9090
/// suggestions, or `null` if no notification should occur.
9191
final SuggestionListener? listener;
9292

93-
/// If specified, will be filled with URIs of libraries that are not yet
94-
/// imported, but could be imported into the requested target. Corresponding
95-
/// [CompletionSuggestion] will have the import index into this list.
96-
final List<Uri>? librariesToImport;
93+
/// If specified, will be filled with suggestions and URIs from libraries
94+
/// that are not yet imported, but could be imported into the requested
95+
/// target. It is up to the client to make copies of [CompletionSuggestion]s
96+
/// with the import index property updated.
97+
final Map<protocol.CompletionSuggestion, Uri>? notImportedSuggestions;
9798

9899
/// Initialize a newly created completion manager. The parameters
99100
/// [includedElementKinds], [includedElementNames], and
@@ -105,7 +106,7 @@ class DartCompletionManager {
105106
this.includedElementNames,
106107
this.includedSuggestionRelevanceTags,
107108
this.listener,
108-
this.librariesToImport,
109+
this.notImportedSuggestions,
109110
}) : assert((includedElementKinds != null &&
110111
includedElementNames != null &&
111112
includedSuggestionRelevanceTags != null) ||
@@ -163,10 +164,11 @@ class DartCompletionManager {
163164
);
164165
}
165166

166-
final librariesToImport = this.librariesToImport;
167-
if (librariesToImport != null) {
167+
final notImportedSuggestions = this.notImportedSuggestions;
168+
if (notImportedSuggestions != null) {
168169
contributors.add(
169-
NotImportedContributor(request, builder, budget, librariesToImport),
170+
NotImportedContributor(
171+
request, builder, budget, notImportedSuggestions),
170172
);
171173
}
172174

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66

7+
import 'package:analysis_server/src/protocol_server.dart' as protocol;
78
import 'package:analysis_server/src/provisional/completion/dart/completion_dart.dart';
89
import 'package:analysis_server/src/services/completion/dart/completion_manager.dart';
910
import 'package:analysis_server/src/services/completion/dart/extension_member_contributor.dart';
@@ -25,13 +26,13 @@ class NotImportedContributor extends DartCompletionContributor {
2526
static void Function(FileState)? onFile;
2627

2728
final CompletionBudget budget;
28-
final List<Uri> librariesToImport;
29+
final Map<protocol.CompletionSuggestion, Uri> notImportedSuggestions;
2930

3031
NotImportedContributor(
3132
DartCompletionRequest request,
3233
SuggestionBuilder builder,
3334
this.budget,
34-
this.librariesToImport,
35+
this.notImportedSuggestions,
3536
) : super(request, builder);
3637

3738
@override
@@ -72,7 +73,10 @@ class NotImportedContributor extends DartCompletionContributor {
7273
var exportNamespace = elementResult.element.exportNamespace;
7374
var exportElements = exportNamespace.definedNames.values.toList();
7475

75-
var newSuggestions = builder.markSuggestions();
76+
builder.laterReplacesEarlier = false;
77+
builder.suggestionAdded = (suggestion) {
78+
notImportedSuggestions[suggestion] = file.uri;
79+
};
7680

7781
if (request.includeIdentifiers) {
7882
_buildSuggestions(exportElements);
@@ -81,12 +85,10 @@ class NotImportedContributor extends DartCompletionContributor {
8185
extensionContributor.addExtensions(
8286
exportElements.whereType<ExtensionElement>().toList(),
8387
);
84-
85-
newSuggestions.setLibraryUriToImportIndex(() {
86-
librariesToImport.add(file.uri);
87-
return librariesToImport.length - 1;
88-
});
8988
}
89+
90+
builder.laterReplacesEarlier = true;
91+
builder.suggestionAdded = null;
9092
}
9193

9294
_Filter _buildFilter(FileSystemState fsState) {

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

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,6 @@ class MemberSuggestionBuilder {
148148
}
149149
}
150150

151-
class NewSuggestionsProcessor {
152-
final SuggestionBuilder _builder;
153-
final Set<protocol.CompletionSuggestion> _current = Set.identity();
154-
155-
NewSuggestionsProcessor._(this._builder) {
156-
_current.addAll(_builder._suggestionMap.values);
157-
}
158-
159-
/// Update suggestions added since this marker was created.
160-
void setLibraryUriToImportIndex(int Function() produce) {
161-
int? libraryUriToImportIndex;
162-
var suggestionMap = _builder._suggestionMap;
163-
for (var entry in suggestionMap.entries.toList()) {
164-
var suggestion = entry.value;
165-
if (!_current.contains(suggestion)) {
166-
libraryUriToImportIndex ??= produce();
167-
suggestionMap[entry.key] = suggestion.copyWith(
168-
libraryUriToImportIndex: CopyWithValue(libraryUriToImportIndex),
169-
);
170-
}
171-
}
172-
}
173-
}
174-
175151
/// An object used to build a list of suggestions in response to a single
176152
/// completion request.
177153
class SuggestionBuilder {
@@ -198,6 +174,9 @@ class SuggestionBuilder {
198174
/// suggestions, or `null` if no notification should occur.
199175
final SuggestionListener? listener;
200176

177+
/// The function to be invoked when a new suggestion is added.
178+
void Function(protocol.CompletionSuggestion)? suggestionAdded;
179+
201180
/// A map from a completion identifier to a completion suggestion.
202181
final Map<String, CompletionSuggestion> _suggestionMap =
203182
<String, CompletionSuggestion>{};
@@ -250,11 +229,6 @@ class SuggestionBuilder {
250229
bool get _isNonNullableByDefault =>
251230
request.libraryElement.isNonNullableByDefault;
252231

253-
/// Return an object that knows which suggestions exist, and which are new.
254-
NewSuggestionsProcessor markSuggestions() {
255-
return NewSuggestionsProcessor._(this);
256-
}
257-
258232
/// Add a suggestion for an [accessor] declared within a class or extension.
259233
/// If the accessor is being invoked with a target of `super`, then the
260234
/// [containingMemberName] should be the name of the member containing the
@@ -965,10 +939,9 @@ class SuggestionBuilder {
965939
key = '$key()';
966940
}
967941
listener?.builtSuggestion(suggestion);
968-
if (laterReplacesEarlier) {
942+
if (laterReplacesEarlier || !_suggestionMap.containsKey(key)) {
969943
_suggestionMap[key] = suggestion;
970-
} else {
971-
_suggestionMap.putIfAbsent(key, () => suggestion);
944+
suggestionAdded?.call(suggestion);
972945
}
973946
}
974947
}

0 commit comments

Comments
 (0)