Skip to content

Sessions ​

new LspSession(transport, options?) is one conversation with one server over a transport. Await initialize() before anything else. It runs the handshake and then sends the configuration, if any.

ts
const session = new LspSession(transport, {
    rootUri: pathToFileUri('/work'),
    configuration: { php: { version: '8.4' } },
    onMessage: (params) => toast(params.message)
});

await session.initialize();

Options ​

LspSessionOptionsDefaultMeaning
rootUrinullThe workspace root
workspaceFoldersfrom rootUriThe folders of the workspace
clientInfo@adecore/lspThe name, and a version, the server sees
initializationOptionsWhat the server takes in its handshake
configuration{}Settings, sent after the handshake and answered to workspace/configuration
timeoutMs30 secondsThe deadline of a request; 0 turns it off
fullSyncDelayMs200 msHow long the changes for a server with full sync gather before the whole text goes out
snippetSupportfalseWhether completion items may be snippets
insertTextModesnot advertisedSupported completion insertion modes: 1 as-is, 2 adjusted indentation
completionItemDefaultsnot advertisedCompletion-list default fields the consumer applies to each item
resourceOperationscreate, renameThe resource operations the host carries out for a workspace edit, announced to servers. A host that also deletes lists delete; see workspace edits
onConfiguration(item)Answers each configuration item instead of configuration
onApplyEdit(params)refusesApplies a workspace/applyEdit of the server
onShowMessage(params)nullAnswers a window/showMessageRequest
onMessage(params)A window/showMessage for a person to read
onLog(params)A window/logMessage line
onTrace(params)A $/logTrace of the server
onTelemetry(data)A telemetry/event
onShowDocument(params)refusesShows a document or web address for window/showDocument; announced only when set
traceoffThe trace level sent with initialize; setTrace(value) changes it later
localeThe language of the interface, for the server's messages

Advertise insertion modes and list defaults only when the completion consumer implements them.

Without onConfiguration, a query without a section gets all settings, and a section gets that key or dotted path, or null. setConfiguration(settings) replaces the settings and notifies the server. Answer onApplyEdit with applied: true only once the edit is made.

state is a SessionState, which goes new, initializing, ready, closing, closed. A closed session does not restart.

Completion defaults ​

applyCompletionDefaults(item, defaults?) returns the item with the list defaults filled in. Explicit item fields win. A default edit range takes textEditText, then label, as its text. Malformed ranges throw RangeError, unless you pass { onInvalidRange } as the third argument, which reports the range and keeps the other defaults. Non-string labels or edit text throw TypeError. The session advertises insert mode 1 (as-is) by default.

Capabilities ​

supports(method, document?) says whether the server answers a method, from its static and dynamic registrations, filtered by document selector. providerOptions(method, document?) returns that provider's options, such as trigger characters or a semantic tokens legend. Ask for the exact method: completionItem/resolve needs resolveProvider, textDocument/prepareRename needs prepareProvider.

onCapabilitiesChanged(listener) fires when the server registers or unregisters a capability, and when it asks for a refresh of semantic tokens, inlay hints, diagnostics, code lenses, inline values, folding ranges or document content. Request again whatever depends on it.

The client announces all of LSP 3.17 and the 3.18 additions. It announces showDocument only when the host handles it.

The workspace ​

MethodWhat it does
workspaceDiagnostics(previousResultIds?)The problems of every file the server knows, for one that sets workspaceDiagnostics; a document it reports unchanged keeps what the client had
textDocumentContent(uri)The text of a document of a scheme the server lists, such as a decompiled class
changeWorkspaceFolders(event)Adds and removes folders, and tells a server that asked for the changes
setTrace(value)Changes the level of $/logTrace
cancelWorkDoneProgress(token)Asks the server to stop work it reported as cancellable

Notebooks ​

Notebook cells do not go through openDocument. openNotebook(notebook, cellTextDocuments), changeNotebook(notebook, change), saveNotebook(notebook) and closeNotebook(notebook, cellUris) send the notebookDocument/* notifications for notebooks the server selects. syncsNotebook(notebook) tells whether it does. The editor packages have no notebook view.

Listening ​

MethodWhat it hears
onDiagnostics(listener)Published diagnostics of open documents; a report for another version is dropped, and closing a document sends an empty one
onProgress(listener)$/progress
onNotification(method, listener)Any notification of the server
onRequest(method, handler)A request of the server; the RequestHandler gets the params and an AbortSignal, and its result is the answer
onError(listener)Errors of the connection and of listeners

The session answers configuration, workspace folders, applyEdit, message requests, progress creation, registrations and refreshes itself. request(method, params?, options?), notify(method, params?), executeCommand, workspaceSymbols and resolveWorkspaceSymbol reach the server directly.

Errors ​

A request rejects with an LspError that carries a code from ErrorCodes, and its data:

CodeValueMeaning
RequestCancelled-32800Cancelled by a signal or the deadline; $/cancelRequest was sent
ContentModified-32801The text changed under the request
MethodNotFound-32601The server does not answer this method
ServerNotInitialized-32002The session is not ready
InvalidRequest-32600The message was not a valid request
InternalError-32603Anything else

StaleResultError is an LspError with ContentModified and the document's uri. Drop it and ask again.

Shutdown ​

shutdown() closes the documents, sends shutdown and exit and closes the connection, even when a step fails. A second call returns the same promise. Stopping the process is the app's job.

Licenses and third-party notices are listed in each package.