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(plugin.ContextRoot contextRoot) {
  final rootPath = contextRoot.root;
  final locator =
      ContextLocator(resourceProvider: resourceProvider).locateRoots(
    includedPaths: <String>[rootPath],
    excludedPaths: <String>[
      ...contextRoot.exclude,
    ],
    optionsFile: contextRoot.optionsFile,
  );
  if (locator.isEmpty) {
    final error = StateError('Unexpected empty context');
    channel.sendNotification(plugin.PluginErrorParams(
      true,
      error.message,
      error.stackTrace.toString(),
    ).toNotification());

    throw error;
  }

  final builder = ContextBuilder(
    resourceProvider: resourceProvider,
  );

  final analysisContext = builder.createContext(contextRoot: locator.first);
  // ignore: avoid_as
  final context = analysisContext as DriverBasedAnalysisContext;
  final dartDriver = context.driver;
  try {
    final analysisOptions =
        (_options[dartDriver] ??= _getAnalysisOptions(dartDriver));

    runZonedGuarded(
      () {
        dartDriver.results.listen((analysisResult) {
          if (analysisResult is ResolvedUnitResult) {
            _processResult(
              dartDriver,
              analysisOptions,
              analysisResult,
            );
          } else if (analysisResult is ErrorsResult) {
            _logger
                .severe('analysis produce errors. ${analysisResult.errors}');
          }
        });
      },
      (Object e, StackTrace stackTrace) {
        channel.sendNotification(
          plugin.PluginErrorParams(
            false,
            'pt_linter. Unexpected error: ${e.toString()}',
            stackTrace.toString(),
          ).toNotification(),
        );
      },
    );
  } catch (e, stackTrace) {
    channel.sendNotification(
      plugin.PluginErrorParams(
        false,
        'pt_linter. Unexpected error: ${e.toString()}',
        stackTrace.toString(),
      ).toNotification(),
    );
  }

  return dartDriver;
}