Future analyze()

in lib/src/analysis_server.dart [270:326]


  Future<proto.AnalysisResults> analyze(String source) {
    var sources = <String, String>{kMainDart: source};

    _logger.fine('analyze: Scheduler queue: ${serverScheduler.queueCount}');

    return serverScheduler
        .schedule(ClosureTask<proto.AnalysisResults>(() async {
      sources = _getOverlayMapWithPaths(sources);
      await _loadSources(sources);
      final errors = (await analysisServer.analysis.getErrors(mainPath)).errors;
      await _unloadSources();

      // Convert the issues to protos.
      final issues = errors.map((error) {
        final issue = proto.AnalysisIssue()
          ..kind = error.severity.toLowerCase()
          ..line = error.location.startLine
          ..message = utils.normalizeFilePaths(error.message)
          ..sourceName = path.basename(error.location.file)
          ..hasFixes = error.hasFix ?? false
          ..charStart = error.location.offset
          ..charLength = error.location.length
          ..diagnosticMessages.addAll(error.contextMessages?.map((m) =>
                  proto.DiagnosticMessage(
                      message: utils.normalizeFilePaths(m.message),
                      line: m.location.startLine,
                      charStart: m.location.offset,
                      charLength: m.location.length)) ??
              []);

        if (error.url != null) {
          issue.url = error.url!;
        }

        if (error.correction != null) {
          issue.correction = utils.normalizeFilePaths(error.correction!);
        }

        return issue;
      }).toList();

      issues.sort((a, b) {
        // Order issues by character position of the bug/warning.
        return a.charStart.compareTo(b.charStart);
      });

      // Calculate the imports.
      final packageImports = {
        for (final source in sources.values)
          ...getAllImportsFor(source).filterSafePackages(),
      };

      return proto.AnalysisResults()
        ..issues.addAll(issues)
        ..packageImports.addAll(packageImports);
    }, timeoutDuration: _analysisServerTimeout));
  }