lsp

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package lsp provides framework-free language-server lifecycle leaves.

Index

Constants

View Source
const (
	DefaultInitializeTimeout = 10 * time.Second
	DefaultRequestTimeout    = 5 * time.Second
	DefaultFailureLimit      = 3
)
View Source
const DefaultDiagnosticsPerFile = 20
View Source
const (
	DefaultMaxMessageBytes = int64(4 << 20)
)

Variables

View Source
var (
	ErrManagerClosed = errors.New("LSP manager is closed")
	ErrUnknownServer = errors.New("unknown LSP server")
)
View Source
var (
	// ErrInvalidURI indicates a non-file or malformed document URI.
	ErrInvalidURI = errors.New("invalid LSP file URI")
	// ErrInvalidPosition indicates a byte offset or LSP position that does not
	// identify a Unicode boundary in the supplied document.
	ErrInvalidPosition = errors.New("invalid LSP position")
)
View Source
var (
	ErrMessageTooLarge    = errors.New("LSP message exceeds limit")
	ErrMalformedTransport = errors.New("malformed LSP transport")
)

Functions

func FileURIToPath

func FileURIToPath(value string) (string, error)

FileURIToPath converts a file URI to a native path. Drive-letter URIs remain recognizable on non-Windows hosts so portable fixture data can be decoded.

func OffsetForPosition

func OffsetForPosition(content []byte, position protocol.Position, encoding protocol.PositionEncodingKind) (int, error)

OffsetForPosition converts an LSP position to a byte offset.

func PathToFileURI

func PathToFileURI(path string) (string, error)

PathToFileURI converts an absolute native or Windows path to a file URI.

func PositionForOffset

func PositionForOffset(content []byte, offset int, encoding protocol.PositionEncodingKind) (protocol.Position, error)

PositionForOffset converts a byte offset to the requested LSP encoding.

func SelectWorkspaceRoot

func SelectWorkspaceRoot(workspaceDir, path string, markers []string) (string, error)

SelectWorkspaceRoot checks markers in precedence order and returns the nearest ancestor containing the first marker found anywhere beneath the workspace boundary. If none match, it returns workspaceDir.

Types

type Client

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

Client is one initialized server/root lifecycle.

func (*Client) Call

func (client *Client) Call(ctx context.Context, method string, params, result any) (success bool)

Call performs a bounded request. Runtime failure returns false after warning instead of leaking a server outage into the host operation.

func (*Client) Diagnostics

func (client *Client) Diagnostics(path string) []Diagnostic

Diagnostics returns a defensive deterministic snapshot for path.

func (*Client) DidChange

func (client *Client) DidChange(ctx context.Context, path string, text []byte) bool

DidChange publishes a full-text change with a monotonic version.

func (*Client) DidClose

func (client *Client) DidClose(ctx context.Context, path string) bool

DidClose ends synchronization and clears cached diagnostics.

func (*Client) DidOpen

func (client *Client) DidOpen(ctx context.Context, path, languageID string, text []byte) bool

DidOpen starts full-text synchronization at document version one.

func (*Client) Notify

func (client *Client) Notify(ctx context.Context, method string, params any) (success bool)

Notify performs a bounded notification with the same no-op degradation.

func (*Client) PositionEncoding

func (client *Client) PositionEncoding() protocol.PositionEncodingKind

PositionEncoding returns the encoding negotiated during initialize.

func (*Client) Root

func (client *Client) Root() string

Root returns the resolved workspace root used by the server.

func (*Client) Server

func (client *Client) Server() Server

Server returns the immutable server configuration.

type Decoration

type Decoration struct {
	Diagnostics []Diagnostic
	Text        string
}

Decoration is the deterministic LSP addition for one successful mutation.

type Diagnostic

type Diagnostic struct {
	Path     string                      `json:"path"`
	Start    protocol.Position           `json:"start"`
	End      protocol.Position           `json:"end"`
	Severity protocol.DiagnosticSeverity `json:"severity"`
	Code     string                      `json:"code,omitempty"`
	Source   string                      `json:"source,omitempty"`
	Message  string                      `json:"message"`
}

Diagnostic is Plasmid's deterministic, JSON-safe diagnostic projection.

func NormalizeDiagnostics

func NormalizeDiagnostics(rootDir, documentURI string, values []protocol.Diagnostic, maximum int) ([]Diagnostic, error)

NormalizeDiagnostics confines, sorts, deduplicates, and bounds diagnostics for one document URI.

type Enforcer

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

Enforcer synchronizes successful write/edit touches and correlates their diagnostics with the exact tool invocation that caused the document version.

func NewEnforcer

func NewEnforcer(options EnforcerOptions) (*Enforcer, error)

NewEnforcer subscribes to the shared touch bus without starting a server.

func (*Enforcer) Await

func (enforcer *Enforcer) Await(ctx context.Context, sessionID, invocationID string) (Decoration, bool)

