formatWorkspaceSymbolResult function

String formatWorkspaceSymbolResult(
  1. List<LspSymbolInformation>? symbols, {
  2. String? cwd,
})

Format workspaceSymbol result (flat list).

Implementation

String formatWorkspaceSymbolResult(
  List<LspSymbolInformation>? symbols, {
  String? cwd,
}) {
  if (symbols == null || symbols.isEmpty) {
    return 'No symbols found in workspace. This may occur if the workspace is '
        'empty, or if the LSP server has not finished indexing the project.';
  }

  final valid = symbols.where((s) => s.location.uri.isNotEmpty).toList();
  if (valid.isEmpty) {
    return 'No symbols found in workspace. This may occur if the workspace is '
        'empty, or if the LSP server has not finished indexing the project.';
  }

  final plural = valid.length == 1 ? 'symbol' : 'symbols';
  final lines = <String>['Found ${valid.length} $plural in workspace:'];

  final byFile = groupByFile(valid, (s) => s.location.uri, cwd: cwd);
  for (final entry in byFile.entries) {
    lines.add('\n${entry.key}:');
    for (final symbol in entry.value) {
      final kind = symbolKindToString(symbol.kind);
      final line = symbol.location.range.start.line + 1;
      var symbolLine = '  ${symbol.name} ($kind) - Line $line';
      if (symbol.containerName != null) {
        symbolLine += ' in ${symbol.containerName}';
      }
      lines.add(symbolLine);
    }
  }

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