lsp

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultClientName = "toe"

DefaultClientName is the client identifier sent to language servers

Variables

View Source
var (
	ErrCommandRequired = errors.New("language server command required")
	ErrRequiredRoot    = errors.New("required language server root not found")
	ErrServerNotFound  = errors.New("language server not found")
)
View Source
var (
	ErrCompletionUnavailable   = errors.New("completion unavailable")
	ErrCodeActionUnavailable   = errors.New("code action unavailable")
	ErrDocumentLinkUnavailable = errors.New("document link unavailable")
	ErrLanguageServerExited    = errors.New("language server exited")
	ErrLanguageServerRequest   = errors.New("language server request failed")
)
View Source
var (
	ErrWorkspaceEditUnsupported = errors.New("workspace edit unsupported")
	ErrWorkspaceEditURI         = errors.New("workspace edit URI unsupported")
	ErrWorkspaceEditRange       = errors.New("workspace edit range invalid")
	ErrWorkspaceEditFile        = errors.New(
		"workspace edit file operation failed",
	)
)

Functions

func DefaultClientCapabilities

func DefaultClientCapabilities() protocol.ClientCapabilities

DefaultClientCapabilities returns the editor's LSP client capabilities

func NewInitializeParams

func NewInitializeParams(cfg InitializeConfig) *protocol.InitializeParams

NewInitializeParams builds an InitializeParams message from the given config

func OffsetEncoding

func OffsetEncoding(
	capabilities protocol.ServerCapabilities,
) protocol.PositionEncodingKind

OffsetEncoding resolves the preferred position encoding

func ResolveWorkspace

func ResolveWorkspace(req WorkspaceRequest) (string, bool)

ResolveWorkspace locates the nearest workspace root for the given file path

func RootFound added in v0.1.10

func RootFound(root string, patterns []string) (bool, error)

RootFound reports whether any required root pattern matches a directory entry

func SupportsFeature

func SupportsFeature(
	capabilities protocol.ServerCapabilities, feature Feature,
) bool

SupportsFeature reports whether the capability set supports the named feature

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client manages a connection to a single language server process

func NewClient

func NewClient(
	ctx context.Context, rwc io.ReadWriteCloser, handler protocol.Client,
) (context.Context, *Client)

NewClient dials the server and returns a ready Client

func Start

func Start(cfg *TransportConfig) (context.Context, *Client, error)

Start launches the language server process and returns a connected Client

func (*Client) Capabilities

func (c *Client) Capabilities() (protocol.ServerCapabilities, bool)

Capabilities returns initialized server capabilities

func (*Client) Close

func (c *Client) Close() error

Close closes the JSON-RPC connection and stops the server process

func (*Client) CodeActions

func (c *Client) CodeActions(
	ctx context.Context, doc DocumentSnapshot, r core.Range,
	diags []protocol.Diagnostic,
) ([]protocol.CommandOrCodeAction, bool, error)

CodeActions requests code actions and commands for a selection

func (*Client) Completion

func (c *Client) Completion(
	ctx context.Context, doc DocumentSnapshot, pos int,
	compCtx protocol.CompletionContext,
) (CompletionList, bool, error)

Completion requests completion items from the server at the given position

func (*Client) DidChange

func (c *Client) DidChange(
	ctx context.Context, doc DocumentSnapshot,
) (bool, error)

DidChange sends a textDocument/didChange notification when supported

func (*Client) DidChangeDocument

func (c *Client) DidChangeDocument(
	ctx context.Context, doc DocumentSnapshot, change view.DocumentChange,
) (bool, error)

DidChangeDocument notifies the server that a document was edited

func (*Client) DidChangeWatchedFile

func (c *Client) DidChangeWatchedFile(ctx context.Context, path string) error

DidChangeWatchedFile notifies the server that a watched file changed

func (*Client) DidChangeWatchedFiles

func (c *Client) DidChangeWatchedFiles(
	ctx context.Context, events []fileWatchEvent,
) error

