reload method

Future<void> reload()

Reloads the analyzer.

This is only needed to be called when a dependency has changed. The analyzer groups packages into their own contexts. Because of this, dependencies are only resolved and cached when the analyzer is initialized.

This is relatively expensive, so it should only be called when necessary.

Implementation

Future<void> reload() async {
  final dependencies = switch (_packageConfig) {
    final String packageConfig => await _getDependencyFiles(
      packageConfig,
      pathDependenciesOnly: true,
      directoryOnly: true,
    ),
    null => <String>[],
  };

  final root = _root;
  if (root == null) {
    throw Exception('No root found');
  }

  await refreshDependencies();

  final old = _analysisCollection;

  _analysisCollection = AnalysisContextCollection(
    includedPaths: [root, ...dependencies],
    resourceProvider: _memoryProvider,
    sdkPath: await sdkPath,
  );

  await old?.dispose();
}