Skip to content

API turboframe.lsp.session

rafageist edited this page Dec 4, 2026 · 4 revisions

turboframe.lsp.session

Source: turboframe/lsp/session.py. Generated from the code of TurboFrame 0.3.0 by tools/wiki_api.py.

A language server's session: the Language Server Protocol, on pygls's client.

LspSession starts the server, says hello (initialize), keeps it informed of the documents open in the editor (open, change, save, close: the whole text each time) and asks it for completions, hovers, signatures, definitions and references. What the server publishes on its own (diagnostics, messages, progress) goes to callbacks. The server never prompts: its questions are answered as dismissed.

The server runs in a process group of its own and is tracked by turboframe.process: it ends with the application, and its own children (node, for pyright) with it. A stop() that comes while start() is still making the process ends that process too, and so does cancelling start().

Each server gets a temporary directory of its own (TMPDIR, TEMP, TMP), on disk under the user's cache, removed when it stops; those of applications no longer running are removed when the next server starts.

An application describes its servers with ServerSpec and finds their command with find_command; it subclasses LspSession to name itself (CLIENT_NAME, CLIENT_VERSION, APP_NAME) and, if its servers ask, to answer their settings (settings()) or to prepare their files (prepare(temp)).

It needs pygls, which comes with the lsp extra: pip install "turboframe[lsp] @ git+...".

Contents

ServerSpec, find_command, section_of, ServerDiagnostic, Location, uri_of, locations, LspSession, LspNoAnswer, LspTimeout, LspRequestFailed, diagnostics_in, temp_root, server_temp_dir

class ServerSpec

class ServerSpec
ServerSpec(name: str, command: tuple[str, ...], language: str = '', section: str = '', url: str = '', project_bins: tuple[str, ...] = (), init_options: dict = <factory>)

A language server an application knows: its name, its command (the program is looked for), and what the application says about it.

find_command

def find_command(server: ServerSpec, root: Path, override: list[str] | None = None, *, folders: Iterable[Path] = ()) -> list[str] | None

The command line of server: the user's (override), else the project's copy (project_bins), else one in folders (an environment's bin), else the PATH's. None when there is none.

section_of

def section_of(settings: dict, section: str | None)

The part of settings a workspace/configuration item asks for ("python.analysis"); None when there is none.

class ServerDiagnostic

class ServerDiagnostic
ServerDiagnostic(start: int, end: int, severity: str, message: str, line: int = 0, column: int = 0)

A diagnostic as text offsets: start, end, severity ("error" or "warning") and its message, with the 0-based line and column where it starts.

class Location

class Location
Location(path: Path, line: int, character: int, end_line: int = -1, end_character: int = 0)

A place the server points to; columns in UTF-16, as the protocol counts them (to_offset converts them once the file is read).

uri_of

def uri_of(path: Path) -> str

locations

def locations(result) -> list[Location]

A definition's or references' answer (a location, a list, or location links) as Locations.

class LspSession

class LspSession
LspSession(server: ServerSpec, command: list[str], root: Path, on_diagnostics: Callable[[Path, list[types.Diagnostic]], None] | None = None, on_message: Callable[[str], None] | None = None, on_progress: Callable[[str, str | None, int | None], None] | None = None)

A running language server (start), its documents and its answers.

on_diagnostics(path, diagnostics), on_message(text) (the server's errors and warnings) and on_progress(token, title, percentage) (title None when the work ends) run on the event loop.

  • settings() -> dict: The settings the server asks for (workspace/configuration), by section; none by default.
  • prepare(temp: Path) -> None: Called before the server starts, with its temporary directory (files it should find there).
  • async start(timeout: float = 60.0) -> bool: Start the server and initialize it; False (and failed) when it does not answer. Cancelled (the worker that started it ends with the application), the server it made is ended, its own children too.
  • async stop() -> None: Shut the server down politely, then make sure it is gone. Called while start runs, it also makes start end the server as soon as its process exists.
  • open(path: Path, text: str, language: str = '') -> None: The editor opened path; language is the protocol's language id (the server's language by default).
  • change(path: Path, text: str) -> None
  • save(path: Path) -> None
  • close(path: Path) -> None
  • async request(method: str, params, timeout: float): Send the request method and await its answer at most timeout seconds. When the time runs out, or the awaiting task is cancelled (a newer request superseded it), the server is told to drop it ($/cancelRequest): pygls does not, so a slow server would still compute every request nobody waits for. Raises LspTimeout and LspRequestFailed; a cancel goes on.
  • async complete(path: Path, offset: int, trigger: str | None = None, timeout: float = 10.0) -> tuple[list[types.CompletionItem], bool]: (items, incomplete) at offset of the document as last sent; ([], False) is the server's empty answer, never a missing one: LspTimeout (the request cancelled in the server) or LspRequestFailed. A cancel of the awaiting task cancels the request in the server too.
  • async completion(path: Path, offset: int, trigger: str | None = None, timeout: float = 10.0) -> tuple[list[types.CompletionItem], bool]: (items, incomplete) at offset of the document as last sent; no answer (out of time, an error) is ([], False) too, as in v0.2. complete tells them apart. Either way a request that runs out of time, or whose task is cancelled, is cancelled in the server.
  • async hover(path: Path, offset: int, timeout: float = 10.0) -> types.Hover | None
  • async definition(path: Path, offset: int, timeout: float = 15.0) -> list[Location]
  • async references(path: Path, offset: int, timeout: float = 60.0) -> list[Location] | None: Every use of the symbol at offset, its declaration included; None when the server did not answer in time.
  • async signature(path: Path, offset: int, timeout: float = 5.0) -> types.SignatureHelp | None: The signature of the call the cursor is in (None: not in a call).
  • async resolve(item: types.CompletionItem, timeout: float = 5.0) -> types.CompletionItem: The item with its documentation (servers send it only when asked).

class LspNoAnswer

class LspNoAnswer(Exception)

A request got no answer to use: LspTimeout or LspRequestFailed. Not an empty answer.

class LspTimeout

class LspTimeout(LspNoAnswer)

The server did not answer in time; the request was cancelled in the server ($/cancelRequest).

class LspRequestFailed

class LspRequestFailed(LspNoAnswer)

The server answered with an error, or the request could not be sent.

diagnostics_in

def diagnostics_in(text: str, items: list[types.Diagnostic], source: str, *, keep: Callable[[str, list[types.Diagnostic]], list[types.Diagnostic]] | None = None) -> list[ServerDiagnostic]

The server's diagnostics as text ranges, labelled with their source (or source); hints and information are left out, and so is what keep(text, items) drops (an application's idioms a static analyser misreads).

temp_root

def temp_root(app: str = 'turboframe') -> Path

Where app's servers get their temporary directories: <cache>/<app>/tmp. The cache is $XDG_CACHE_HOME when it is set; else %LOCALAPPDATA% on Windows, ~/.cache elsewhere.

server_temp_dir

def server_temp_dir(app: str = 'turboframe') -> Path

A new temporary directory for a server, named after this process; those of processes gone are removed.

Clone this wiki locally