createAnalysisDriver method

  1. @override
AnalysisDriverGeneric createAnalysisDriver(
  1. ContextRoot contextRoot
)
override

Create an analysis driver that can analyze the files within the given contextRoot.

Implementation

@override
AnalysisDriverGeneric createAnalysisDriver(ContextRoot contextRoot) {
  final rootPath = contextRoot.root;
  final locator =
      ContextLocator(resourceProvider: resourceProvider).locateRoots(
    includedPaths: [rootPath],
    excludedPaths: contextRoot.exclude,
    optionsFile: contextRoot.optionsFile,
  );

  if (locator.isEmpty) {
    final error = StateError('Unexpected empty context');
    channel.sendNotification(PluginErrorParams(
      true,
      error.message,
      error.stackTrace.toString(),
    ).toNotification());

    throw error;
  }

  final builder = ContextBuilder(resourceProvider: resourceProvider);
  final context = builder.createContext(contextRoot: locator.first)
      as DriverBasedAnalysisContext;
  final dartDriver = context.driver;
  final config = _createConfig(dartDriver, rootPath);

  if (config == null) {
    return dartDriver;
  }

  runZonedGuarded<void>(
    () {
      dartDriver.results.listen((analysisResult) {
        if (analysisResult is ResolvedUnitResult) {
          _processResult(dartDriver, analysisResult);
        }
      });
    },
    (e, stackTrace) {
      channel.sendNotification(
        PluginErrorParams(false, e.toString(), stackTrace.toString())
            .toNotification(),
      );
    },
  );

  return dartDriver;
}