Await consumes one invocation receipt and waits within the configured settle bound.

func (*Enforcer) Close

func (enforcer *Enforcer) Close() error

Close idempotently unsubscribes and releases pending invocation receipts.

func (*Enforcer) Drop

func (enforcer *Enforcer) Drop(sessionID, invocationID string)

Drop releases any receipt that cannot reach a successful after-tool callback.

func (*Enforcer) ObserveTouch

func (enforcer *Enforcer) ObserveTouch(ctx context.Context, touch workspace.Touch)

ObserveTouch implements workspace.TouchObserver.

func (*Enforcer) Status

func (enforcer *Enforcer) Status() string

Status returns the current prompt line for automatic LSP mode.

type EnforcerOptions

type EnforcerOptions struct {
	WorkspaceDir  string
	Touches       *workspace.TouchBus
	Registry      Registry
	Manager       enforcementManager
	SettleTimeout time.Duration
	Output        outputlimit.Policy
	Warnings      warning.Warner
	Maximum       int
}

EnforcerOptions binds the framework-free LSP lifecycle to workspace touches.

type LookPathFunc

type LookPathFunc func(string) (string, error)

LookPathFunc is the executable-detection seam. Production uses exec.LookPath; tests can prove that detection never installs anything.

type Manager

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

Manager owns lazy language-server processes independently of any Harness.

func NewManager

func NewManager(parent context.Context, registry Registry, options ManagerOptions) (*Manager, error)

NewManager creates a lifecycle owner. No executable lookup or process start occurs until Start is called.

func (*Manager) ActiveServers

func (manager *Manager) ActiveServers() []string

ActiveServers returns deterministic server IDs for live clients.

func (*Manager) Close

func (manager *Manager) Close() error

Close idempotently stops all owned transports.

func (*Manager) Start

func (manager *Manager) Start(ctx context.Context, serverID, rootDir string) (*Client, error)

Start returns the lazily initialized client for a server/root pair. Missing executables and server failures return (nil, nil) after one structured warning; configuration and caller cancellation remain explicit errors.

type ManagerOptions

type ManagerOptions struct {
	Warnings           warning.Warner
	LookPath           LookPathFunc
	Start              StartFunc
	InitializeTimeout  time.Duration
	RequestTimeout     time.Duration
	FailureLimit       int
	MaxMessageBytes    int64
	DiagnosticsPerFile int
}

ManagerOptions controls bounded LSP lifecycle behavior.

type MessageHandler

type MessageHandler func(context.Context, string, json.RawMessage) (any, error)

MessageHandler receives server-to-client calls. Its result is used only for requests; notification results are discarded.

type RPCTransport

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

RPCTransport is a bounded sourcegraph/jsonrpc2 connection.

func NewRPCTransport

func NewRPCTransport(ctx context.Context, connection io.ReadWriteCloser, maximum int64, handler MessageHandler) (*RPCTransport, error)

NewRPCTransport constructs a bounded Content-Length framed connection. The context owns the connection lifetime.

func (*RPCTransport) Call

func (transport *RPCTransport) Call(ctx context.Context, method string, params, result any) error

Call sends one bounded LSP request and decodes its result with the protocol package's union-aware codec.

func (*RPCTransport) Close

func (transport *RPCTransport) Close() error

Close closes the connection.

func (*RPCTransport) Done

func (transport *RPCTransport) Done() <-chan struct{}

Done closes when the underlying JSON-RPC stream disconnects.

func (*RPCTransport) Notify

func (transport *RPCTransport) Notify(ctx context.Context, method string, params any) error

Notify sends one bounded LSP notification.

type Registry

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

Registry is an immutable, deterministic language-server registry.

func DefaultRegistry

func DefaultRegistry() Registry

DefaultRegistry returns the built-in registry.

func MergeRegistry

func MergeRegistry(entries []Server, warnings warning.Warner) Registry

MergeRegistry overlays entries on the built-ins by server ID. Invalid entries are skipped and reported without invalidating unrelated servers.

func (Registry) Match

func (registry Registry) Match(path string) []Server

Match returns enabled servers that own the path's extension.

func (Registry) Server

func (registry Registry) Server(id string) (Server, bool)

Server returns a defensive copy of the named server.

func (Registry) Servers

func (registry Registry) Servers() []Server

Servers returns defensive copies in server-ID order.

type Server

type Server struct {
	ID          string
	Command     string
	Args        []string
	Extensions  []string
	RootMarkers []string
	Disabled    bool
}

Server describes one language-server executable and the files it owns.

type StartFunc

StartFunc is the lazy process/transport seam.

type Transport

type Transport interface {
	Call(context.Context, string, any, any) error
	Notify(context.Context, string, any) error
	Done() <-chan struct{}
	Close() error
}

Transport is the narrow fakeable JSON-RPC seam used by the LSP lifecycle.

Jump to

Keyboard shortcuts

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