DidChangeWatchedFiles notifies the server that watched files changed

func (*Client) DidClose

func (c *Client) DidClose(
	ctx context.Context, doc DocumentSnapshot,
) (bool, error)

DidClose sends a textDocument/didClose notification when supported

func (*Client) DidCreateFile

func (c *Client) DidCreateFile(
	ctx context.Context, path string, dir bool,
) (bool, error)

DidCreateFile notifies the server that a file was created

func (*Client) DidDeleteFile

func (c *Client) DidDeleteFile(
	ctx context.Context, path string, dir bool,
) (bool, error)

DidDeleteFile notifies the server that a file was deleted

func (*Client) DidOpen

func (c *Client) DidOpen(
	ctx context.Context, doc DocumentSnapshot,
) (bool, error)

DidOpen sends a textDocument/didOpen notification when supported

func (*Client) DidRenameFile

func (c *Client) DidRenameFile(
	ctx context.Context, rename view.FileRename, dir bool,
) (bool, error)

DidRenameFile notifies the server that a file was renamed

func (*Client) DidSave

func (c *Client) DidSave(
	ctx context.Context, doc DocumentSnapshot,
) (bool, error)

DidSave sends a textDocument/didSave notification when supported

func (*Client) DocumentColors

func (c *Client) DocumentColors(
	ctx context.Context, doc DocumentSnapshot,
) ([]protocol.ColorInformation, bool, error)

DocumentColors requests document-wide colors

func (*Client) DocumentDiagnostics

func (c *Client) DocumentDiagnostics(
	ctx context.Context, doc DocumentSnapshot, previousID *string,
) (protocol.DocumentDiagnosticReport, bool, error)

DocumentDiagnostics pulls diagnostics for a document

func (*Client) DocumentHighlights

func (c *Client) DocumentHighlights(
	ctx context.Context, doc DocumentSnapshot, pos int,
) ([]protocol.DocumentHighlight, bool, error)

DocumentHighlights requests same-document symbol highlights at pos

func (c *Client) DocumentLinks(
	ctx context.Context, doc DocumentSnapshot,
) ([]protocol.DocumentLink, bool, error)

DocumentLinks requests document-wide links

func (*Client) DocumentSymbols

func (c *Client) DocumentSymbols(
	ctx context.Context, doc DocumentSnapshot,
) (protocol.DocumentSymbolResult, bool, error)

DocumentSymbols requests the document symbol tree from the server

func (*Client) ExecuteCommand

func (c *Client) ExecuteCommand(
	ctx context.Context, params *protocol.ExecuteCommandParams,
) error

ExecuteCommand executes a workspace command on the server

func (*Client) FormatDocument

func (c *Client) FormatDocument(
	ctx context.Context, doc DocumentSnapshot, opts protocol.FormattingOptions,
) ([]protocol.TextEdit, bool, error)

FormatDocument requests whole-document formatting edits

func (*Client) FormatRange

FormatRange requests range-formatting edits

func (*Client) GotoDeclaration

func (c *Client) GotoDeclaration(
	ctx context.Context, doc DocumentSnapshot, pos int,
) ([]protocol.Location, bool, error)

GotoDeclaration requests the declaration location of the symbol at pos

func (*Client) GotoDefinition

func (c *Client) GotoDefinition(
	ctx context.Context, doc DocumentSnapshot, pos int,
) ([]protocol.Location, bool, error)

GotoDefinition requests the definition location of the symbol at pos

func (*Client) GotoImplementation

func (c *Client) GotoImplementation(
	ctx context.Context, doc DocumentSnapshot, pos int,
) ([]protocol.Location, bool, error)

GotoImplementation requests implementation locations for the symbol at pos

func (*Client) GotoReference

func (c *Client) GotoReference(
	ctx context.Context, doc DocumentSnapshot, pos int,
) ([]protocol.Location, bool, error)

