lsp

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package lsp integrates Language Server Protocol (LSP) support into BroCode.

A language server (gopls for Go, typescript-language-server for TS, etc.) is spawned over stdio and queried for definition, references, hover and diagnostics — real code intelligence instead of grep-only exploration. The server is launched lazily on first use and kept alive for the session; if no server is available for the requested language the tools fail with a clear message telling the model to fall back to grep/glob/read_file.

Index

Constants

View Source
const DefaultIdleTimeout = 10 * time.Minute

DefaultIdleTimeout is how long a language server stays alive after its last use before it is shut down to free memory. 10 minutes covers a normal task burst while keeping idle processes bounded.

Variables

This section is empty.

Functions

func RegisterTools

func RegisterTools(r *tool.Registry, m *Manager)

RegisterTools registers the LSP intelligence tools into the registry. The tools share one Manager (one language server process per language) and fail with a clear message when no server is available so the model falls back to grep/glob/read_file.

Types

type AutoFixTool

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

AutoFixTool — apply all auto-fixable quick-fixes across the project in one shot.

func (*AutoFixTool) Description

func (t *AutoFixTool) Description() string

func (*AutoFixTool) Execute

func (t *AutoFixTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*AutoFixTool) Name

func (t *AutoFixTool) Name() string

func (*AutoFixTool) Parameters

func (t *AutoFixTool) Parameters() map[string]any

type Client

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

Client is a live connection to one language server.

type DefinitionTool

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

DefinitionTool — jump to where a symbol is defined.

func (*DefinitionTool) Description

func (t *DefinitionTool) Description() string

func (*DefinitionTool) Execute

func (t *DefinitionTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*DefinitionTool) Name

func (t *DefinitionTool) Name() string

func (*DefinitionTool) Parameters

func (t *DefinitionTool) Parameters() map[string]any

type DiagnosticsTool

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

DiagnosticsTool — compiler/linter errors for a file.

func (*DiagnosticsTool) Description

func (t *DiagnosticsTool) Description() string

func (*DiagnosticsTool) Execute

func (t *DiagnosticsTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*DiagnosticsTool) Name

func (t *DiagnosticsTool) Name() string

func (*DiagnosticsTool) Parameters

func (t *DiagnosticsTool) Parameters() map[string]any

type FixTool

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

FixTool — apply the language server's auto-fixable code actions to a file.

func (*FixTool) Description

func (t *FixTool) Description() string

func (*FixTool) Execute

func (t *FixTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*FixTool) Name

func (t *FixTool) Name() string

func (*FixTool) Parameters

func (t *FixTool) Parameters() map[string]any

type HoverTool

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

HoverTool — documentation and type info for a symbol.

func (*HoverTool) Description

func (t *HoverTool) Description() string

func (*HoverTool) Execute

func (t *HoverTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*HoverTool) Name

func (t *HoverTool) Name() string

func (*HoverTool) Parameters

func (t *HoverTool) Parameters() map[string]any

type Manager

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

Manager owns zero or more language server connections (one per language). Servers are spawned lazily on first use and reaped when idle, so a long session does not accumulate live language-server processes (the "unbounded memory growth" problem other agents have).

func NewManager

func NewManager() *Manager

NewManager creates an empty LSP manager with an idle reaper.

func (*Manager) ActiveServers

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

ActiveServers returns the languages whose server process is currently running (spawned by a previous lsp_* tool call this session).

func (*Manager) AutoFixAll

func (m *Manager) AutoFixAll(ctx context.Context, root string) (string, error)

AutoFixAll applies all auto-fixable quick-fixes across every file that has diagnostics, in a single call (one language-server session, one settle). This is the "batch clear" counterpart to lsp_fix: instead of calling lsp_fix per file, the engine fixes the whole project in one shot. Returns a per-file summary of what was applied.

func (*Manager) AvailableServers

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

AvailableServers returns the language names whose server binary is on PATH — these are the languages LSP tools can actually use right now.

func (*Manager) Close

func (m *Manager) Close()

Close shuts down all running language servers and stops the idle reaper.

func (*Manager) CodeAction

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

CodeAction applies the first auto-applicable code action the server offers for the file's current diagnostics — e.g. auto-import, organize imports, or a quick-fix rewrite — preferring the server's "preferred" action. The resulting edits are written to disk and recorded for the turn's diff, undo snapshots and verification. Returns a summary of what was applied, or the available action titles when none carried an auto-applyable edit.

func (*Manager) Definition

func (m *Manager) Definition(ctx context.Context, path string, line, col int) (string, error)

Definition returns the source location(s) of the symbol under the cursor.

func (*Manager) DeterministicFixAll

func (m *Manager) DeterministicFixAll(ctx context.Context, root string) (string, error)

DeterministicFixAll applies gopls source.fixAll across every supported file in the project in one call — the deterministic bulk clearing pass for mechanical warnings. It returns a per-file summary; the LLM only handles whatever remains (genuinely semantic issues) afterward.

func (*Manager) Diagnostics

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

Diagnostics returns the current diagnostics (errors/warnings) for a file.

func (*Manager) FixAllFile

