lsp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package lsp gives a file's errors as language servers see them (ADR 0009): after a tool writes a file, the server for its language is told of the new content, and the errors it publishes for that file are added to the tool's result, in opencode's form. The servers run in the sandbox, live in the daemon across turns, and stop when not used for a while. A server that is not there, does not start, or does not answer in time adds nothing.

Index

Constants

View Source
const MaxErrors = 20

MaxErrors is how many of a file's errors are shown.

View Source
const Off = "KAIROCODE_LSP"

Off is the environment variable that, set to "off", starts no server (the tests of packages that write files set it).

Variables

View Source
var (
	Settle = 150 * time.Millisecond
	Wait   = 5 * time.Second
	Idle   = 10 * time.Minute
)

Timing (ADR 0009, after opencode): the errors are taken once the server has been quiet for Settle after publishing them for the file; nothing is added if they do not come within Wait. A server not used for Idle stops.

View Source
var Builtin = []Server{
	{ID: "gopls", Command: []string{"gopls"}, Extensions: []string{".go"},
		Roots: []string{"go.work", "go.mod"}, Caches: []string{"gopls", "go-build"}},
	{ID: "typescript", Command: []string{"typescript-language-server", "--stdio"},
		Extensions: []string{".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".mts", ".cts"},
		Roots:      []string{"package-lock.json", "bun.lockb", "bun.lock", "pnpm-lock.yaml", "yarn.lock", "package.json"}},
	{ID: "pyright", Command: []string{"pyright-langserver", "--stdio"}, Extensions: []string{".py", ".pyi"},
		Roots: []string{"pyproject.toml", "setup.py", "setup.cfg", "requirements.txt", "Pipfile", "pyrightconfig.json"}},
	{ID: "rust", Command: []string{"rust-analyzer"}, Extensions: []string{".rs"}, Roots: []string{"Cargo.toml"}},
	{ID: "clangd", Command: []string{"clangd"}, Extensions: []string{".c", ".cc", ".cpp", ".cxx", ".c++", ".h", ".hh", ".hpp", ".hxx"},
		Roots: []string{"compile_commands.json", "compile_flags.txt", ".clangd", "CMakeLists.txt", "Makefile"}},
}

Builtin are the servers known without settings, as opencode names them.

Functions

func FakeServer

func FakeServer()

FakeServer is a fake language server, for tests: a test binary runs it when started again with KC_FAKE_LSP=1. It notes each start in KC_FAKE_LSP_LOG and, for each document opened or changed, publishes no diagnostics, then those of KC_FAKE_LSP_DIAGS (a JSON array); with KC_FAKE_LSP_SILENT=1 it publishes nothing; with KC_FAKE_LSP_STUCK=1 it stops reading once initialized; with KC_FAKE_LSP_STALE=1 it publishes, after a change's diagnostics, the version before's again.

func Format

func Format(file string, diags []Diagnostic) string

Format puts a file's errors as opencode does; "" when it has none.

func ReadMessage

func ReadMessage(r *bufio.Reader) ([]byte, error)

ReadMessage reads one (the fake server of the tests).

func WriteMessage

func WriteMessage(w io.Writer, m any) error

WriteMessage writes one message of the base protocol (the fake server of the tests).

Types

type Diagnostic

type Diagnostic struct {
	Range struct {
		Start struct {
			Line      int `json:"line"`
			Character int `json:"character"`
		} `json:"start"`
	} `json:"range"`
	Severity int    `json:"severity"` // 1: error
	Message  string `json:"message"`
}

Diagnostic is one of a file's problems, as the server put it.

type Manager

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

Manager keeps the language servers of a process: one per server and root, started when first needed.

func NewManager

func NewManager(log *slog.Logger) *Manager

NewManager makes a manager; Close stops its servers.

func (*Manager) Close

func (m *Manager) Close()

Close stops every server.

func (*Manager) Errors

func (m *Manager) Errors(ctx context.Context, servers []Server, dir, file string, content []byte) string

Errors are the errors the server for file (an absolute path in the working directory dir) publishes for content, as text to add to a tool's result: "" if there are none, or no server, or none in time.

type Server

type Server struct {
	ID         string   `json:"id"`
	Command    []string `json:"command"`
	Extensions []string `json:"extensions"`
	// Roots are the files that mark a project's root, nearest first in
	// each directory, looked for up from the file (not above the working
	// directory). None found: the working directory.
	Roots []string          `json:"roots,omitempty"`
	Env   map[string]string `json:"env,omitempty"`
	// Initialization is the server's initializationOptions.
	Initialization json.RawMessage `json:"initialization,omitempty"`
	// Caches are directories under the user's cache directory the server
	// may write (its index), besides the working and temporary ones.
	Caches []string `json:"caches,omitempty"`
}

Server is how to run a language server, and for which files.

func For

func For(servers []Server, dir, file string) (Server, string, bool)

For is the server for file, and the root it runs in: the nearest directory from file up to dir that has one of its Roots, else dir.

func Servers

func Servers(user, project string) ([]Server, error)

Servers are the servers to use: the built-in ones as kairocode.json's "lsp" changes them, the user's file (user), then the project's (project). Either may say "lsp": false, to start none, or {"<id>": {"disabled": true}}, not that one; and set an existing server's "initialization". Only the user's own file may add a server or change one's "command", "extensions" or "env" (one not built in needs its command and extensions): a project does not choose the commands run on every write (ADR 0009, 追記). What a project sets that it may not is left out, and said in the error, with the servers still given.

Jump to

Keyboard shortcuts

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