updateImportStatements method

  1. @visibleForTesting
Future<void> updateImportStatements(
  1. ResolvedReferences import, {
  2. Config? config,
})

Implementation

@visibleForTesting
Future<void> updateImportStatements(
  ResolvedReferences import, {
  Config? config,
}) async {
  final _config = config ?? Config();
  final ResolvedReferences(:path, :imports, :hasImports) = import;

  if (path == null) return;
  if (!hasImports) return;

  final lines = fs.file(path).readAsLinesSync();

  final (importStart: importStart, importEnd: importEnd) =
      findImportBlockIndices(lines);

  var start = importStart;
  if (start == null) {
    if (importEnd != null) {
      start = importEnd;
    } else {
      log('No import start found for ${fs.path.relative(path)}');
      return;
    }
  }

  final end = importEnd;
  if (end == null) {
    log('No import end found for ${fs.path.relative(path)}');
    return;
  }

  final contentStart = lines.take(start).join('\n');
  final contentEnd = _removeDartFormatCommentsBetweenParts(lines.sublist(end));

  final (:dart, :relative, :package) = imports(
    trailComments: !_config.format,
  );

  final importStatements = [
    if (dart.isNotEmpty) ...dart.map((e) => '$e').followedBy(['']),
    if (package.isNotEmpty) ...package.map((e) => '$e').followedBy(['']),
    if (relative.isNotEmpty) ...relative.map((e) => '$e').followedBy(['']),
  ];

  if (_config case Config(format: false) when importStatements.isNotEmpty) {
    importStatements.insert(0, '// dart format off');
    // leave the last line empty
    importStatements.insert(importStatements.length - 1, '// dart format on');
  }

  String? startContent = null;

  if (contentStart.trim() case final String start when start.isNotEmpty) {
    final lines = start.split('\n');
    for (final (index, line) in lines.reversed.indexed) {
      if (line.trim().isEmpty) {
        continue;
      }

      if (line.trim() == '// dart format off') {
        continue;
      }

      final reversedIndex = lines.length - index;

      startContent = lines.sublist(0, reversedIndex).join('\n');
      break;
    }
  }

  var content = [
    if (startContent?.trim() case final String start when start.isNotEmpty)
      '$start\n',
    ...importStatements,
    contentEnd.trimRight(),
    '',
  ].join('\n');

  fs.file(path).writeAsStringSync(content.trimLeft());
}