Skip to content

Commit

Permalink
DAS: Simplify fields in AnalysisServer
Browse files Browse the repository at this point in the history
Many fields in this class are initialized in the class body, which is
unfortunate but seems necessary. However, such fields can often be
marked final, which makes the life cycle of such fields more clear:
contextManager, analysisDriverScheduler, pubPackageService.

Also, searchEngine can be a late final field wtih an initializer.

The analysisPerformanceLogger field is unused outside of the
constructor body in which it is declared; can be deleted.

isFirstAnalysisSinceContextsBuilt can be made private.

`_getByteStorePath` is also moved out to be an extension getter; it was
an instance method, but only uses the one OverlayResourceProvider.
Logically the function is just extracting a value from an
OverlayResourceProvider.

Change-Id: Ib08693ef16221560a1f14279725d9d44f9f5f99c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403920
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
  • Loading branch information
srawlins authored and Commit Queue committed Jan 9, 2025
1 parent 7cbec89 commit 7d5f0a6
Showing 1 changed file with 36 additions and 46 deletions.
82 changes: 36 additions & 46 deletions pkg/analysis_server/lib/src/analysis_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ abstract class AnalysisServer {

/// The [ContextManager] that handles the mapping from analysis roots to
/// context directories.
late ContextManager contextManager;
late final ContextManager contextManager;

/// The object used to manage sending a subset of notifications to the client.
/// The subset of notifications are those to which plugins may contribute.
Expand All @@ -154,7 +154,7 @@ abstract class AnalysisServer {
final DartSdkManager sdkManager;

/// The [SearchEngine] for this server.
late final SearchEngine searchEngine;
late final SearchEngine searchEngine = SearchEngineImpl(driverMap.values);

/// The optional [ByteStore] to use as [byteStore].
final ByteStore? providedByteStore;
Expand All @@ -167,7 +167,7 @@ abstract class AnalysisServer {

final InfoDeclarationStore infoDeclarationStore = InfoDeclarationStoreImpl();

late analysis.AnalysisDriverScheduler analysisDriverScheduler;
late final analysis.AnalysisDriverScheduler analysisDriverScheduler;

late StreamSubscription<Object?>? analysisDriverSchedulerEventsSubscription;

Expand All @@ -191,7 +191,7 @@ abstract class AnalysisServer {
final PubApi pubApi;

/// A service for fetching pub.dev package details.
late PubPackageService pubPackageService;
late final PubPackageService pubPackageService;

/// The class into which performance information is currently being recorded.
/// During startup, this will be the same as [performanceDuringStartup]
Expand All @@ -204,9 +204,7 @@ abstract class AnalysisServer {
/// Performance about recent requests.
final ServerRecentPerformance recentPerformance = ServerRecentPerformance();

RequestStatisticsHelper? requestStatistics;

PerformanceLog? analysisPerformanceLogger;
final RequestStatisticsHelper? requestStatistics;

/// Manages prompts telling the user about "dart fix".
late final DartFixPromptManager _dartFixPrompt;
Expand Down Expand Up @@ -237,7 +235,7 @@ abstract class AnalysisServer {
/// This is useful for triggering things like [_dartFixPrompt] only when
/// it may be useful to do so (after initial analysis and
/// "pub get"/"pub upgrades").
bool isFirstAnalysisSinceContextsBuilt = true;
bool _isFirstAnalysisSinceContextsBuilt = true;

/// A completer that tracks in-progress analysis context rebuilds.
///
Expand Down Expand Up @@ -333,7 +331,7 @@ abstract class AnalysisServer {
this.pluginManager =
pluginManager ??= PluginManager(
resourceProvider,
_getByteStorePath(),
resourceProvider.byteStorePath,
sdkManager.defaultSdkDirectory,
notificationManager,
instrumentationService,
Expand All @@ -342,22 +340,22 @@ abstract class AnalysisServer {
pluginWatcher = PluginWatcher(resourceProvider, pluginManager);
}

var name = options.newAnalysisDriverLog;
var logName = options.newAnalysisDriverLog;
StringSink sink = NullStringSink();
if (name != null) {
if (name == 'stdout') {
if (logName != null) {
if (logName == 'stdout') {
sink = io.stdout;
} else if (name.startsWith('file:')) {
var path = name.substring('file:'.length);
} else if (logName.startsWith('file:')) {
var path = logName.substring('file:'.length);
sink = FileStringSink(path);
}
}

var requestStatistics = this.requestStatistics;
if (requestStatistics != null) {
sink = TeeStringSink(sink, requestStatistics.perfLoggerStringSink);
}
var analysisPerformanceLogger =
this.analysisPerformanceLogger = PerformanceLog(sink);
var analysisPerformanceLogger = PerformanceLog(sink);

byteStore = createByteStore(resourceProvider);
fileContentCache = FileContentCache(resourceProvider);
Expand All @@ -381,17 +379,13 @@ abstract class AnalysisServer {
instrumentationService,
enableBlazeWatcher: enableBlazeWatcher,
);
searchEngine = SearchEngineImpl(driverMap.values);

if (dartFixPromptManager != null) {
_dartFixPrompt = dartFixPromptManager;
} else {
var promptPreferences = UserPromptPreferences(
resourceProvider,
instrumentationService,
);
_dartFixPrompt = DartFixPromptManager(this, promptPreferences);
}

_dartFixPrompt =
dartFixPromptManager ??
DartFixPromptManager(
this,
UserPromptPreferences(resourceProvider, instrumentationService),
);
}

/// A [Future] that completes when any in-progress analysis context rebuild
Expand Down Expand Up @@ -494,7 +488,7 @@ abstract class AnalysisServer {

void afterContextsCreated() {
_timingByteStore?.newTimings('after contexts created');
isFirstAnalysisSinceContextsBuilt = true;
_isFirstAnalysisSinceContextsBuilt = true;
}

void afterContextsDestroyed() {}
Expand Down Expand Up @@ -863,9 +857,9 @@ abstract class AnalysisServer {

@mustCallSuper
FutureOr<void> handleAnalysisStatusChange(analysis.AnalysisStatus status) {
if (isFirstAnalysisSinceContextsBuilt && !status.isWorking) {
if (_isFirstAnalysisSinceContextsBuilt && !status.isWorking) {
_timingByteStore?.newTimings('initial analysis completed');
isFirstAnalysisSinceContextsBuilt = false;
_isFirstAnalysisSinceContextsBuilt = false;
_dartFixPrompt.triggerCheck();
}
}
Expand Down Expand Up @@ -1080,22 +1074,6 @@ abstract class AnalysisServer {
await contextManager.dispose();
await analyticsManager.shutdown();
}

/// Return the path to the location of the byte store on disk, or `null` if
/// there is no on-disk byte store.
String? _getByteStorePath() {
ResourceProvider provider = resourceProvider;
if (provider is OverlayResourceProvider) {
provider = provider.baseProvider;
}
if (provider is PhysicalResourceProvider) {
var stateLocation = provider.getStateLocation('.analysis-driver');
if (stateLocation != null) {
return stateLocation.path;
}
}
return null;
}
}

/// ContextManager callbacks that operate on the base server regardless
Expand Down Expand Up @@ -1268,3 +1246,15 @@ class ServerRecentPerformance {
slowRequestsListMaxLength,
);
}

extension on OverlayResourceProvider {
/// The path to the location of the byte store on disk, or `null` if there is
/// no on-disk byte store.
String? get byteStorePath {
if (baseProvider is PhysicalResourceProvider) {
var stateLocation = baseProvider.getStateLocation('.analysis-driver');
return stateLocation?.path;
}
return null;
}
}

0 comments on commit 7d5f0a6

Please sign in to comment.