GotoReference requests all reference locations for the symbol at pos

func (*Client) GotoTypeDefinition

func (c *Client) GotoTypeDefinition(
	ctx context.Context, doc DocumentSnapshot, pos int,
) ([]protocol.Location, bool, error)

GotoTypeDefinition requests the type definition location of the symbol at pos

func (*Client) Hover

func (c *Client) Hover(
	ctx context.Context, doc DocumentSnapshot, pos int,
) (*protocol.Hover, bool, error)

Hover requests documentation for the symbol at a position

func (*Client) Initialize

func (c *Client) Initialize(
	ctx context.Context, params *protocol.InitializeParams,
) (*protocol.InitializeResult, error)

Initialize performs the LSP initialization handshake with the server

func (*Client) InlayHints

func (c *Client) InlayHints(
	ctx context.Context, doc DocumentSnapshot, r core.Range,
) ([]protocol.InlayHint, bool, error)

InlayHints requests inlay hints for a document range

func (*Client) Name

func (c *Client) Name() string

Name returns the registered name of the language server

func (*Client) OffsetEncoding

func (c *Client) OffsetEncoding() protocol.PositionEncodingKind

OffsetEncoding returns the position encoding kind negotiated with the server

func (*Client) PrepareRename

func (c *Client) PrepareRename(
	ctx context.Context, doc DocumentSnapshot, pos int,
) (protocol.PrepareRenameResult, bool, error)

PrepareRename requests the server's rename range or placeholder

func (*Client) RenameSymbol

func (c *Client) RenameSymbol(
	ctx context.Context, doc DocumentSnapshot, pos int, name string,
) (*protocol.WorkspaceEdit, bool, error)

RenameSymbol requests a workspace edit for a new symbol name

func (c *Client) ResolveDocumentLink(
	ctx context.Context, link protocol.DocumentLink,
) (*protocol.DocumentLink, bool, error)

ResolveDocumentLink resolves a stored document link target when supported

func (*Client) Server

func (c *Client) Server() protocol.Server

Server returns the underlying protocol server handle

func (*Client) Shutdown

func (c *Client) Shutdown(ctx context.Context) error

Shutdown gracefully terminates the server session

func (*Client) SignatureHelp

func (c *Client) SignatureHelp(
	ctx context.Context, doc DocumentSnapshot, pos int,
	shCtx protocol.SignatureHelpContext,
) (*protocol.SignatureHelp, bool, error)

SignatureHelp requests parameter hints for the call at a position

func (*Client) State

func (c *Client) State() ClientState

State returns the current lifecycle state of the client

func (*Client) SupportsFeature

func (c *Client) SupportsFeature(feature Feature) bool

SupportsFeature reports whether the server supports the given LSP feature

func (*Client) WillCreateFile

func (c *Client) WillCreateFile(
	ctx context.Context, path string, dir bool,
) (*protocol.WorkspaceEdit, bool, error)

WillCreateFile asks the server for edits to make before a file is created

func (*Client) WillDeleteFile

func (c *Client) WillDeleteFile(
	ctx context.Context, path string, dir bool,
) (*protocol.WorkspaceEdit, bool, error)

WillDeleteFile asks the server for edits to make before a file is deleted

func (*Client) WillRenameFile

func (c *Client) WillRenameFile(
	ctx context.Context, rename view.FileRename, dir bool,
) (*protocol.WorkspaceEdit, bool, error)

WillRenameFile asks the server for edits to make before a file is renamed

func (*Client) WorkspaceSymbols

func (c *Client) WorkspaceSymbols(
	ctx context.Context, query string,
) (protocol.WorkspaceSymbolResult, bool, error)

WorkspaceSymbols requests workspace symbols matching query from the server

type ClientState

type ClientState int

ClientState is the lifecycle state of a Client

const (
	ClientNew ClientState = iota
	ClientInitialized
	ClientShutdown
)

