lsp

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package lsp provides an LSP client for code intelligence. It communicates with language servers via JSON-RPC 2.0 over stdin/stdout.

Index

Constants

This section is empty.

Variables

View Source
var ErrServerDied = errors.New("language server died")

ErrServerDied is returned when the language server process exits unexpectedly.

Functions

func FindSymbolColumn

func FindSymbolColumn(file string, line int, symbol string) (int, error)

FindSymbolColumn finds the column (0-based) of a symbol on a given line.

func FormatHover

func FormatHover(hover *HoverResult) string

FormatHover formats hover information.

func FormatLocations

func FormatLocations(locs []Location, cwd string, contextLines int) string

FormatLocations formats a list of locations as readable text.

func FormatSymbols

func FormatSymbols(symbols []DocumentSymbol, cwd string) string

FormatSymbols formats document symbols as a structured list.

func LanguageForFile

func LanguageForFile(file string) string

LanguageForFile returns the language ID for the given file path.

func URIToPath

func URIToPath(uri string) string

URIToPath converts a file:// URI to a filesystem path.

Types

type CapabilitySupport

type CapabilitySupport struct {
	DynamicRegistration bool `json:"dynamicRegistration"`
}

CapabilitySupport is a generic capability flag.

type Client

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

Client is an LSP client connected to a language server process.

func NewClient

func NewClient(ctx context.Context, command string, args ...string) (*Client, error)

NewClient spawns a language server process and returns a connected client.

func (*Client) Definition

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

Definition returns definition locations for the symbol at the given position.

func (*Client) DidClose

func (c *Client) DidClose(file string) error

DidClose notifies the server that a file has been closed.

func (*Client) DidOpen

func (c *Client) DidOpen(ctx context.Context, file, languageID, content string) error

DidOpen notifies the server that a file has been opened.

func (*Client) DocumentSymbols

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

DocumentSymbols returns symbols defined in the given file.

func (*Client) Hover

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

Hover returns hover information for the symbol at the given position.

func (*Client) Initialize

func (c *Client) Initialize(ctx context.Context, rootPath string) error

Initialize performs the LSP initialize/initialized handshake.

func (*Client) References

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

References returns all reference locations for the symbol at the given position.

func (*Client) Shutdown

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

Shutdown sends the shutdown request and exit notification.

type ClientCapabilities

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

ClientCapabilities describes client capabilities.

type DidCloseTextDocumentParams

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

DidCloseTextDocumentParams for textDocument/didClose.

type DidOpenTextDocumentParams

type DidOpenTextDocumentParams struct {
	TextDocument TextDocumentItem `json:"textDocument"`
}

DidOpenTextDocumentParams for textDocument/didOpen.

type DocumentSymbol

type DocumentSymbol struct {
	Name           string           `json:"name"`
	Detail         string           `json:"detail,omitempty"`
	Kind           SymbolKind       `json:"kind"`
	Range          Range            `json:"range"`
	SelectionRange Range            `json:"selectionRange"`
	Children       []DocumentSymbol `json:"children,omitempty"`
}

DocumentSymbol represents a symbol in a document (hierarchical).

type DocumentSymbolParams

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

DocumentSymbolParams for textDocument/documentSymbol.

type DocumentSymbolSupport

type DocumentSymbolSupport struct {
	DynamicRegistration               bool `json:"dynamicRegistration"`
	HierarchicalDocumentSymbolSupport bool `json:"hierarchicalDocumentSymbolSupport"`
}

DocumentSymbolSupport describes documentSymbol capabilities.

type HoverResult

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

HoverResult is the response to textDocument/hover.

type InitializeParams

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

InitializeParams for the initialize request.

type InitializeResult

type InitializeResult struct {
	Capabilities ServerCapabilities `json:"capabilities"`
}

InitializeResult is the response to initialize.

type Location

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

Location represents a location in a document.

type LocationLink struct {
	TargetURI   string `json:"targetUri"`
	TargetRange Range  `json:"targetRange"`
}

LocationLink is an alternative definition response format.

type Manager

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

Manager manages language server connections, one per language.

func NewManager

func NewManager(cwd string) *Manager

NewManager creates a new server manager rooted at the given directory.

func (*Manager) CWD

func (m *Manager) CWD() string

CWD returns the root working directory (immutable).

func (*Manager) EnsureFileOpen

func (m *Manager) EnsureFileOpen(ctx context.Context, client *Client, file, lang string) error

EnsureFileOpen sends didOpen for the file if not already open.

func (*Manager) ForFile

func (m *Manager) ForFile(ctx context.Context, file string) (*Client, string, error)

