lsp

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package lsp provides a Language Server Protocol client for hawk. It manages LSP server subprocesses with refcounted connection pooling, idle reaping, and typed crash recovery.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrManagerClosed         = &LSPError{Code: "MANAGER_CLOSED", Message: "LSP manager is closed"}
	ErrLanguageNotConfigured = &LSPError{Code: "LANG_NOT_CONFIGURED", Message: "no LSP server configured for language"}
)

Errors

View Source
var ReadOnlyRetryTools = map[string]bool{
	"diagnostics":     true,
	"goto_definition": true,
	"find_references": true,
	"symbols":         true,
	"prepare_rename":  true,
	"status":          true,
}

ReadOnlyRetryTools contains tool names that are safe to retry on crash.

Functions

func ResetConfig

func ResetConfig()

ResetConfig clears the cached config (for testing).

Types

type Client

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

Client represents an LSP client connection.

func (*Client) Notify

func (c *Client) Notify(method string, params interface{})

Notify sends a notification (no response expected).

func (*Client) Request

func (c *Client) Request(method string, params interface{}) (*Response, error)

Request sends a request to an LSP server.

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 represents an LSP diagnostic.

type DiagnosticSeverity

type DiagnosticSeverity int

DiagnosticSeverity represents LSP diagnostic severity levels.

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

type LSPClient

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

LSPClient communicates with a single language server subprocess.

func NewLSPClient

func NewLSPClient(ctx context.Context, lang string, cfg ServerConfig) (*LSPClient, error)

NewLSPClient starts a language server subprocess and initializes the LSP protocol.

func (*LSPClient) Close

func (c *LSPClient) Close() error

Close shuts down the language server.

func (*LSPClient) Diagnostics

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

Diagnostics requests diagnostics for a file.

func (*LSPClient) DocumentSymbol

func (c *LSPClient) DocumentSymbol(ctx context.Context, uri string) ([]SymbolInformation, error)

DocumentSymbol returns all symbols in a document.

func (*LSPClient) FindReferences

func (c *LSPClient) FindReferences(ctx context.Context, uri string, line, char int) ([]Location, error)

FindReferences finds all references to a symbol at a position.

func (*LSPClient) GotoDefinition

func (c *LSPClient) GotoDefinition(ctx context.Context, uri string, line, char int) ([]Location, error)

GotoDefinition finds the definition of a symbol at a position.

func (*LSPClient) Language

func (c *LSPClient) Language() string

Language returns the language this client serves.

func (*LSPClient) PrepareRename

func (c *LSPClient) PrepareRename(ctx context.Context, uri string, line, char int) (*Range, error)

PrepareRename checks if a symbol at a position can be renamed.

func (*LSPClient) Rename

func (c *LSPClient) Rename(ctx context.Context, uri string, line, char int, newName string) (*WorkspaceEdit, error)

Rename renames a symbol at a position across the workspace.

type LSPConfig

type LSPConfig struct {
	Servers map[string]ServerConfig `json:"lsp"`
}

LSPConfig holds all configured language servers.

func LoadConfig

func LoadConfig(projectDir string) *LSPConfig

LoadConfig merges project-level (.agents/lsp.json) and global user-state LSP configuration. Project-level overrides global.

func (*LSPConfig) LanguageForExtension

func (c *LSPConfig) LanguageForExtension(ext string) string

LanguageForExtension returns the language name for a file extension, or empty string if no LSP server is configured for that extension.

func (*LSPConfig) ServerForFile

func (c *LSPConfig) ServerForFile(filePath string) (string, ServerConfig, bool)

ServerForFile returns the language and server config for a file path.

type LSPError

type LSPError struct {
	Code    string
	Message string
}

func (*LSPError) Error

func (e *LSPError) Error() string

type LSPManager

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

LSPManager manages a pool of language server connections.

func NewManager

func NewManager(cfg *LSPConfig) *LSPManager

NewManager creates an LSPManager with the given config.

func NewManagerFromProject

func NewManagerFromProject(projectDir string) *LSPManager

NewManagerFromProject creates an LSPManager loading config from project directory.

func (*LSPManager) Close

func (m *LSPManager) Close() error

Close shuts down all language server connections.

func (*LSPManager) Config

func (m *LSPManager) Config() *LSPConfig

Config returns the LSP configuration.

func (*LSPManager) Execute

func (m *LSPManager) Execute(ctx context.Context, lang string, readOnly bool, fn func(client *LSPClient) error) error

Execute runs fn with an LSPClient, handling acquire/release and crash recovery. readOnly indicates if the operation is idempotent (safe to retry once on crash).

func (*LSPManager) Status

func (m *LSPManager) Status() map[string]string

Status returns the state of all configured language servers.

type Location

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

Location represents an LSP location.

type ManagedClient

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

ManagedClient wraps an LSPClient with refcounting and idle tracking.

type Position

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

Position represents an LSP position.

type Range

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

Range represents an LSP range.

type RenameCapabilities

type RenameCapabilities struct {
	PrepareSupport bool `json:"prepareSupport,omitempty"`
}

RenameCapabilities describes rename support.

type Request

type Request struct {
	JSONRPC string      `json:"jsonrpc"`
	ID      int         `json:"id"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
}

Request is an LSP request.

type Response

type Response struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      int             `json:"id,omitempty"`
	Method  string          `json:"method,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *ResponseError  `json:"error,omitempty"`
}

Response is an LSP response.

type ResponseError

type ResponseError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

ResponseError is an LSP error.

type ServerConfig

type ServerConfig struct {
	Command    string   `json:"command"`
	Args       []string `json:"args,omitempty"`
	Extensions []string `json:"extensions"`
}

ServerConfig defines how to launch a language server for a given language.

type ServerManager

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

ServerManager manages LSP server connections.

func NewServerManager

func NewServerManager() *ServerManager

NewServerManager creates a new LSP server manager.

func (*ServerManager) IsRunning

func (m *ServerManager) IsRunning(name string) bool

IsRunning checks if a server is running.

func (*ServerManager) List

func (m *ServerManager) List() []string

List returns all running servers.

func (*ServerManager) Start

func (m *ServerManager) Start(name, command string, args ...string) error

Start starts an LSP server.

func (*ServerManager) Stop

func (m *ServerManager) Stop(name string) error

Stop stops an LSP server gracefully.

type SymbolInformation

type SymbolInformation struct {
	Name          string   `json:"name"`
	Kind          int      `json:"kind"`
	Location      Location `json:"location"`
	ContainerName string   `json:"containerName,omitempty"`
}

SymbolInformation represents an LSP symbol.

type TextEdit

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

TextEdit represents an LSP text edit.

type WorkspaceEdit

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

WorkspaceEdit represents an LSP workspace edit.

Jump to

Keyboard shortcuts

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