ClientNew is the initial state; ClientInitialized means handshake complete; ClientShutdown means closed

type CompletionList

type CompletionList struct {
	Items      []*view.CompletionItem
	Raw        []protocol.CompletionItem
	Incomplete bool
}

CompletionList is a normalized completion response

type DocumentSnapshot

type DocumentSnapshot struct {
	URI        uri.URI
	LanguageID string
	Version    int32
	Text       string
}

DocumentSnapshot is the LSP-visible state of a file-backed document

func SnapshotDocument

func SnapshotDocument(doc *view.Document) (DocumentSnapshot, bool)

SnapshotDocument captures the current file-backed view document state

type Feature

type Feature int

Feature names an LSP capability group the editor can query before issuing requests

const (
	FeatureFormat Feature = iota
	FeatureRangeFormat
	FeatureGotoDeclaration
	FeatureGotoDefinition
	FeatureGotoTypeDefinition
	FeatureGotoReference
	FeatureGotoImplementation
	FeatureSignatureHelp
	FeatureHover
	FeatureDocumentHighlight
	FeatureCompletion
	FeatureCodeAction
	FeatureDocumentLinks
	FeatureWorkspaceCommand
	FeatureDocumentSymbols
	FeatureWorkspaceSymbols
	FeatureDiagnostics
	FeaturePullDiagnostics
	FeatureRename
	FeatureInlayHints
	FeatureDocumentColors
	FeatureCallHierarchy
)

Feature constants name the LSP capability groups the editor supports

type InitializeConfig

type InitializeConfig struct {
	ClientName            string
	ClientVersion         string
	WorkspaceRoot         string
	InitializationOptions protocol.LSPAny
}

InitializeConfig holds parameters for LSP server initialization

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry maps named server configurations to running client connections

func NewRegistry

func NewRegistry(servers map[string]language.Server) *Registry

NewRegistry creates a Registry seeded with the given server configurations

func (*Registry) Server

func (r *Registry) Server(name string) (language.Server, bool)

Server returns the configuration for the named language server

func (*Registry) Start

func (r *Registry) Start(
	ctx context.Context, args RegistryStartArgs,
) (*Client, error)

Start launches a named server and returns the resulting client

type RegistryStartArgs added in v0.2.0

type RegistryStartArgs struct {
	Name    string
	Dir     string
	Handler protocol.Client
}

RegistryStartArgs names the server to launch and the directory to run it in

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session owns runtime language-server clients for an editor

func Attach

func Attach(ctx context.Context, e *view.Editor) *Session

Attach starts an LSP session for the editor and observes document changes

func NewSession

func NewSession(ctx context.Context, cwd string) *Session

NewSession creates an LSP session from language configuration

func (*Session) ApplyCodeAction

func (s *Session) ApplyCodeAction(
	_ *view.Document, _ view.Id, item view.CodeAction,
) error

ApplyCodeAction applies a selected code action or command

func (*Session) ApplyCompletion

func (s *Session) ApplyCompletion(
	doc *view.Document, viewID view.Id, item *view.CompletionItem,
) error

ApplyCompletion applies the selected completion item to the document

func (*Session) ApplyWorkspaceEdit

func (s *Session) ApplyWorkspaceEdit(
	server string, edit protocol.WorkspaceEdit,
) error

ApplyWorkspaceEdit applies a server-provided workspace edit

func (*Session) Busy

func (s *Session) Busy() bool

Busy reports whether any language server has an in-flight progress notification, for the status-line activity spinner

func (*Session) Close

func (s *Session) Close() error

Close terminates all clients owned by the session

func (*Session) CodeActions

func (s *Session) CodeActions(
	doc *view.Document, viewID view.Id,
) ([]view.CodeAction, error)

CodeActions returns code actions and commands for the current selection

func (*Session) Completions

func (s *Session) Completions(
	doc *view.Document, viewID view.Id,
) (view.CompletionResult, error)

Completions requests an invoked completion list at the cursor

