computeLspTool function

Future<Map<String, Object?>> computeLspTool(
  1. String name,
  2. Map<String, Object?> args,
  3. McpLimits limits
)

Runs the LSP tool named name with args and returns its JSON result map (always including an isError flag). Pure with respect to the host, so it runs identically in-process or inside a spawned isolate.

Implementation

Future<Map<String, Object?>> computeLspTool(
  String name,
  Map<String, Object?> args,
  McpLimits limits,
) async {
  final rt = LspRuntime(limits: limits);

  switch (name) {
    case lspDiagnosticsToolName:
      return rt.diagnostics(
        _str(args, 'language'),
        _str(args, 'source'),
        uri: args['uri'] as String?,
      );

    case lspSymbolsToolName:
      return rt.documentSymbols(
        _str(args, 'language'),
        _str(args, 'source'),
        uri: args['uri'] as String?,
      );

    case lspHoverToolName:
      return rt.hover(
        _str(args, 'language'),
        _str(args, 'source'),
        _int(args, 'line'),
        _int(args, 'character'),
        uri: args['uri'] as String?,
      );

    case lspDefinitionToolName:
      return rt.definition(
        _str(args, 'language'),
        _str(args, 'source'),
        _int(args, 'line'),
        _int(args, 'character'),
        uri: args['uri'] as String?,
      );

    case lspReferencesToolName:
      return rt.references(
        _str(args, 'language'),
        _str(args, 'source'),
        _int(args, 'line'),
        _int(args, 'character'),
        includeDeclaration: (args['includeDeclaration'] as bool?) ?? true,
        uri: args['uri'] as String?,
      );

    case lspCompletionToolName:
      return rt.completion(
        _str(args, 'language'),
        _str(args, 'source'),
        _int(args, 'line'),
        _int(args, 'character'),
        uri: args['uri'] as String?,
      );

    case lspWorkspaceSymbolsToolName:
      return rt.workspaceSymbols(
        _str(args, 'query'),
        (args['files'] as List?)?.cast<Object?>() ?? const [],
      );

    default:
      return <String, Object?>{
        'diagnostics': [
          <String, Object?>{
            'severity': 'error',
            'message': 'Unknown LSP tool: $name',
          },
        ],
        'isError': true,
      };
  }
}