lsp

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package lsp provides a JSON-RPC 2.0 client for Language Server Protocol servers (gopls, typescript-language-server, rust-analyzer, etc.).

Wire format: Content-Length framing over stdio.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LanguageID

func LanguageID(ext string) string

LanguageID returns the LSP languageId string for a file extension.

Types

type Client

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

Client wraps a language server subprocess and speaks JSON-RPC 2.0 over its stdin/stdout using the Content-Length framing defined by the LSP spec.

func NewClient

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

NewClient spawns the language server at cmd+args, performs the LSP initialize/initialized handshake, and returns a ready Client.

func NewClientFromPipes

func NewClientFromPipes(ctx context.Context, stdin io.WriteCloser, stdout io.ReadCloser) (*Client, error)

NewClientFromPipes creates a Client using caller-supplied stdin/stdout pipes. This is useful for testing with an in-process mock server. It performs the same initialize/initialized handshake as NewClient.

func (*Client) Close

func (c *Client) Close() error

Close sends the LSP shutdown + exit sequence and waits for the process.

func (*Client) Diagnostics

func (c *Client) Diagnostics(uri string) []Diagnostic

Diagnostics returns the most recently cached diagnostics for uri.

func (*Client) Notify

func (c *Client) Notify(method string, params json.RawMessage) error

Notify sends a JSON-RPC notification (no ID, no response expected).

func (*Client) Request

func (c *Client) Request(ctx context.Context, method string, params json.RawMessage) (json.RawMessage, error)

Request sends a JSON-RPC request and blocks until the server responds.

func (*Client) StoreDiagnostics

func (c *Client) StoreDiagnostics(uri string, diags []Diagnostic)

StoreDiagnostics manually sets the diagnostics cache for a URI. This is primarily useful in tests to simulate server push notifications.

type Diagnostic

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

Diagnostic is a single diagnostic message from the server.

type DiagnosticSeverity

type DiagnosticSeverity int

DiagnosticSeverity mirrors LSP DiagnosticSeverity.

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

func (DiagnosticSeverity) String

func (s DiagnosticSeverity) String() string

type DidCloseTextDocumentParams

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

DidCloseTextDocumentParams is the payload for textDocument/didClose.

type DidOpenTextDocumentParams

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

DidOpenTextDocumentParams is the payload for textDocument/didOpen.

type Hover

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

Hover is the result of a textDocument/hover request.

type InitializeParams

type InitializeParams struct {
	ProcessID    *int           `json:"processId"`
	RootURI      string         `json:"rootUri"`
	Capabilities map[string]any `json:"capabilities"`
}

InitializeParams is the payload for the initialize request.

type InitializeResult

type InitializeResult struct {
	Capabilities map[string]any `json:"capabilities"`
}

InitializeResult is the response to initialize.

type Location

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

Location is a URI + Range (e.g. a definition site).

type Manager

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

Manager owns one *Client per language and starts servers on demand.

func NewManager

func NewManager() *Manager

NewManager creates an empty Manager.

func (*Manager) Close

func (m *Manager) Close()

Close shuts down all running language servers.

func (*Manager) ServerFor

func (m *Manager) ServerFor(ctx context.Context, filePath string) (*Client, error)

ServerFor returns the running LSP client for the given file path, launching the appropriate language server if it has not been started yet. Returns an error if no known server exists for the file's extension, or if the server binary is not on PATH.

type MarkupContent

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

MarkupContent holds a marked-up string (plaintext or markdown).

type Position

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

Position is a zero-based line/character offset in a text document.

type PublishDiagnosticsParams

type PublishDiagnosticsParams struct {
	URI         string       `json:"uri"`
	Diagnostics []Diagnostic `json:"diagnostics"`
}

PublishDiagnosticsParams is sent as a server notification.

type Range

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

Range is a start/end pair of Positions.

type ReferenceContext

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

ReferenceContext is included in references requests.

type ReferenceParams

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

ReferenceParams extends TextDocumentPositionParams with context.

type TextDocumentIdentifier

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

TextDocumentIdentifier identifies a text 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 carries the full content of a document on open.

type TextDocumentPositionParams

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

TextDocumentPositionParams is the common position params for many requests.

Jump to

Keyboard shortcuts

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