src — lsp

Module: src-lsp Cohesion: 0.80 Members: 0

src — lsp

The src/lsp module provides comprehensive Language Server Protocol (LSP) capabilities, serving two primary roles:

  1. LSP Client: Interacting with external, standard LSP servers (e.g., typescript-language-server, pylsp) to provide core IDE features like go-to-definition, find references, diagnostics, and hover information.
  2. Code Buddy LSP Server: Implementing an internal LSP server that integrates Code Buddy's AI capabilities (completions, diagnostics, code actions, hover, signature help) directly into any LSP-compatible editor (e.g., VS Code, Neovim).

This dual approach allows Code Buddy to leverage existing, highly optimized language servers for foundational features while augmenting the developer experience with AI-powered assistance.


1. LSP Client (lsp-client.ts)

The LSPClient class is responsible for spawning and managing external LSP server processes, communicating with them via JSON-RPC over stdio, and translating LSP responses into Code Buddy's internal data structures.

1.1. Purpose

1.2. Key Features & How it Works

Server Lifecycle Management

The LSPClient manages the lifecycle of child processes for each language server.

JSON-RPC Communication

Communication with LSP servers happens over stdio using the JSON-RPC 2.0 protocol.

Document Synchronization

LSP servers need to be aware of the content of files being edited.

Supported LSP Operations

The LSPClient exposes methods for common LSP features:

1.3. Integration

The LSPClient is used by various Code Buddy tools and features:

graph TD
    subgraph Code Buddy Tools/Features
        A[lsp_check Tool]
        B[lsp_goto_def Tool]
        C[lsp_find_refs Tool]
        D[LSP Rename Tool]
        E[Auto-Import Tool]
        F[Issues Tree Provider]
    end

    subgraph src/lsp
        G[LSPClient]
    end

    subgraph External LSP Servers
        H[typescript-language-server]
        I[pylsp]
        J[clangd]
        K[...]
    end

    A --> G: getDiagnostics
    B --> G: goToDefinition
    C --> G: findReferences
    D --> G: prepareRename, rename
    E --> G: codeAction
    F --> G: getDiagnostics

    G -- Spawns & Communicates via JSON-RPC --> H
    G -- Spawns & Communicates via JSON-RPC --> I
    G -- Spawns & Communicates via JSON-RPC --> J
    G -- Spawns & Communicates via JSON-RPC --> K

2. Code Buddy LSP Server (server.ts)

This file implements an actual LSP server that an editor (like VS Code with the Code Buddy extension) can connect to. It leverages Code Buddy's AI (CodeBuddyClient) to provide advanced code assistance features.

2.1. Purpose

2.2. Key Features & How it Works

The server uses vscode-languageserver/node to establish a connection and manage text documents.

Initialization and Configuration

AI-Powered Diagnostics

AI-Powered Completions

AI-Powered Code Actions

AI-Powered Hover

AI-Powered Signature Help

2.3. Internal Components

2.4. Integration

The Code Buddy LSP Server (server.ts) is designed to be run as a separate process, typically spawned by an editor extension (e.g., vscode-extension/src/extension.ts).

graph TD
    subgraph Editor (e.g., VS Code)
        A[User Interaction]
    end

    subgraph Code Buddy LSP Server (src/lsp/server.ts)
        B[LSP Connection]
        C[TextDocuments Manager]
        D[CompletionCache]
        E[ContextGatherer]
    end

    subgraph Code Buddy AI Backend
        F[CodeBuddyClient (src/codebuddy/client.ts)]
    end

    A -- LSP Requests (completion, hover, diagnostics) --> B
    B -- Document Content --> C
    C -- Get Text, Position --> E: gatherCompletionContext
    E -- Context --> D: get/set
    D -- Cache Miss --> F: chat (for completions, diagnostics, hover, etc.)
    F -- AI Response --> D: set
    D -- Cached/AI Result --> B
    B -- LSP Responses (CompletionItem[], Diagnostic[], Hover) --> A