lsp

package
v0.0.0-beta Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package lsp provides Language Server Protocol integration for real-time diagnostics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client wraps a powernap LSP client with diagnostic caching and file tracking.

func NewClient

func NewClient(ctx context.Context, name string, server Server, rootURI string) (*Client, error)

NewClient creates a powernap LSP client, registers the diagnostic handler, and initializes the connection. Returns an error if initialization fails.

func (*Client) ClearDiagnostic

func (c *Client) ClearDiagnostic(uri protocol.DocumentURI)

ClearDiagnostic removes cached diagnostics for a single URI.

func (*Client) Diagnostics

func (c *Client) Diagnostics() map[protocol.DocumentURI][]protocol.Diagnostic

Diagnostics returns a snapshot of the cached diagnostics.

func (*Client) HandlesFile

func (c *Client) HandlesFile(path string) bool

HandlesFile checks whether this client handles the given file path based on configured file types and language detection.

func (*Client) Kill

func (c *Client) Kill()

Kill forcefully terminates the LSP server.

func (*Client) Name

func (c *Client) Name() string

Name returns the server name.

func (*Client) NotifyChange

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

NotifyChange reads the file from disk and sends a full-content change notification.

func (*Client) OpenFileOnDemand

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

OpenFileOnDemand opens the file in the LSP server if not already open.

func (*Client) Shutdown

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

Shutdown gracefully shuts down the LSP server.

func (*Client) State

func (c *Client) State() ServerState

State returns the current server state.

func (*Client) WaitForDiagnostics

func (c *Client) WaitForDiagnostics(ctx context.Context, timeout time.Duration)

WaitForDiagnostics polls until the diagnostic version changes or the timeout expires.

type Config

type Config struct {
	// Servers maps server names to their configuration.
	// Example: "gopls" → {Command: "gopls", Args: ["serve"], FileTypes: ["go", "mod"]}.
	Servers map[string]Server `yaml:"servers"`
}

Config holds LSP configuration passed to the Manager.

type DiagnosticsInputs

type DiagnosticsInputs struct {
	Path string `json:"path,omitempty" jsonschema:"description=File path to get diagnostics for. Omit for project-wide diagnostics."`
}

DiagnosticsInputs defines the parameters for the Diagnostics tool.

type DiagnosticsTool

type DiagnosticsTool struct {
	tool.Base
	// contains filtered or unexported fields
}

DiagnosticsTool returns LSP diagnostics for a file or the whole project.

func NewDiagnosticsTool

func NewDiagnosticsTool(manager *Manager) *DiagnosticsTool

NewDiagnosticsTool creates a Diagnostics tool backed by the given manager.

func (*DiagnosticsTool) Available

func (t *DiagnosticsTool) Available() bool

func (*DiagnosticsTool) Execute

func (t *DiagnosticsTool) Execute(ctx context.Context, args map[string]any) (string, error)

func (*DiagnosticsTool) Name

func (t *DiagnosticsTool) Name() string

func (*DiagnosticsTool) Sandboxable

func (t *DiagnosticsTool) Sandboxable() bool

Sandboxable returns false because the tool communicates with in-process LSP servers via the manager pointer, which does not exist in the sandboxed child process.

func (*DiagnosticsTool) Schema

func (t *DiagnosticsTool) Schema() tool.Schema

type FileInfo

type FileInfo struct {
	URI     protocol.DocumentURI
	Version int32
}

FileInfo tracks an open file's version for incremental change notifications.

type Manager

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

Manager manages LSP server lifecycle with lazy initialization. Servers are started on first file touch matching their file types and root markers.

func NewManager

func NewManager(cfg Config, workDir string) *Manager

NewManager creates a manager with the given configuration. No servers are started until Start() or NotifyChange() is called.

func (*Manager) AllDiagnostics

func (m *Manager) AllDiagnostics(ctx context.Context, path string) string

AllDiagnostics starts servers for the path, notifies changes, and returns formatted diagnostics. This is the entry point for the Diagnostics tool.

func (*Manager) DidOpen

func (m *Manager) DidOpen(ctx context.Context, path string)

DidOpen opens the file in all LSP servers that handle it. This is the entry point for Read (no change notification, no diagnostic wait).

func (*Manager) FormatDiagnostics

func (m *Manager) FormatDiagnostics(path string) string

FormatDiagnostics returns formatted diagnostics from all clients. If path is non-empty, only diagnostics for that file are included. Resolves relative paths to absolute for URI matching. Returns empty string if there are no diagnostics.

