Repository navigation
7 2 language server tc lsp
Source: https://deepwiki.com/PonvasanthPon/TransCompilator/7-2-language-server-tc-lsp
Relevant source files
The following files were used as context for generating this wiki page:
- crates/tc-cli/tests/line_span_regressions.rs
- crates/tc-cli/tests/natural_text_and_math.rs
- crates/tc-lsp/Cargo.toml
- crates/tc-lsp/build.rs
- crates/tc-lsp/src/analysis.rs
- crates/tc-lsp/src/doc.rs
- crates/tc-lsp/src/handlers.rs
- crates/tc-lsp/src/json.rs
- crates/tc-lsp/src/lib.rs
- crates/tc-lsp/src/main.rs
- crates/tc-lsp/src/rpc.rs
- crates/tc-lsp/src/tokens.rs
- crates/tc-semantics/src/clause_graph.rs
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.
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
Diagram 1: JSON-RPC Transport Data Flow (run_with_io, Json, Server::handle)
Sources:
crates/tc-lsp/src/lib.rs:18-41crates/tc-lsp/src/main.rs:1-29crates/tc-lsp/src/json.rs:1-27
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-21crates/tc-lsp/src/doc.rs:40-51crates/tc-lsp/src/doc.rs:148-185crates/tc-lsp/src/doc.rs:187-208
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
SymbolIndexcrates/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
Diagram 2: Bridging Natural Language Space to Code Entity Space via analyze() and SymbolIndex
Sources:
crates/tc-lsp/src/analysis.rs:104-157crates/tc-lsp/src/analysis.rs:161-209
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:
-
initialize: Advertises text document synchronization, hover, completion, definition, document symbols, and semantic tokens crates/tc-lsp/src/handlers.rs:143-148. -
textDocument/hover: Inspects the word under the cursor against the symbol index and standard library modules to render markdown descriptions crates/tc-lsp/src/handlers.rs:163. -
textDocument/completion: Suggests keywords, built-in phrases, record fields, and variables crates/tc-lsp/src/handlers.rs:164. -
textDocument/definition: Resolves cursor symbols to their declaration source location crates/tc-lsp/src/handlers.rs:165. -
textDocument/documentSymbol: Builds a hierarchical tree of records, unions, variables, and functions crates/tc-lsp/src/handlers.rs:166. -
textDocument/semanticTokens/full: Computes monochrome semantic tokens for the document crates/tc-lsp/src/handlers.rs:167.
Sources:
crates/tc-lsp/src/handlers.rs:105-120crates/tc-lsp/src/handlers.rs:130-176
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-57crates/tc-lsp/src/tokens.rs:70-122crates/tc-lsp/src/tokens.rs:154-207
The TransCompilator Project