Documentation
¶
Overview ¶
Package lspbridge bridges a WebSocket carrying JSON-RPC LSP messages to a language-server subprocess's stdio. Each LSP server gets a per-connection process (texlab / gopls / pyright / typescript- language-server / rust-analyzer / …) keyed by the language id.
Wire shape on the WS :
server → client : one JSON-RPC object per WS text frame client → server : one JSON-RPC object per WS text frame
The bridge strips + adds the Content-Length framing on the stdio side so the WS payload is the bare JSON, matching what the standard CodeMirror LSP clients expect (e.g. `codemirror-languageserver`, `@open-rpc/codemirror-lsp`).
Binary resolution : a language id resolves to a binary via the Servers registry. Each entry is configurable via an environment variable so operators can override the binary path per-deployment without rebuilding the image. DefaultServers uses a neutral, project-agnostic prefix :
LSPBRIDGE_TEXLAB — texlab binary path (default: "texlab") LSPBRIDGE_GOPLS — gopls binary path (default: "gopls") LSPBRIDGE_PYRIGHT — pyright-langserver (default: "pyright-langserver") LSPBRIDGE_TS — typescript-language-server (default: "typescript-language-server") LSPBRIDGE_RUSTANALYZER — rust-analyzer (default: "rust-analyzer")
When the binary isn't on $PATH the bridge writes a JSON-RPC error response back to the client + closes the WS so the editor can surface a single visible failure instead of silently dropping completions.
The registry is a plain map : callers may pass their own by replacing Servers (e.g. `lspbridge.Servers = myMap`) or extend the defaults in place (`lspbridge.Servers["zig"] = …`). Adding a new language is a one-line change ; the handler dispatches by the URL path's lang parameter.
Index ¶
- Variables
- func AvailableLanguages() []string
- func DefaultServers() map[string]LanguageServer
- func EncodeError(id any, code int, msg string) []byte
- func HandleWS(w http.ResponseWriter, r *http.Request, langKey string, logger *slog.Logger, ...)
- func WithSubject(ctx context.Context, subject string) context.Context
- type LanguageServer
Constants ¶
This section is empty.
Variables ¶
var Servers = DefaultServers()
Servers is the registry the exported handlers dispatch through. Initialised to DefaultServers; callers may replace it wholesale or mutate it before serving.
Functions ¶
func AvailableLanguages ¶
func AvailableLanguages() []string
AvailableLanguages returns the set of language ids whose binary is actually resolvable on the host. A client uses this for progressive enablement : connecting to the bridge when the host advertises support, and falling back to a simpler autocomplete otherwise.
func DefaultServers ¶
func DefaultServers() map[string]LanguageServer
DefaultServers returns a fresh registry of the common LSP launchers, keyed by canonical language id. Each entry's EnvOverride uses the neutral LSPBRIDGE_* prefix so nothing about this catalogue is tied to a particular host application. Callers can use it as-is, extend it, or ignore it and supply their own map via Servers.
func EncodeError ¶
EncodeError writes a JSON-RPC error response. Used by a handler when the bridge can't be set up before the subprocess even starts.
func HandleWS ¶
func HandleWS(w http.ResponseWriter, r *http.Request, langKey string, logger *slog.Logger, acceptOpts *websocket.AcceptOptions)
HandleWS upgrades the HTTP request to a WebSocket + spawns the per-language LSP. Bidirectional pipe runs until either side closes ; the subprocess is killed on WS close.
langKey : the URL path param ("latex", "go", …) extracted by the caller's mux. Returning quickly on unknown languages keeps the editor's error path clean.
acceptOpts : the caller passes the same websocket.AcceptOptions it uses for the rest of its WS endpoints so the Origin allowlist stays uniform across endpoints. nil = use a permissive default (dev mode).
func WithSubject ¶
WithSubject returns a child context tagged with the per-user counter key. Call this in the HTTP handler before HandleWS so the per-user concurrency cap buckets by the authenticated identity rather than by raw token (which would double the cap during rotation overlap or multi-device use).
Types ¶
type LanguageServer ¶
type LanguageServer struct {
Lang string // canonical key passed via URL path : "latex" / "go" / …
Binary string // default executable name
Args []string // additional CLI arguments
// EnvOverride : if non-empty + the env var is set (and points at an
// existing file), use that path instead of Binary. Lets ops override
// per-deployment without re-building the image.
EnvOverride string
}
LanguageServer describes how to launch one LSP for a given language.