func (*Manager) HandlesFile

func (m *Manager) HandlesFile(server Server, path string) bool

HandlesFile checks if a server configuration matches the given file path by extension or detected language.

func (*Manager) HasRootMarkers

func (m *Manager) HasRootMarkers(server Server) bool

HasRootMarkers checks if at least one root marker is present in the working directory.

func (*Manager) NotifyChange

func (m *Manager) NotifyChange(ctx context.Context, path string)

NotifyChange opens the file on demand, sends a change notification, and waits for diagnostics on all clients handling the file. This is the primary entry point for Patch/Write tools.

func (*Manager) ResolvePath

func (m *Manager) ResolvePath(path string) string

ResolvePath converts a relative path to an absolute path using the working directory. Returns the path unchanged if it's already absolute or if resolution fails.

func (*Manager) RestartAll

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

RestartAll stops and restarts all previously configured servers.

func (*Manager) RestartServer

func (m *Manager) RestartServer(ctx context.Context, name string) error

RestartServer stops and restarts a single named server. Returns an error if the server name is not in the configuration.

func (*Manager) ServerCount

func (m *Manager) ServerCount() int

ServerCount returns the number of running LSP servers.

func (*Manager) ServerNames

func (m *Manager) ServerNames() []string

ServerNames returns the names of all configured servers.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, filePath string)

Start lazily starts LSP servers that handle the given file path. Already-running servers are skipped. Safe for concurrent use.

func (*Manager) StopAll

func (m *Manager) StopAll()

StopAll gracefully shuts down all running LSP servers.

type RestartInputs

type RestartInputs struct {
	Server string `json:"server,omitempty" jsonschema:"description=Server name to restart. Omit to restart all servers."`
}

RestartInputs defines the parameters for the LspRestart tool.

type RestartTool

type RestartTool struct {
	tool.Base
	// contains filtered or unexported fields
}

RestartTool restarts LSP servers.

func NewRestartTool

func NewRestartTool(manager *Manager) *RestartTool

NewRestartTool creates an LspRestart tool backed by the given manager.

func (*RestartTool) Available

func (t *RestartTool) Available() bool

func (*RestartTool) Execute

func (t *RestartTool) Execute(ctx context.Context, args map[string]any) (string, error)

func (*RestartTool) Name

func (t *RestartTool) Name() string

func (*RestartTool) Parallel

func (t *RestartTool) Parallel() bool

Parallel returns false because restarting LSP servers races with Diagnostics and Read's DidOpen.

func (*RestartTool) Sandboxable

func (t *RestartTool) Sandboxable() bool

Sandboxable returns false because the tool communicates with in-process LSP servers via the manager pointer, which does not exist in the sandboxed child process.

func (*RestartTool) Schema

func (t *RestartTool) Schema() tool.Schema

type Server

type Server struct {
	// Command is the executable to run (e.g. "gopls", "typescript-language-server").
	Command string `yaml:"command"`
	// Args are command-line arguments (e.g. ["serve"]).
	Args []string `yaml:"args"`
	// FileTypes lists file extensions or language identifiers this server handles.
	// Extensions can include the leading dot or not (e.g. ".go" or "go").
	// Empty means the server handles all file types.
	FileTypes []string `yaml:"file_types"`
	// RootMarkers are filenames that identify the project root (e.g. ["go.mod"]).
	// The server only starts if at least one marker is found in the working directory.
	// Empty means the server applies to any directory.
	RootMarkers []string `yaml:"root_markers"`
	// Settings are LSP workspace settings passed to the server.
	Settings map[string]any `yaml:"settings"`
	// InitOptions are LSP initialization options.
	InitOptions map[string]any `yaml:"init_options"`
	// Timeout is the initialization timeout in seconds. Default: 30.
	Timeout int `yaml:"timeout"`
	// Disabled skips this server during startup. Default: false.
	Disabled bool `yaml:"disabled"`
}

Server describes a single LSP server and how to match files to it.

func (Server) TimeoutOrDefault

func (s Server) TimeoutOrDefault() int

TimeoutOrDefault returns the configured timeout or 30 seconds.

type ServerState

type ServerState int

ServerState represents the lifecycle state of an LSP server.

const (
	StateStarting ServerState = iota
	StateReady
	StateError
	StateStopped
)

func (ServerState) String

func (s ServerState) String() string

String returns a human-readable state name.

Jump to

Keyboard shortcuts

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