func (*Session) DidCreateFile

func (s *Session) DidCreateFile(path string, dir bool) error

DidCreateFile tells every server that path was created

func (*Session) DidDeleteFile

func (s *Session) DidDeleteFile(path string, dir bool) error

DidDeleteFile tells every server that path was deleted

func (*Session) DidRenameFile

func (s *Session) DidRenameFile(rename view.FileRename, dir bool) error

DidRenameFile tells every server that a path was renamed

func (*Session) DocumentChanged

func (s *Session) DocumentChanged(
	doc *view.Document, change view.DocumentChange,
)

DocumentChanged sends didChange notifications to attached servers

func (*Session) DocumentClosed

func (s *Session) DocumentClosed(doc *view.Document)

DocumentClosed sends didClose notifications and forgets the document

func (*Session) DocumentColors

func (s *Session) DocumentColors(
	doc *view.Document,
) ([]view.DocumentColor, error)

DocumentColors returns document-wide colors and stores them on the document

func (*Session) DocumentHighlights

func (s *Session) DocumentHighlights(
	doc *view.Document, viewID view.Id,
) ([]view.DocumentHighlight, error)

DocumentHighlights returns same-document symbol highlights at the cursor

func (s *Session) DocumentLinks(
	doc *view.Document,
) ([]view.DocumentLink, error)

DocumentLinks returns document-wide links and stores them on the document

func (*Session) DocumentOpened

func (s *Session) DocumentOpened(doc *view.Document)

DocumentOpened starts matching servers and sends didOpen notifications

func (*Session) DocumentSaved

func (s *Session) DocumentSaved(doc *view.Document)

DocumentSaved sends didSave notifications to attached servers

func (*Session) DocumentSymbols

func (s *Session) DocumentSymbols(doc *view.Document) ([]view.Symbol, error)

DocumentSymbols returns flattened symbols from all document servers

func (*Session) ExecuteWorkspaceCommand

func (s *Session) ExecuteWorkspaceCommand(
	doc *view.Document, name string, args []string,
) error

ExecuteWorkspaceCommand runs a named workspace command on the matching server

func (*Session) FormatDocument

func (s *Session) FormatDocument(doc *view.Document, _ view.Id) error

FormatDocument applies whole-document formatting from a language server

func (*Session) FormatSelection

func (s *Session) FormatSelection(doc *view.Document, viewID view.Id) error

FormatSelection applies range formatting for the current primary selection

func (*Session) GotoDeclaration

func (s *Session) GotoDeclaration(
	doc *view.Document, viewID view.Id,
) ([]view.Location, error)

GotoDeclaration returns declaration locations for the symbol at the cursor

func (*Session) GotoDefinition

func (s *Session) GotoDefinition(
	doc *view.Document, viewID view.Id,
) ([]view.Location, error)

GotoDefinition returns definition locations for the symbol at the cursor

func (*Session) GotoImplementation

func (s *Session) GotoImplementation(
	doc *view.Document, viewID view.Id,
) ([]view.Location, error)

GotoImplementation returns implementation locations for the symbol at the cursor

func (*Session) GotoReference

func (s *Session) GotoReference(
	doc *view.Document, viewID view.Id,
) ([]view.Location, error)

GotoReference returns all reference locations for the symbol at the cursor

func (*Session) GotoTypeDefinition

func (s *Session) GotoTypeDefinition(
	doc *view.Document, viewID view.Id,
) ([]view.Location, error)

GotoTypeDefinition returns type definition locations for the symbol at the cursor

func (*Session) Hover

func (s *Session) Hover(doc *view.Document, viewID view.Id) (string, error)

Hover returns the hover text for the symbol at the cursor position

func (*Session) InlayHints

func (s *Session) InlayHints(
	doc *view.Document, viewID view.Id,
) ([]view.InlayHint, error)

InlayHints returns language-server hints and stores them for the view

