workspaceSymbols method

Future<Map<String, Object?>> workspaceSymbols(
  1. String query,
  2. List<Object?> files
)

Searches declarations matching query across a set of in-memory files ({uri, source, language?}), enabling codebase-wide symbol lookup.

Implementation

Future<Map<String, Object?>> workspaceSymbols(
  String query,
  List<Object?> files,
) async {
  final entries = <({String uri, String source})>[];
  var totalChars = 0;
  for (final raw in files) {
    if (raw is! Map) continue;
    final source = (raw['source'] as String?) ?? '';
    totalChars += source.length;
    if (totalChars > limits.maxSourceChars) {
      return _error(
        'Combined sources exceed maxSourceChars '
        '($totalChars > ${limits.maxSourceChars})',
      );
    }
    final language = raw['language'] as String?;
    final uri = _fileUri(raw['uri'] as String?, language, entries.length);
    entries.add((uri: uri, source: source));
  }

  final lsp = LspService();
  try {
    // Analyze each file so its symbols are cached before the query.
    for (final e in entries) {
      await lsp.analyze(e.uri, e.source);
    }
    final symbols = await lsp.workspaceSymbols(query);
    return <String, Object?>{
      'symbols': symbols.map((s) => s.toJson()).toList(),
      'files': entries.map((e) => e.uri).toList(),
      'isError': false,
    };
  } finally {
    await lsp.dispose();
  }
}