Skip to content

7 2 language server tc lsp

TransCompilator edited this page Nov 26, 2026 · 1 revision

7.2 Language Server (tc-lsp)

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/7-2-language-server-tc-lsp

Language Server (tc-lsp)

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

The tc-lsp crate provides a native, zero-dependency language server implemented purely in Rust (std-only) for TransCompilator English (.eng) files. It bridges the compiler's core semantic analysis pipeline (tc-semantics, tc-typeck, tc-lexicon) with standard Language Server Protocol (LSP) clients like Zed or VS Code. It handles JSON-RPC transport over buffered I/O, maintains open document states, performs incremental in-memory project analysis with import resolution, and delivers responsive hover information, code completion, symbol definitions, document symbols, and monochrome semantic tokens.


1. JSON-RPC Transport and Main Loop

The language server communicates via stdin/stdout (or arbitrary buffered readers and writers) using a strict subset of JSON-RPC over Content-Length framed or newline-delimited streams. The core transport loop is driven by run_with_io crates/tc-lsp/src/lib.rs:22-41 and invoked via the command-line entry point in main.rs crates/tc-lsp/src/main.rs:1-29.

Messages are parsed and serialized using an allocation-friendly, strict RFC 8259 JSON parser (Json) located in crates/tc-lsp/src/json.rs crates/tc-lsp/src/json.rs:1-27, supporting objects, arrays, strings with full escape sequences, numbers, booleans, and nulls.

graph TD
    "Stdin" -->|"RawBytes"| "read_json"
    "read_json" -->|"Json"| "Server::handle"
    "Server::handle" -->|"MethodDispatch"| "Handlers"
    "Handlers" -->|"ResponseJson"| "write_json"
    "write_json" -->|"Stdout"| "Client"

    style "Server::handle" fill:#fff,stroke:#000,stroke-width:1px
    style "read_json" fill:#fff,stroke:#000,stroke-width:1px
Loading

Diagram 1: JSON-RPC Transport Data Flow (run_with_io, Json, Server::handle)

Sources:

  • crates/tc-lsp/src/lib.rs:18-41
  • crates/tc-lsp/src/main.rs:1-29
  • crates/tc-lsp/src/json.rs:1-27

2. Document Store and URI Management

Open documents are managed inside the Server state machine. The Document struct crates/tc-lsp/src/doc.rs:40-51 tracks the active URI, resolved filesystem PathBuf, current full text, and client version numbers.

Position mapping utilities handle conversions between zero-based UTF-16 code unit offsets (Position) crates/tc-lsp/src/doc.rs:6-21 and Rust byte offsets. word_at crates/tc-lsp/src/doc.rs:148-185 extracts identifier-like words under the cursor for hover and definition requests. URI conversions (uri_to_path and path_to_uri) crates/tc-lsp/src/doc.rs:187-208 handle percent-encoding for cross-platform filesystem paths.

Sources:

  • crates/tc-lsp/src/doc.rs:6-21
  • crates/tc-lsp/src/doc.rs:40-51
  • crates/tc-lsp/src/doc.rs:148-185
  • crates/tc-lsp/src/doc.rs:187-208

3. In-Memory Project Analysis

Project analysis is orchestrated by analyze() crates/tc-lsp/src/analysis.rs:161-209, which processes an open document alongside its transitive sibling and std/ library imports.

The analysis produces an Analysis result crates/tc-lsp/src/analysis.rs:142-157 containing:

  • Lowered project outcome (Option<LoweredProject>)
  • Per-sentence execution traces (Vec<SentenceTrace>)
  • LSP-compatible diagnostics (Vec<Diagnostic>)
  • A searchable SymbolIndex crates/tc-lsp/src/analysis.rs:104-116 containing types, variables, functions, globals, and phrase templates.
graph TD
    subgraph "NaturalLanguageSpace"
    "EnglishSourceFile" -->|"Paragraphs"| "segment_source"
    "EnglishClauses" -->|"Tokens"| "tokenize"
    end

    subgraph "CodeEntitySpace"
    "analyze" -->|"Imports"| "Document"
    "analyze" -->|"Builds"| "SymbolIndex"
    "SymbolIndex" -->|"TypesAndVars"| "TypeInfo"
    "SymbolIndex" -->|"Functions"| "FunctionInfo"
    end

    style "analyze" fill:#fff,stroke:#000,stroke-width:1px
    style "SymbolIndex" fill:#fff,stroke:#000,stroke-width:1px
    style "TypeInfo" fill:#fff,stroke:#000,stroke-width:1px
Loading

Diagram 2: Bridging Natural Language Space to Code Entity Space via analyze() and SymbolIndex

Sources:

  • crates/tc-lsp/src/analysis.rs:104-157
  • crates/tc-lsp/src/analysis.rs:161-209

4. LSP Handlers and Capabilities

The Server struct crates/tc-lsp/src/handlers.rs:115-120 dispatches incoming JSON-RPC requests via Server::handle() crates/tc-lsp/src/handlers.rs:130-176. Supported capabilities include:

Sources:

  • crates/tc-lsp/src/handlers.rs:105-120
  • crates/tc-lsp/src/handlers.rs:130-176

5. Monochrome Semantic Tokens Engine

The semantic token generator (semantic_tokens) crates/tc-lsp/src/tokens.rs:154-207 translates live .eng documents into LSP semantic token arrays using delta encoding. It relies on LanguageIndex crates/tc-lsp/src/tokens.rs:70-76 to classify tokens into keywords, functions, variables, strings, numbers, and comments.

It supports modifiers for declarations, readonly anaphors, and documentation-comment spans representing omitted prose.

Sources:

  • crates/tc-lsp/src/tokens.rs:16-57
  • crates/tc-lsp/src/tokens.rs:70-122
  • crates/tc-lsp/src/tokens.rs:154-207

Clone this wiki locally