func (m *Manager) FixAllFile(ctx context.Context, path string) (int, string, error)

FixAllFile clears every mechanically-fixable diagnostic in ONE file by asking gopls for quick-fixes and applying them one at a time, re-syncing the server and re-reading diagnostics after each. Crucially, every applied fix is build-gated: if applying it makes `go build` of the package fail, the edit is reverted and that diagnostic is skipped. This keeps the project always compiling while absorbing the bulk of the warnings (imports, Fprintf/Sprintf rewrites, CutPrefix, max/min, range loops, slices.Backward, etc.) — no LLM in the loop. Determinism + the build gate mean it can never leave broken code; anything that would break the build is left for a human/LLM. Returns the number of fixes applied and a summary.

func (*Manager) Hover

func (m *Manager) Hover(ctx context.Context, path string, line, col int) (string, error)

Hover returns the hover documentation for the symbol under the cursor.

func (*Manager) InstallHints

func (m *Manager) InstallHints() map[string]string

InstallHints returns, for each supported language whose server binary is missing, the official command to install it (empty map = all installed). The platform-dependent clangd hint is resolved here so callers (UI, tools) never need runtime.GOOS themselves.

func (*Manager) Outline

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

Outline returns the hierarchical symbol tree of a file (functions, types, methods, fields) via documentSymbol — a semantic map of a file's shape.

func (*Manager) References

func (m *Manager) References(ctx context.Context, path string, line, col int) (string, error)

References returns all references to the symbol under the cursor.

func (*Manager) Rename

func (m *Manager) Rename(ctx context.Context, path string, line, col int, newName string) (string, error)

Rename renames the symbol under the cursor across the whole project using the server's semantic rename, applying the resulting edits to disk. The verification ladder catches anything the rename missed. Returns a per-file summary of what changed.

func (*Manager) ScanDiagnostics

func (m *Manager) ScanDiagnostics(ctx context.Context, root string) (string, error)

ScanDiagnostics proactively scans a project for compiler/linter diagnostics: errors, warnings and deprecated usages across source files — a full health check without running a build. It opens at most scanMaxFiles supported files (dependency/build dirs skipped), gives the servers one short settle window, then aggregates everything they publish. Files whose language server is not installed are skipped silently. Returns a compact report, or a clean line when no issues were found.

func (*Manager) Symbols

func (m *Manager) Symbols(ctx context.Context, path, query string) (string, error)

Symbols searches the whole workspace for symbols matching a name using workspace/symbol — semantic lookup that does not need a cursor position (unlike definition/references). path anchors the language server to use.

func (*Manager) WarmUp

func (m *Manager) WarmUp(root string)

WarmUp spawns language servers for supported files under root in the background, so the first lsp_* call of the session is instant instead of paying spawn + initialize + (cache-warm) index on first use — the persistent per-session gap. Servers the user never touches are shut down by the idle reaper after idleTimeout, so unused warm-up costs nothing in the long run. Never blocks; errors are ignored (lazy spawn still happens on first call).

type OutlineTool

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

OutlineTool — hierarchical symbol tree of a file.

func (*OutlineTool) Description

func (t *OutlineTool) Description() string

func (*OutlineTool) Execute

func (t *OutlineTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*OutlineTool) Name

func (t *OutlineTool) Name() string

func (*OutlineTool) Parameters

func (t *OutlineTool) Parameters() map[string]any

type ReferencesTool

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

ReferencesTool — find every place a symbol is used.

func (*ReferencesTool) Description

func (t *ReferencesTool) Description() string

func (*ReferencesTool) Execute

func (t *ReferencesTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*ReferencesTool) Name

func (t *ReferencesTool) Name() string

func (*ReferencesTool) Parameters

func (t *ReferencesTool) Parameters() map[string]any

type RenameTool

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

RenameTool — semantic rename across the project.

func (*RenameTool) Description

func (t *RenameTool) Description() string

func (*RenameTool) Execute

func (t *RenameTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*RenameTool) Name

func (t *RenameTool) Name() string

func (*RenameTool) Parameters

func (t *RenameTool) Parameters() map[string]any

type ScanTool

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

ScanTool — project-wide health check: errors, warnings, deprecated APIs.

func (*ScanTool) Description

func (t *ScanTool) Description() string

func (*ScanTool) Execute

func (t *ScanTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*ScanTool) Name

func (t *ScanTool) Name() string

func (*ScanTool) Parameters

func (t *ScanTool) Parameters() map[string]any

type ServerSpec

type ServerSpec struct {
	Language string // display name, e.g. "go"
	Command  string // binary, e.g. "gopls"
	Args     []string
}

ServerSpec describes how to spawn a language server for a language.

type SymbolsTool

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

SymbolsTool — find symbols by name, no cursor needed.

func (*SymbolsTool) Description

func (t *SymbolsTool) Description() string

func (*SymbolsTool) Execute

func (t *SymbolsTool) Execute(ctx context.Context, argsJSON string) (string, error)

func (*SymbolsTool) Name

func (t *SymbolsTool) Name() string

func (*SymbolsTool) Parameters

func (t *SymbolsTool) Parameters() map[string]any

Jump to

Keyboard shortcuts

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