updateFileContexts method

Future<bool> updateFileContexts(
  1. Set<String> filePaths
)

Inform the analyzer that the provided filePaths have been updated.

Refreshes the Dart analysis context for the changed files and returns true if any of them are (or were) future call files, meaning code generation should run.

Implementation

Future<bool> updateFileContexts(Set<String> filePaths) async {
  // Only consider files within the tracked directory.
  final relevantPaths = filePaths
      .where((f) => p.isWithin(absoluteIncludedPaths, p.absolute(f)))
      .toSet();

  final erroredFilesBefore = _erroredFiles;
  final keysBefore = _fileCache.keys.toSet();

  await analyze(
    collector: CodeGenerationCollector(),
    changedFiles: relevantPaths,
  );

  final erroredFilesAfter = _erroredFiles;
  final keysAfter = _fileCache.keys.toSet();

  // Regenerate when the set of future call files changed, or when any
  // file's error state flipped (an error appearing or clearing changes the
  // parsed definitions). A persistently broken - or not yet fully analyzed
  // (see `hadErrors: !hasModels`) - file, by contrast, must not turn every
  // unrelated change - such as watcher echoes of freshly generated files -
  // into another generation, or generation loops forever while an error
  // exists anywhere in the project.
  if (keysBefore.length != keysAfter.length ||
      keysAfter.difference(keysBefore).isNotEmpty ||
      erroredFilesBefore.length != erroredFilesAfter.length ||
      erroredFilesAfter.difference(erroredFilesBefore).isNotEmpty) {
    return true;
  }

  for (final path in relevantPaths) {
    if (!path.endsWith('.dart') || path.endsWith('_test.dart')) continue;
    if (_fileCache.containsKey(path)) return true;
    if (_isFutureCallFile(File(path))) return true;
  }

  return false;
}