lsp

package
v0.1.0-rc.5 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 17 Imported by: 1 module, 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.

README

Pencode LSP

此包实现独立于传输的 LSP JSON-RPC 客户端,生产环境通过远程 stdio 流通信;扩展通过 peb.LanguageServer 声明服务,宿主管理生命周期。协议参考 LSP 3.17。

扩展接入

一个扩展可以贡献多个服务。每个服务提供 ID、展示名称、languageId、文件名/后缀 selector、启动程序和参数,以及可选的 initializationOptions / configuration。示例见 Go 扩展 和 peb 协议。服务可使用服务端系统 PATH 内的程序,或扩展内附带的可执行文件。宿主不会自动安装语言服务器。

服务按需启动,一个项目根目录和服务贡献共享一个进程。扩展禁用、移除、项目关闭和最终视图销毁都会停止服务。初始化最多 10 秒、普通请求最多 30 秒、退出宽限 1 秒;请求取消不关闭服务。失败会显示在工作台状态中,可通过禁用再启用扩展重新启动。

协议接口

  • Reader / Writer:Content-Length 分帧,处理拆包、短写、UTF-8 编码和大小限制;缓存后的分帧零分配。
  • Conn.Call / Conn.Notify:任意标准或自定义方法;保留 raw JSON、字符串/数字服务端 ID、结果联合类型和结构化错误。没有固定方法白名单。
  • Conn.BeginCall / PendingCall.Wait:拆开发送与响应等待。BeginCall 返回前已将请求放入有序发送队列,调度器可继续发送文档通知;每次成功调用后必须执行一次 Wait,包括取消的请求,以清理等待状态。
  • Request[P, R]:泛型请求接口,按扩展实际使用的参数、结果类型解码。
  • Handler:服务端请求与通知;响应读循环独立于处理器,处理器可以发起嵌套请求。未知请求返回 MethodNotFound,未知通知无需响应。
  • 请求 context 取消发送 $/cancelRequest;服务端取消会取消相应 handler context。连接断开释放所有等待请求并关闭进程管道。
  • Client:initialize / initialized、shutdown / exit、真实服务能力、UTF-16/UTF-8/UTF-32 编码协商、配置查询/更新、workspaceFolders、动态能力注册/注销,以及 workDoneProgress/create。初始化 context 只约束初始化。
  • SyncDocument / CloseDocument / WillSave / DidSaveText:依服务端 textDocumentSync 发送打开、完整/增量修改、关闭和保存。版本单调递增,增量修改保持 UTF-8 rune 和 CRLF 边界。
  • ApplyTextEdits:先检查所有范围和重叠,再一次生成结果;位置转换只正向扫描文档一次。同位置插入保持顺序。
  • DecodeSemanticTokens / ApplySemanticDelta:检查相对位置、token 类型、字符边界和增量编辑。
stream, err := backend.OpenProcess(ctx, projectPath, "my-language-server")
// 检查 err;进程由 pencode-server 持有。
client, err := lsp.Connect(ctx, lsp.Options{
    Root: projectPath,
    Handler: handleServerMessage,
}, stream)
// 检查 err;应用退出时用独立、有期限的 context 调用 client.Shutdown。
result, err := lsp.Request[lsp.TextDocumentPositionParams, json.RawMessage](
    ctx, client.Conn, "textDocument/hover", params,
)

初始化 processId 为 null,避免把 GUI 的本地 PID 传给另一台机器上的语言服务;进程生命周期由服务端管道控制。文件 URI 使用服务端路径,POSIX 绝对路径和 Windows 盘符路径的编码、解码不依赖 GUI 所在系统;解码统一返回 / 分隔路径。SSH 重连后原 LSP 进程和句柄失效,按需创建新进程并同步当前 GUI 文档。

Options.Capabilities 可以递归扩展初始化能力。调用方自行定义高级或自定义请求的参数/结果类型,处理对应服务端回调;包括 notebook、inlineValue、linkedEditingRange、onTypeFormatting、文件操作和自定义命令等。$/progress 中的 work done 和 partial result 通知会原样交给 Handler。能力声明必须与调用方实际处理的功能一致。

原生工作台

工作台按服务端声明选择 provider,后台通信不执行渲染或直接修改文档。结果通过有界队列回到 UI 线程。支持的操作包含补全与 resolve、悬浮信息、参数提示、定义/声明/类型/实现/引用跳转、文档/工作区符号、重命名、文档/选区格式化、代码操作及 resolve/executeCommand、Code Lens、调用/类型层级、选择范围、文档高亮位置与诊断。