func (*Session) LanguageServerNames added in v0.3.0

func (s *Session) LanguageServerNames(doc *view.Document) []string

LanguageServerNames returns the servers configured for the document

func (*Session) PublishDiagnostics

func (s *Session) PublishDiagnostics(
	_ context.Context, params *protocol.PublishDiagnosticsParams,
) error

PublishDiagnostics forwards server diagnostics to the editor

func (*Session) PullDiagnostics

func (s *Session) PullDiagnostics(doc *view.Document) error

PullDiagnostics requests fresh diagnostics from document servers

func (*Session) ReloadConfig

func (s *Session) ReloadConfig() error

ReloadConfig reloads language-server config and restarts open documents

func (*Session) RenameSymbol

func (s *Session) RenameSymbol(
	doc *view.Document, viewID view.Id, name string,
) error

RenameSymbol applies a language-server rename edit for the current symbol

func (*Session) RenameSymbolPrefill

func (s *Session) RenameSymbolPrefill(
	doc *view.Document, viewID view.Id,
) (string, error)

RenameSymbolPrefill returns the initial rename prompt text

func (*Session) ResolveCompletion

func (s *Session) ResolveCompletion(
	_ *view.Document, _ view.Id, item *view.CompletionItem,
) (*view.CompletionItem, error)

ResolveCompletion fetches extra completion item details

func (s *Session) ResolveDocumentLink(
	doc *view.Document, link view.DocumentLink,
) (view.DocumentLink, error)

ResolveDocumentLink resolves a document link target and updates the document

func (*Session) RestartLanguageServers

func (s *Session) RestartLanguageServers(
	doc *view.Document, names []string,
) ([]string, error)

RestartLanguageServers stops and restarts the named servers for the document

func (*Session) SignatureHelp

func (s *Session) SignatureHelp(
	doc *view.Document, viewID view.Id,
) (view.SignatureHelp, error)

SignatureHelp returns the signature help at the cursor for an invoked trigger

func (*Session) StopLanguageServers

func (s *Session) StopLanguageServers(
	doc *view.Document, names []string,
) ([]string, error)

StopLanguageServers shuts down the named language servers for the document

func (*Session) TriggerCompletions

func (s *Session) TriggerCompletions(
	doc *view.Document, viewID view.Id,
) (view.CompletionResult, error)

TriggerCompletions requests character-triggered completion

func (*Session) TriggerSignatureHelp

func (s *Session) TriggerSignatureHelp(
	doc *view.Document, viewID view.Id,
) (view.SignatureHelp, error)

TriggerSignatureHelp returns signature help triggered by the character before the cursor

func (*Session) WillCreateFile

func (s *Session) WillCreateFile(path string, dir bool) error

WillCreateFile applies any edits servers want made before creating path

func (*Session) WillDeleteFile

func (s *Session) WillDeleteFile(path string, dir bool) error

WillDeleteFile applies any edits servers want made before deleting path

func (*Session) WillRenameFile

func (s *Session) WillRenameFile(rename view.FileRename, dir bool) error

WillRenameFile applies any edits servers want made before renaming

func (*Session) WorkspaceCommands

func (s *Session) WorkspaceCommands(doc *view.Document) []string

WorkspaceCommands returns commands advertised by attached servers

func (*Session) WorkspaceSymbols

func (s *Session) WorkspaceSymbols(
	doc *view.Document, query string,
) ([]view.Symbol, error)

WorkspaceSymbols returns workspace symbols from all running servers

type TransportConfig

type TransportConfig struct {
	Context context.Context
	Name    string
	Server  language.Server
	Dir     string
	Handler protocol.Client
}

TransportConfig describes a language server process to start

type WorkspaceRequest

type WorkspaceRequest struct {
	FilePath       string
	Workspace      string
	WorkspaceIsCWD bool
	RootMarkers    []string
	RootDirs       []string
}

WorkspaceRequest carries parameters for resolving a project workspace root

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL