lsp

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SeverityError       = 1
	SeverityWarning     = 2
	SeverityInformation = 3
	SeverityHint        = 4
)

Diagnostic severities (LSP spec).

View Source
const (
	SymbolKindFile          = 1
	SymbolKindModule        = 2
	SymbolKindNamespace     = 3
	SymbolKindPackage       = 4
	SymbolKindClass         = 5
	SymbolKindMethod        = 6
	SymbolKindProperty      = 7
	SymbolKindField         = 8
	SymbolKindConstructor   = 9
	SymbolKindEnum          = 10
	SymbolKindInterface     = 11
	SymbolKindFunction      = 12
	SymbolKindVariable      = 13
	SymbolKindConstant      = 14
	SymbolKindString        = 15
	SymbolKindNumber        = 16
	SymbolKindBoolean       = 17
	SymbolKindArray         = 18
	SymbolKindObject        = 19
	SymbolKindKey           = 20
	SymbolKindNull          = 21
	SymbolKindEnumMember    = 22
	SymbolKindStruct        = 23
	SymbolKindEvent         = 24
	SymbolKindOperator      = 25
	SymbolKindTypeParameter = 26
)

LSP symbol kinds (subset of the LSP SymbolKind enum).

Variables

View Source
var KnownServers = []struct {
	Lang    string
	Command string
	Args    []string
}{
	{"go", "gopls", []string{"serve"}},
	{"typescript", "typescript-language-server", []string{"--stdio"}},
	{"python", "pyright-langserver", []string{"--stdio"}},
	{"python", "pylsp", []string{}},
	{"rust", "rust-analyzer", []string{}},
	{"c", "clangd", []string{}},
	{"cpp", "clangd", []string{}},
	{"java", "jdtls", []string{}},
	{"ruby", "solargraph", []string{"stdio"}},
}

KnownServers maps a language ID to the server binary and default args. AutoDetect scans PATH for these binaries in priority order — the first found for a language wins. Explicit Config.LSP entries always override auto-detected ones.

Functions

func AutoDetect

func AutoDetect(existing map[string]bool) map[string]ServerConfig

AutoDetect scans PATH for known language server binaries and returns configs for every language that has a server available. When multiple servers exist for the same language (e.g. pyright-langserver and pylsp for Python), the first found in KnownServers order wins.

existing is the set of languages already explicitly configured — those are skipped so explicit config always takes priority.

func DetectedServers

func DetectedServers(alreadyConfigured map[string]bool) []string

DetectedServers returns a human-readable list of auto-detected servers for boot messages. It scans PATH and returns lines like "go: gopls". Pass alreadyConfigured to skip languages with explicit config.

Types

type Client

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

Client is a JSON-RPC 2.0 connection to an LSP server over stdio.

func Start

func Start(ctx context.Context, cfg ServerConfig, rootDir string) (*Client, error)

Start launches a language server process and performs the LSP initialize/initialized handshake.

func (*Client) Close

func (c *Client) Close() error

Close sends shutdown/exit and kills the server process.

func (*Client) CodeActions

func (c *Client) CodeActions(ctx context.Context, file string, line, col int) ([]CodeAction, error)

CodeActions requests available code actions for a position. line is 1-indexed.

func (*Client) Definition

func (c *Client) Definition(ctx context.Context, file string, line, col int) ([]Location, error)

Definition requests go-to-definition at the given position. line is 1-indexed (converted to 0-indexed internally).

func (*Client) Diagnostics

func (c *Client) Diagnostics(ctx context.Context, file string) ([]Diagnostic, error)

Diagnostics fetches diagnostics for a file. Not all servers support pull-based diagnostics (textDocument/diagnostic); for those we return an empty list. This is best-effort.

func (*Client) DidClose

func (c *Client) DidClose(ctx context.Context, file string) error

DidClose notifies the server that a file was closed.

func (*Client) DidOpen

func (c *Client) DidOpen(ctx context.Context, file, lang, text string) error

DidOpen notifies the server that a file was opened.

func (*Client) DocumentSymbols

func (c *Client) DocumentSymbols(ctx context.Context, file string) ([]Symbol, error)

DocumentSymbols requests all symbols in a document.

func (*Client) Hover

func (c *Client) Hover(ctx context.Context, file string, line, col int) (*Hover, error)

Hover requests hover information at a position. line is 1-indexed.

func (*Client) References

func (c *Client) References(ctx context.Context, file string, line, col int) ([]Location, error)

References requests all references to the symbol at the given position. line is 1-indexed.

func (*Client) Rename

func (c *Client) Rename(ctx context.Context, file string, line, col int, newName string) (*WorkspaceEdit, error)

Rename requests a symbol rename at a position. line is 1-indexed.

type ClientCapabilities

type ClientCapabilities struct {
	TextDocument TextDocumentClientCapabilities `json:"textDocument"`
}

ClientCapabilities declares which features the client supports.

type CodeAction

type CodeAction struct {
	Title string         `json:"title"`
	Kind  string         `json:"kind,omitempty"`
	Edit  *WorkspaceEdit `json:"edit,omitempty"`
}