push 诊断按 URI 和版本保存,文档修改清除旧诊断;也可手动请求 pull 诊断。语义 token 自动请求 full / delta / range,增量解码后与现有语法颜色合并;改变主题重建颜色,不重复请求服务;仅保留当前原生文档的语义绘制缓存,最多 65536 个 token。空闲轮询和缓存后的语义颜色读取不分配。

多文件 WorkspaceEdit 在所有文档通过版本、范围校验后才发布;未打开的文件成为未保存标签,所有编辑保留撤销和未保存提示,不直接写磁盘。资源 create/rename/delete 不在原生 workspaceEdit 能力声明中,会明确返回失败。保存传递实际保存快照;服务注册文件监听后,扩展可以用通用 Conn API 实现其声明的文件通知。

内嵌提示、折叠区域、文档链接目前通过结果面板展示协议数据。工作台未提供 notebook 编辑、代码折叠布局、snippet 占位符交互、外部 URL 打开、逐字符格式化、文件系统监听和资源文件操作,也不会宣称这些能力。原生补全仅声明纯文本插入,语义颜色使用已有主题 scope;不解析 semanticTokenColors。扩展自定义编辑器可以使用此协议包补充其所需界面和方法。

验证

在仓库根目录运行串行定向检查,不启动编辑器截图:

go test -p 1 ./lsp ./peb ./extensions/... ./workbench .
go vet -p 1 ./lsp ./peb ./extensions/... ./workbench .
git submodule foreach 'GOWORK=off go test ./... && GOWORK=off go vet ./...'

测试包含独立假服务进程、双向请求、初始化期间的配置查询、取消和断开、Unicode/CRLF 随机增量修改、事务编辑、过期结果、卸载回收及零分配检查。若 PATH 有 gopls,还会运行真实服务的格式化、定义跳转和工作台生命周期测试;无 gopls 时只跳过这两项。

Documentation

Overview

Package lsp implements the Language Server Protocol over a full-duplex, cancellable JSON-RPC connection. Unknown methods and extension fields remain available through Call and Notify rather than a closed method enumeration.

Index

Constants

View Source
const (
	ParseError       = -32700
	InvalidRequest   = -32600
	MethodNotFound   = -32601
	InvalidParams    = -32602
	InternalError    = -32603
	RequestCancelled = -32800
	ContentModified  = -32801
)
View Source
const MaxMessage = 32 << 20

Variables

View Source
var ErrClosed = errors.New("lsp: connection closed")
View Source
var SemanticTokenModifiers = []string{"declaration", "definition", "readonly", "static", "deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary"}
View Source
var SemanticTokenTypes = []string{"namespace", "type", "class", "enum", "interface", "struct", "typeParameter", "parameter", "variable", "property", "enumMember", "event", "function", "method", "macro", "keyword", "modifier", "comment", "string", "number", "regexp", "operator", "decorator"}

Functions

func ApplySemanticDelta

func ApplySemanticDelta(previous []uint32, edits []SemanticTokensEdit) ([]uint32, error)

func ApplyTextEdits

func ApplyTextEdits(content string, edits []TextEdit, encoding PositionEncoding) (string, error)

ApplyTextEdits validates the entire edit set before creating a new string. All endpoints are resolved in a single forward scan. Sorting costs O(m log m) for m edits, and scanning/copying costs O(n) for n document bytes.

func OffsetAt

func OffsetAt[V piecetree.Value](value V, p Position, encoding PositionEncoding) (int, error)

func Request

func Request[P, R any](ctx context.Context, c *Conn, method string, params P) (R, error)

Request is a typed convenience without a method whitelist.

Types

type Client

type Client struct {
	Capabilities ServerCapabilities
	ProcessDone  <-chan struct{}

	*Conn

	Encoding PositionEncoding
	Root     URI
	// contains filtered or unexported fields
}

func Connect

func Connect(ctx context.Context, options Options, conn io.ReadWriteCloser) (*Client, error)

Connect runs LSP negotiation over an owned remote stdio stream. Process creation, PATH lookup, and reaping belong to pencode-server.

func (*Client) CloseDocument

func (c *Client) CloseDocument(ctx context.Context, uri URI) error

func (*Client) Configure

func (c *Client) Configure(ctx context.Context, configuration json.RawMessage) error

func (*Client) DidSave

func (c *Client) DidSave(ctx context.Context, uri URI) error

func (*Client) DidSaveText

