Skip to content

Commit 1588eca

Browse files
author
Dart CI
committed
Version 2.14.0-374.0.dev
Merge commit 'af50a684d12a69fc90a06e394a37adfb957abaa9' into 'dev'
2 parents 69f0ebc + af50a68 commit 1588eca

33 files changed

+953
-143
lines changed

pkg/analysis_server/lib/src/analysis_server.dart

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import 'package:analysis_server/src/server/error_notifier.dart';
4040
import 'package:analysis_server/src/server/features.dart';
4141
import 'package:analysis_server/src/server/sdk_configuration.dart';
4242
import 'package:analysis_server/src/services/flutter/widget_descriptions.dart';
43+
import 'package:analysis_server/src/utilities/process.dart';
4344
import 'package:analysis_server/src/utilities/request_statistics.dart';
4445
import 'package:analyzer/dart/analysis/results.dart';
4546
import 'package:analyzer/dart/ast/ast.dart';
@@ -130,6 +131,7 @@ class AnalysisServer extends AbstractAnalysisServer {
130131
CrashReportingAttachmentsBuilder crashReportingAttachmentsBuilder,
131132
InstrumentationService instrumentationService, {
132133
http.Client? httpClient,
134+
ProcessRunner? processRunner,
133135
RequestStatisticsHelper? requestStatistics,
134136
DiagnosticServer? diagnosticServer,
135137
this.detachableFileSystemManager,
@@ -143,6 +145,7 @@ class AnalysisServer extends AbstractAnalysisServer {
143145
baseResourceProvider,
144146
instrumentationService,
145147
httpClient,
148+
processRunner,
146149
NotificationManager(channel, baseResourceProvider.pathContext),
147150
requestStatistics: requestStatistics,
148151
enableBazelWatcher: enableBazelWatcher,
@@ -429,9 +432,11 @@ class AnalysisServer extends AbstractAnalysisServer {
429432
bool isPubspec(String filePath) =>
430433
file_paths.isPubspecYaml(resourceProvider.pathContext, filePath);
431434

432-
// When a pubspec is opened, trigger package name caching for completion.
433-
if (!pubPackageService.isRunning && files.any(isPubspec)) {
434-
pubPackageService.beginPackageNamePreload();
435+
// When pubspecs are opened, trigger pre-loading of pub package names and
436+
// versions.
437+
final pubspecs = files.where(isPubspec).toList();
438+
if (pubspecs.isNotEmpty) {
439+
pubPackageService.beginCachePreloads(pubspecs);
435440
}
436441

437442
priorityFiles.clear();
@@ -687,6 +692,18 @@ class ServerContextManagerCallbacks extends ContextManagerCallbacks {
687692
analysisDriver.priorityFiles = analysisServer.priorityFiles.toList();
688693
}
689694

695+
@override
696+
void pubspecChanged(String pubspecPath) {
697+
analysisServer.pubPackageService.fetchPackageVersionsViaPubOutdated(
698+
pubspecPath,
699+
pubspecWasModified: true);
700+
}
701+
702+
@override
703+
void pubspecRemoved(String pubspecPath) {
704+
analysisServer.pubPackageService.flushPackageCaches(pubspecPath);
705+
}
706+
690707
@override
691708
void recordAnalysisErrors(String path, List<AnalysisError> errors) {
692709
filesToFlush.add(path);

pkg/analysis_server/lib/src/analysis_server_abstract.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ import 'package:analysis_server/src/services/completion/dart/documentation_cache
1919
import 'package:analysis_server/src/services/completion/dart/extension_cache.dart';
2020
import 'package:analysis_server/src/services/correction/namespace.dart';
2121
import 'package:analysis_server/src/services/pub/pub_api.dart';
22+
import 'package:analysis_server/src/services/pub/pub_command.dart';
2223
import 'package:analysis_server/src/services/pub/pub_package_service.dart';
2324
import 'package:analysis_server/src/services/search/element_visitors.dart';
2425
import 'package:analysis_server/src/services/search/search_engine.dart';
2526
import 'package:analysis_server/src/services/search/search_engine_internal.dart';
2627
import 'package:analysis_server/src/utilities/file_string_sink.dart';
2728
import 'package:analysis_server/src/utilities/null_string_sink.dart';
29+
import 'package:analysis_server/src/utilities/process.dart';
2830
import 'package:analysis_server/src/utilities/request_statistics.dart';
2931
import 'package:analysis_server/src/utilities/tee_string_sink.dart';
3032
import 'package:analyzer/dart/analysis/analysis_context.dart';
@@ -152,14 +154,26 @@ abstract class AbstractAnalysisServer {
152154
ResourceProvider baseResourceProvider,
153155
this.instrumentationService,
154156
http.Client? httpClient,
157+
ProcessRunner? processRunner,
155158
this.notificationManager, {
156159
this.requestStatistics,
157160
bool enableBazelWatcher = false,
158161
}) : resourceProvider = OverlayResourceProvider(baseResourceProvider),
159162
pubApi = PubApi(instrumentationService, httpClient,
160163
Platform.environment['PUB_HOSTED_URL']) {
161-
pubPackageService =
162-
PubPackageService(instrumentationService, baseResourceProvider, pubApi);
164+
// We can only spawn processes (eg. to run pub commands) when backed by
165+
// a real file system, otherwise we may try to run commands in folders that
166+
// don't really exist. If processRunner was supplied, it's likely a mock
167+
// from a test in which case the pub command should still be created.
168+
if (baseResourceProvider is PhysicalResourceProvider) {
169+
processRunner ??= ProcessRunner();
170+
}
171+
final pubCommand = processRunner != null
172+
? PubCommand(instrumentationService, processRunner)
173+
: null;
174+
175+
pubPackageService = PubPackageService(
176+
instrumentationService, baseResourceProvider, pubApi, pubCommand);
163177
performance = performanceDuringStartup;
164178

165179
pluginManager = PluginManager(

pkg/analysis_server/lib/src/context_manager.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ abstract class ContextManagerCallbacks {
122122
/// TODO(scheglov) Just pass results in here?
123123
void listenAnalysisDriver(AnalysisDriver driver);
124124

125+
/// The `pubspec.yaml` at [path] was added/modified.
126+
void pubspecChanged(String path);
127+
128+
/// The `pubspec.yaml` at [path] was removed.
129+
void pubspecRemoved(String path);
130+
125131
/// Record error information for the file with the given [path].
126132
void recordAnalysisErrors(String path, List<protocol.AnalysisError> errors);
127133
}
@@ -566,12 +572,21 @@ class ContextManagerImpl implements ContextManager {
566572

567573
_instrumentationService.logWatchEvent('<unknown>', path, type.toString());
568574

575+
final isPubpsec = file_paths.isPubspecYaml(pathContext, path);
569576
if (file_paths.isAnalysisOptionsYaml(pathContext, path) ||
570577
file_paths.isDotPackages(pathContext, path) ||
571578
file_paths.isPackageConfigJson(pathContext, path) ||
572-
file_paths.isPubspecYaml(pathContext, path) ||
579+
isPubpsec ||
573580
false) {
574581
_createAnalysisContexts();
582+
583+
if (isPubpsec) {
584+
if (type == ChangeType.REMOVE) {
585+
callbacks.pubspecRemoved(path);
586+
} else {
587+
callbacks.pubspecChanged(path);
588+
}
589+
}
575590
return;
576591
}
577592

@@ -726,6 +741,12 @@ class NoopContextManagerCallbacks implements ContextManagerCallbacks {
726741
@override
727742
void listenAnalysisDriver(AnalysisDriver driver) {}
728743

744+
@override
745+
void pubspecChanged(String pubspecPath) {}
746+
747+
@override
748+
void pubspecRemoved(String pubspecPath) {}
749+
729750
@override
730751
void recordAnalysisErrors(String path, List<protocol.AnalysisError> errors) {}
731752
}

pkg/analysis_server/lib/src/lsp/lsp_analysis_server.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import 'package:analysis_server/src/server/error_notifier.dart';
3535
import 'package:analysis_server/src/services/completion/completion_performance.dart'
3636
show CompletionPerformance;
3737
import 'package:analysis_server/src/services/refactoring/refactoring.dart';
38+
import 'package:analysis_server/src/utilities/process.dart';
3839
import 'package:analyzer/dart/analysis/context_locator.dart';
3940
import 'package:analyzer/dart/analysis/results.dart';
4041
import 'package:analyzer/error/error.dart';
@@ -126,6 +127,7 @@ class LspAnalysisServer extends AbstractAnalysisServer {
126127
CrashReportingAttachmentsBuilder crashReportingAttachmentsBuilder,
127128
InstrumentationService instrumentationService, {
128129
http.Client? httpClient,
130+
ProcessRunner? processRunner,
129131
DiagnosticServer? diagnosticServer,
130132
// Disable to avoid using this in unit tests.
131133
bool enableBazelWatcher = false,
@@ -137,6 +139,7 @@ class LspAnalysisServer extends AbstractAnalysisServer {
137139
baseResourceProvider,
138140
instrumentationService,
139141
httpClient,
142+
processRunner,
140143
LspNotificationManager(channel, baseResourceProvider.pathContext),
141144
enableBazelWatcher: enableBazelWatcher,
142145
) {
@@ -184,10 +187,10 @@ class LspAnalysisServer extends AbstractAnalysisServer {
184187
RefactoringWorkspace(driverMap.values, searchEngine);
185188

186189
void addPriorityFile(String filePath) {
187-
// When a pubspec is opened, trigger package name caching for completion.
188-
if (!pubPackageService.isRunning &&
189-
file_paths.isPubspecYaml(resourceProvider.pathContext, filePath)) {
190-
pubPackageService.beginPackageNamePreload();
190+
// When pubspecs are opened, trigger pre-loading of pub package names and
191+
// versions.
192+
if (file_paths.isPubspecYaml(resourceProvider.pathContext, filePath)) {
193+
pubPackageService.beginCachePreloads([filePath]);
191194
}
192195

193196
final didAdd = priorityFiles.add(filePath);
@@ -851,6 +854,18 @@ class LspServerContextManagerCallbacks extends ContextManagerCallbacks {
851854
analysisDriver.priorityFiles = analysisServer.priorityFiles.toList();
852855
}
853856

857+
@override
858+
void pubspecChanged(String pubspecPath) {
859+
analysisServer.pubPackageService.fetchPackageVersionsViaPubOutdated(
860+
pubspecPath,
861+
pubspecWasModified: true);
862+
}
863+
864+
@override
865+
void pubspecRemoved(String pubspecPath) {
866+
analysisServer.pubPackageService.flushPackageCaches(pubspecPath);
867+
}
868+
854869
@override
855870
void recordAnalysisErrors(String path, List<protocol.AnalysisError> errors) {
856871
final errorsToSend = errors.where(_shouldSendError).toList();

pkg/analysis_server/lib/src/lsp/mapping.dart

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ final diagnosticTagsForErrorCode = <String, List<lsp.DiagnosticTag>>{
4949
],
5050
};
5151

52+
/// Pattern for docComplete text on completion items that can be upgraded to
53+
/// the "detail" field so that it can be shown more prominently by clients.
54+
///
55+
/// This is typically used for labels like _latest compatible_ and _latest_ in
56+
/// the pubspec version items. These go into docComplete so that they appear
57+
/// reasonably for non-LSP clients where there is no equivalent of the detail
58+
/// field.
59+
final _upgradableDocCompletePattern = RegExp(r'^_([\w ]{0,20})_$');
60+
5261
lsp.Either2<String, lsp.MarkupContent> asStringOrMarkupContent(
5362
Set<lsp.MarkupKind>? preferredFormats, String content) {
5463
return preferredFormats == null
@@ -967,7 +976,21 @@ lsp.CompletionItem toCompletionItem(
967976
final insertText = insertTextInfo.first;
968977
final insertTextFormat = insertTextInfo.last;
969978
final isMultilineCompletion = insertText.contains('\n');
970-
final cleanedDoc = cleanDartdoc(suggestion.docComplete);
979+
980+
var cleanedDoc = cleanDartdoc(suggestion.docComplete);
981+
var detail = getCompletionDetail(suggestion, completionKind,
982+
supportsCompletionDeprecatedFlag || supportsDeprecatedTag);
983+
984+
// To improve the display of some items (like pubspec version numbers),
985+
// short labels in the format `_foo_` in docComplete are "upgraded" to the
986+
// detail field.
987+
final labelMatch = cleanedDoc != null
988+
? _upgradableDocCompletePattern.firstMatch(cleanedDoc)
989+
: null;
990+
if (labelMatch != null) {
991+
cleanedDoc = null;
992+
detail = labelMatch.group(1);
993+
}
971994

972995
// Because we potentially send thousands of these items, we should minimise
973996
// the generated JSON as much as possible - for example using nulls in place
@@ -982,8 +1005,7 @@ lsp.CompletionItem toCompletionItem(
9821005
commitCharacters:
9831006
includeCommitCharacters ? dartCompletionCommitCharacters : null,
9841007
data: resolutionData,
985-
detail: getCompletionDetail(suggestion, completionKind,
986-
supportsCompletionDeprecatedFlag || supportsDeprecatedTag),
1008+
detail: detail,
9871009
documentation: cleanedDoc != null
9881010
? asStringOrMarkupContent(formats, cleanedDoc)
9891011
: null,

pkg/analysis_server/lib/src/services/completion/yaml/producer.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,11 @@ abstract class Producer {
176176
const Producer();
177177

178178
/// A utility method used to create a suggestion for the [identifier].
179-
CompletionSuggestion identifier(String identifier, {int relevance = 1000}) =>
179+
CompletionSuggestion identifier(String identifier,
180+
{int relevance = 1000, String? docComplete}) =>
180181
CompletionSuggestion(CompletionSuggestionKind.IDENTIFIER, relevance,
181-
identifier, identifier.length, 0, false, false);
182+
identifier, identifier.length, 0, false, false,
183+
docComplete: docComplete);
182184

183185
/// A utility method used to create a suggestion for the package [packageName].
184186
CompletionSuggestion packageName(String packageName,

pkg/analysis_server/lib/src/services/completion/yaml/pubspec_generator.dart

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,25 @@ class PubPackageVersionProducer extends Producer {
3737
@override
3838
Iterable<CompletionSuggestion> suggestions(
3939
YamlCompletionRequest request) sync* {
40-
// TOOD(dantup): Consider supporting async completion requests so this
41-
// could call packageDetails() (with a short timeout, and pub retries
42-
// disabled). A user that explicitly invokes completion in the location
43-
// of a version may be prepared to wait a short period for a web request
44-
// to get completion versions (this is also the only way for non-LSP
45-
// clients to get them, since there are no resolve calls).
40+
final versions = request.pubPackageService
41+
?.cachedPubOutdatedVersions(request.filePath, package);
42+
final resolvable = versions?.resolvableVersion;
43+
var latest = versions?.latestVersion;
44+
45+
// If we didn't get a latest version from the "pub outdated" results, we can
46+
// use the result from the Pub API if we've called it (this will usually
47+
// only be the case for LSP where a resolve() call was sent).
4648
//
47-
// Supporting this will require making the completion async further up.
48-
final details = request.pubPackageService?.cachedPackageDetails(package);
49-
final version = details?.latestVersion;
50-
if (version != null) {
51-
yield identifier('^$version');
49+
// This allows us (in some cases) to still show version numbers even if the
50+
// package was newly added to pubspec and not saved, so not yet in the
51+
// "pub outdated" results.
52+
latest ??= request.pubPackageService?.cachedPubApiLatestVersion(package);
53+
54+
if (resolvable != null && resolvable != latest) {
55+
yield identifier('^$resolvable', docComplete: '_latest compatible_');
56+
}
57+
if (latest != null) {
58+
yield identifier('^$latest', docComplete: '_latest_');
5259
}
5360
}
5461
}

0 commit comments

Comments
 (0)