formatFindReferencesResult function

String formatFindReferencesResult(
  1. List<LspLocation>? locations, {
  2. String? cwd,
})

Format findReferences result.

Implementation

String formatFindReferencesResult(List<LspLocation>? locations, {String? cwd}) {
  if (locations == null || locations.isEmpty) {
    return 'No references found. This may occur if the symbol has no usages, '
        'or if the LSP server has not fully indexed the workspace.';
  }

  final valid = locations.where((l) => l.uri.isNotEmpty).toList();
  if (valid.isEmpty) {
    return 'No references found. This may occur if the symbol has no usages, '
        'or if the LSP server has not fully indexed the workspace.';
  }

  if (valid.length == 1) {
    return 'Found 1 reference:\n  ${formatLocation(valid[0], cwd: cwd)}';
  }

  final byFile = groupByFile(valid, (l) => l.uri, cwd: cwd);
  final lines = <String>[
    'Found ${valid.length} references across ${byFile.length} files:',
  ];

  for (final entry in byFile.entries) {
    lines.add('\n${entry.key}:');
    for (final loc in entry.value) {
      final line = loc.range.start.line + 1;
      final character = loc.range.start.character + 1;
      lines.add('  Line $line:$character');
    }
  }

  return lines.join('\n');
}