func (c *Client) DidSaveText[V piecetree.Value](ctx context.Context, uri URI, value V) error

func (*Client) DocumentVersion

func (c *Client) DocumentVersion(uri URI) (int, bool)

func (*Client) ProviderOptions

func (c *Client) ProviderOptions(method, capability string) json.RawMessage

ProviderOptions includes dynamically registered trigger characters and resolve support, using the same provider selection as Supports.

func (*Client) Shutdown

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

Shutdown performs shutdown -> response -> exit, flushes the final notification and reaps the process. A deadline also terminates unresponsive servers.

func (*Client) Supports

func (c *Client) Supports(method, capability string) bool

func (*Client) SyncDocument

func (c *Client) SyncDocument[V piecetree.Value](ctx context.Context, uri URI, language string, value V, version int) error

SyncDocument sends didOpen once, then full or incremental changes according to the negotiated sync kind. Calls are ordered by the session worker.

func (*Client) WillSave

func (c *Client) WillSave(ctx context.Context, uri URI) error

type CodeAction

type CodeAction struct {
	Title    string         `json:"title"`
	Kind     string         `json:"kind,omitempty"`
	Edit     *WorkspaceEdit `json:"edit,omitempty"`
	Command  *Command       `json:"command,omitempty"`
	Disabled *struct {
		Reason string `json:"reason"`
	} `json:"disabled,omitempty"`
	Data json.RawMessage `json:"data,omitempty"`
}

type Command

type Command struct {
	Title     string            `json:"title"`
	Command   string            `json:"command"`
	Arguments []json.RawMessage `json:"arguments,omitempty"`
}

type CompletionItem

type CompletionItem struct {
	TextEdit            *TextEdit       `json:"textEdit,omitempty"`
	Command             *Command        `json:"command,omitempty"`
	Label               string          `json:"label"`
	SortText            string          `json:"sortText,omitempty"`
	FilterText          string          `json:"filterText,omitempty"`
	Detail              string          `json:"detail,omitempty"`
	InsertText          string          `json:"insertText,omitempty"`
	AdditionalTextEdits []TextEdit      `json:"additionalTextEdits,omitempty"`
	Data                json.RawMessage `json:"data,omitempty"`
	Kind                int             `json:"kind,omitempty"`
	InsertTextFormat    int             `json:"insertTextFormat,omitempty"`
}

type Conn

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

func NewConn

func NewConn(input io.Reader, output io.Writer, closeIO func(), handler Handler) *Conn

NewConn owns its streams. closeIO must interrupt both reads and writes. Responses are dispatched independently of handlers, so a server request can make a nested client request without deadlocking the reader.

func (*Conn) BeginCall

func (c *Conn) BeginCall(ctx context.Context, method string, params any) (PendingCall, error)

BeginCall enqueues the request before returning. A session can therefore keep didOpen/didChange/requests ordered while waiting for responses concurrently.

func (*Conn) Call

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

func (*Conn) Close

func (c *Conn) Close()

func (*Conn) Done

func (c *Conn) Done() <-chan struct{}

func (*Conn) Err

func (c *Conn) Err() error

func (*Conn) Flush

func (c *Conn) Flush(ctx context.Context) error

Flush waits until preceding messages have reached the server pipe.

func (*Conn) Notify

func (c *Conn) Notify(ctx context.Context, method string, params any) error

type Diagnostic

type Diagnostic struct {
	Source             string          `json:"source,omitempty"`
	Message            string          `json:"message"`
	Code               json.RawMessage `json:"code,omitempty"`
	Tags               []int           `json:"tags,omitempty"`
	RelatedInformation json.RawMessage `json:"relatedInformation,omitempty"`
	Data               json.RawMessage `json:"data,omitempty"`
	Range              Range           `json:"range"`
	Severity           int             `json:"severity,omitempty"`
}

type Handler

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

type Location

type Location struct {
	URI   URI   `json:"uri"`
	Range Range `json:"range"`
}
type LocationLink struct {
	TargetURI            URI   `json:"targetUri"`
	TargetRange          Range `json:"targetRange"`
	TargetSelectionRange Range `json:"targetSelectionRange"`
}

type Message

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

Message.ID is absent for notifications. Raw JSON preserves string and numeric server IDs, result unions, custom methods and future protocol additions.

type Options

type Options struct {
	// Handler receives server requests and notifications not handled internally.
	// Returning MethodNotFound is required for unsupported requests.
	Handler Handler
	// Capabilities augments the conservative built-in client capabilities.
	Capabilities          map[string]any
	Root                  string
	InitializationOptions json.RawMessage
	Configuration         json.RawMessage
}