CodeAction describes a refactoring or fix-it action.

type CodeActionParams

type CodeActionParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Range        Range                  `json:"range"`
	Context      struct {
		Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
		Only        []string     `json:"only,omitempty"`
	} `json:"context"`
}

CodeActionParams requests code actions for a range.

type Diagnostic

type Diagnostic struct {
	Range    Range  `json:"range"`
	Severity int    `json:"severity"`
	Code     string `json:"code,omitempty"`
	Source   string `json:"source,omitempty"`
	Message  string `json:"message"`
}

Diagnostic is a compiler/linter message for a range.

type DocumentSymbolParams

type DocumentSymbolParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
}

DocumentSymbolParams requests symbols for a document.

type Hover

type Hover struct {
	Contents MarkupContent `json:"contents"`
	Range    *Range        `json:"range,omitempty"`
}

Hover is the result of a textDocument/hover request.

type HoverParams

type HoverParams = TextDocumentPositionParams

HoverParams requests hover information at a position.

type InitializeParams

type InitializeParams struct {
	RootURI      string             `json:"rootUri,omitempty"`
	Capabilities ClientCapabilities `json:"capabilities"`
	ProcessID    int                `json:"processId"`
}

InitializeParams is the LSP initialize request payload.

type Location

type Location struct {
	URI   string `json:"uri"`
	Range Range  `json:"range"`
}

Location is a URI + range, the result of definition/references.

type Manager

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

Manager owns the set of running LSP servers, one per language. It starts them on demand, caches the connections, and shuts them all down on Close.

func NewManager

func NewManager(configs map[string]ServerConfig, rootDir string) *Manager

NewManager creates a Manager from the given server configs.

func (*Manager) Close

func (m *Manager) Close() error

Close shuts down all running servers.

func (*Manager) Server

func (m *Manager) Server(lang string) (*Client, error)

Server returns a ready client for the given language, starting the server on first call. Subsequent calls return the cached client.

func (*Manager) ServerForFile

func (m *Manager) ServerForFile(path string) (*Client, error)

ServerForFile maps a file path to its language and returns the server.

func (*Manager) Status

func (m *Manager) Status() string

Status returns a human-readable report of configured and running servers.

type MarkupContent

type MarkupContent struct {
	Kind  string `json:"kind"` // "markdown" or "plaintext"
	Value string `json:"value"`
}

MarkupContent is markdown or plaintext content.

type Position

type Position struct {
	Line      int `json:"line"`
	Character int `json:"character"`
}

Position is a 0-indexed line and character position in a document. LSP uses 0-indexed lines; the chat tool converts from 1-indexed.

type Range

type Range struct {
	Start Position `json:"start"`
	End   Position `json:"end"`
}

Range is a span between two positions.

type ReferenceParams

type ReferenceParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Position     Position               `json:"position"`
	Context      struct {
		IncludeDeclaration bool `json:"includeDeclaration"`
	} `json:"context"`
}

ReferenceParams extends TextDocumentPositionParams with context.

type RenameParams

type RenameParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Position     Position               `json:"position"`
	NewName      string                 `json:"newName"`
}

RenameParams requests a symbol rename.

type ServerConfig

type ServerConfig struct {
	Command string
	Args    []string
	Env     map[string]string
}

ServerConfig describes how to start a language server.

type Symbol

type Symbol struct {
	Name           string   `json:"name"`
	Kind           int      `json:"kind"`
	Range          Range    `json:"range"`
	SelectionRange Range    `json:"selectionRange"`
	Children       []Symbol `json:"children,omitempty"`
}

Symbol is a document symbol (function, class, variable, etc.).

type TextDocumentClientCapabilities

type TextDocumentClientCapabilities struct {
	Definition         *struct{} `json:"definition,omitempty"`
	References         *struct{} `json:"references,omitempty"`
	DocumentSymbol     *struct{} `json:"documentSymbol,omitempty"`
	Hover              *struct{} `json:"hover,omitempty"`
	Rename             *struct{} `json:"rename,omitempty"`
	CodeAction         *struct{} `json:"codeAction,omitempty"`
	PublishDiagnostics *struct{} `json:"publishDiagnostics,omitempty"`
}

TextDocumentClientCapabilities enables per-feature capabilities.

type TextDocumentIdentifier

type TextDocumentIdentifier struct {
	URI string `json:"uri"`
}

TextDocumentIdentifier identifies a document by URI.

type TextDocumentPositionParams

type TextDocumentPositionParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Position     Position               `json:"position"`
}

TextDocumentPositionParams is a document + position, used by definition and references requests.

type TextEdit

type TextEdit struct {
	Range   Range  `json:"range"`
	NewText string `json:"newText"`
}

TextEdit is a range replacement.

type WorkspaceEdit

type WorkspaceEdit struct {
	Changes map[string][]TextEdit `json:"changes,omitempty"`
}

WorkspaceEdit is the result of a rename — a set of edits per file.

Jump to

Keyboard shortcuts

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