ForFile returns an LSP client for the given file, starting the server if needed.

func (*Manager) Shutdown

func (m *Manager) Shutdown(ctx context.Context)

Shutdown stops all running language servers.

type MarkupContent

type MarkupContent struct {
	Kind  string `json:"kind"`
	Value string `json:"value"`
}

MarkupContent represents hover/documentation content.

type MissingServer added in v0.18.1

type MissingServer struct {
	Language        string   `json:"language"`
	TriedCommands   []string `json:"tried_commands"`
	FoundExtensions []string `json:"found_extensions"`
}

MissingServer reports source files for a language whose configured LSP command is not available on PATH.

type Position

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

Position in a text document (0-based line and character).

type Querier

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

Querier implements in-process LSP reference lookups via a shared Manager. Spawn one Manager per repomap invocation; all symbol queries reuse it.

func NewQuerier

func NewQuerier(mgr *Manager) *Querier

NewQuerier returns a Querier backed by the given Manager.

func (*Querier) Refs

func (q *Querier) Refs(ctx context.Context, file string, line int, symbol string) ([]RefLocation, error)

Refs returns all reference locations for the named symbol at file:line. line is 1-based (same convention as the --calls pipeline).

type Range

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

Range in a text document.

type RefLocation

type RefLocation struct {
	File   string `json:"file"`
	Line   int    `json:"line"`
	Column int    `json:"column"`
}

RefLocation is a source position returned by a refs query. It mirrors the JSON shape used by the --calls pipeline.

type ReferenceContext

type ReferenceContext struct {
	IncludeDeclaration bool `json:"includeDeclaration"`
}

ReferenceContext controls whether the declaration is included in references.

type ReferenceParams

type ReferenceParams struct {
	TextDocumentPositionParams
	Context ReferenceContext `json:"context"`
}

ReferenceParams extends TextDocumentPositionParams with reference context.

type ServerCapabilities

type ServerCapabilities struct {
	DefinitionProvider     any `json:"definitionProvider,omitempty"`
	ReferencesProvider     any `json:"referencesProvider,omitempty"`
	HoverProvider          any `json:"hoverProvider,omitempty"`
	DocumentSymbolProvider any `json:"documentSymbolProvider,omitempty"`
}

ServerCapabilities describes what the server supports.

type ServerConfig

type ServerConfig struct {
	Command string
	Args    []string
}

ServerConfig describes how to start a language server.

type StatusReport added in v0.18.1

type StatusReport struct {
	Root    string          `json:"root"`
	Servers []StatusServer  `json:"servers"`
	Missing []MissingServer `json:"missing"`
}

StatusReport describes LSP coverage detected from source files and project markers without starting any language servers.

func DetectStatus added in v0.18.1

func DetectStatus(ctx context.Context, root string) (StatusReport, error)

DetectStatus scans root for source files and project markers, then reports which configured language servers are available. It does not start servers.

type StatusServer added in v0.18.1

type StatusServer struct {
	Language  string   `json:"language"`
	Root      string   `json:"root"`
	Command   string   `json:"command"`
	Args      []string `json:"args,omitempty"`
	FileTypes []string `json:"file_types"`
}

StatusServer is one detected language server root with an available command.

type SymbolInformation

type SymbolInformation struct {
	Name     string     `json:"name"`
	Kind     SymbolKind `json:"kind"`
	Location Location   `json:"location"`
}

SymbolInformation is the flat (non-hierarchical) symbol format.

type SymbolKind

type SymbolKind int

SymbolKind constants.

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

func (SymbolKind) String

func (k SymbolKind) String() string

String returns the human-readable name of the symbol kind.

type TextDocumentClientCapabilities

type TextDocumentClientCapabilities struct {
	Definition     CapabilitySupport     `json:"definition,omitempty"`
	References     CapabilitySupport     `json:"references,omitempty"`
	Hover          CapabilitySupport     `json:"hover,omitempty"`
	DocumentSymbol DocumentSymbolSupport `json:"documentSymbol,omitempty"`
}

TextDocumentClientCapabilities describes text document capabilities.

type TextDocumentIdentifier

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

TextDocumentIdentifier identifies a document by URI.

type TextDocumentItem

type TextDocumentItem struct {
	URI        string `json:"uri"`
	LanguageID string `json:"languageId"`
	Version    int    `json:"version"`
	Text       string `json:"text"`
}

TextDocumentItem represents a document with content.

type TextDocumentPositionParams

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

TextDocumentPositionParams is a common base for position-based requests.

Jump to

Keyboard shortcuts

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