type PendingCall

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

PendingCall owns one response slot. Exactly one Wait must follow BeginCall, including when its context has been cancelled, to release the registration.

func (PendingCall) Wait

func (call PendingCall) Wait(ctx context.Context, result any) error

type Position

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

func PositionAt

func PositionAt[V piecetree.Value](value V, offset int, encoding PositionEncoding) (Position, error)

type PositionEncoding

type PositionEncoding string

Offsets and positions are checked against rune boundaries. CRLF is one line ending; characters count UTF-16 code units by default, never UTF-8 bytes.

const (
	UTF8  PositionEncoding = "utf-8"
	UTF16 PositionEncoding = "utf-16"
	UTF32 PositionEncoding = "utf-32"
)

type PublishDiagnosticsParams

type PublishDiagnosticsParams struct {
	URI         URI          `json:"uri"`
	Version     *int         `json:"version,omitempty"`
	Diagnostics []Diagnostic `json:"diagnostics"`
}

type Range

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

func IncrementalChange

func IncrementalChange(old, next string, encoding PositionEncoding) (Range, string)

IncrementalChange computes one minimal replacement, without allocating or splitting UTF-8 runes. It also handles changes to line endings.

type Reader

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

func NewReader

func NewReader(source io.Reader) *Reader

func (*Reader) Read

func (r *Reader) Read() ([]byte, error)

Read borrows its result until the next Read; framing storage is reused.

type Registration

type Registration struct {
	ID              string          `json:"id"`
	Method          string          `json:"method"`
	RegisterOptions json.RawMessage `json:"registerOptions,omitempty"`
}

type ResponseError

type ResponseError struct {
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
	Code    int             `json:"code"`
}

func (*ResponseError) Error

func (e *ResponseError) Error() string

type SemanticToken

type SemanticToken struct {
	Start, End      int
	Type, Modifiers uint32
}

func DecodeSemanticTokens

func DecodeSemanticTokens[V piecetree.Value](dst []SemanticToken, data []uint32, value V, encoding PositionEncoding, types int) ([]SemanticToken, error)

DecodeSemanticTokens resolves relative positions in one forward document scan. The client does not advertise overlapping or multiline token support.

type SemanticTokens

type SemanticTokens struct {
	ResultID string   `json:"resultId,omitempty"`
	Data     []uint32 `json:"data"`
}

type SemanticTokensDelta

type SemanticTokensDelta struct {
	ResultID string               `json:"resultId,omitempty"`
	Edits    []SemanticTokensEdit `json:"edits"`
}

type SemanticTokensEdit

type SemanticTokensEdit struct {
	Data        []uint32 `json:"data,omitempty"`
	Start       int      `json:"start"`
	DeleteCount int      `json:"deleteCount"`
}

type ServerCapabilities

type ServerCapabilities map[string]json.RawMessage

func (ServerCapabilities) Has

func (c ServerCapabilities) Has(key string) bool

type TextDocumentEdit

type TextDocumentEdit struct {
	TextDocument VersionedTextDocumentIdentifier `json:"textDocument"`
	Edits        []TextEdit                      `json:"edits"`
}

type TextDocumentIdentifier

type TextDocumentIdentifier struct {
	URI URI `json:"uri"`
}

type TextDocumentPositionParams

type TextDocumentPositionParams struct {
	TextDocument TextDocumentIdentifier `json:"textDocument"`
	Position     Position               `json:"position"`
}

type TextEdit

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

type URI

type URI string

func FileURI

func FileURI(path string) URI

func (URI) Path

func (u URI) Path() (string, error)

type VersionedTextDocumentIdentifier

type VersionedTextDocumentIdentifier struct {
	Version *int `json:"version"`
	URI     URI  `json:"uri"`
}

type WorkspaceEdit

type WorkspaceEdit struct {
	Changes           map[URI][]TextEdit `json:"changes,omitempty"`
	DocumentChanges   []json.RawMessage  `json:"documentChanges,omitempty"`
	ChangeAnnotations json.RawMessage    `json:"changeAnnotations,omitempty"`
}

type WorkspaceFolder

type WorkspaceFolder struct {
	URI  URI    `json:"uri"`
	Name string `json:"name"`
}

type Writer

type Writer struct {
	Target io.Writer
	// contains filtered or unexported fields
}

func (*Writer) Write

func (w *Writer) Write(data []byte) error

Jump to

Keyboard shortcuts

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