- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
377 lines
11 KiB
Go
377 lines
11 KiB
Go
// Package lsp 实现一个最小可用的 LSP 客户端,通过 stdio 与 gopls 通信。
|
|
//
|
|
// 只实现 MyGoland 需要的部分:初始化、文档同步、诊断、补全、悬停、跳转定义、
|
|
// 引用、重命名、代码动作、格式化、内联提示。
|
|
package lsp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
)
|
|
|
|
// Position 是 LSP 的位置:行号与行内偏移都是 0 起,Character 以 UTF-16 码元计。
|
|
type Position struct {
|
|
Line int `json:"line"`
|
|
Character int `json:"character"`
|
|
}
|
|
|
|
// Range 是文本区间。
|
|
type Range struct {
|
|
Start Position `json:"start"`
|
|
End Position `json:"end"`
|
|
}
|
|
|
|
// Location 是文件中的一个区间。
|
|
type Location struct {
|
|
URI string `json:"uri"`
|
|
Range Range `json:"range"`
|
|
}
|
|
|
|
// TextEdit 是一次文本替换。
|
|
type TextEdit struct {
|
|
Range Range `json:"range"`
|
|
NewText string `json:"newText"`
|
|
}
|
|
|
|
// TextDocumentItem 是 didOpen 传入的文档。
|
|
type TextDocumentItem struct {
|
|
URI string `json:"uri"`
|
|
LanguageID string `json:"languageId"`
|
|
Version int `json:"version"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
// VersionedTextDocumentIdentifier 标识某个版本的文档。
|
|
type VersionedTextDocumentIdentifier struct {
|
|
URI string `json:"uri"`
|
|
Version int `json:"version"`
|
|
}
|
|
|
|
// TextDocumentIdentifier 只标识文档。
|
|
type TextDocumentIdentifier struct {
|
|
URI string `json:"uri"`
|
|
}
|
|
|
|
// ContentChange 是 didChange 的一项增量修改。
|
|
type ContentChange struct {
|
|
Range *Range `json:"range,omitempty"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
// MarkupContent 是 LSP 的富文本,也接受纯字符串的写法。
|
|
type MarkupContent struct {
|
|
Kind string `json:"kind"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
// UnmarshalJSON 让 MarkupContent 兼容 "text" 与 {kind,value} 两种形式。
|
|
func (m *MarkupContent) UnmarshalJSON(b []byte) error {
|
|
if len(b) > 0 && b[0] == '"' {
|
|
var s string
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
return err
|
|
}
|
|
m.Kind, m.Value = "plaintext", s
|
|
return nil
|
|
}
|
|
type alias MarkupContent
|
|
var a alias
|
|
if err := json.Unmarshal(b, &a); err != nil {
|
|
return err
|
|
}
|
|
*m = MarkupContent(a)
|
|
return nil
|
|
}
|
|
|
|
// CompletionItem 是一条补全建议。
|
|
type CompletionItem struct {
|
|
Label string `json:"label"`
|
|
Kind int `json:"kind"`
|
|
Detail string `json:"detail"`
|
|
Documentation json.RawMessage `json:"documentation"`
|
|
InsertText string `json:"insertText"`
|
|
TextEdit *TextEdit `json:"textEdit"`
|
|
FilterText string `json:"filterText"`
|
|
SortText string `json:"sortText"`
|
|
}
|
|
|
|
// CompletionList 是补全结果的一种包装。
|
|
type CompletionList struct {
|
|
Items []CompletionItem `json:"items"`
|
|
IsIncomplete bool `json:"isIncomplete"`
|
|
}
|
|
|
|
// DocumentationText 把 documentation 字段统一成纯文本。
|
|
func (c CompletionItem) DocumentationText() string {
|
|
if len(c.Documentation) == 0 {
|
|
return ""
|
|
}
|
|
var m MarkupContent
|
|
if err := json.Unmarshal(c.Documentation, &m); err == nil {
|
|
return m.Value
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Hover 是悬停信息。
|
|
type Hover struct {
|
|
Contents json.RawMessage `json:"contents"`
|
|
Range *Range `json:"range,omitempty"`
|
|
}
|
|
|
|
// Text 把悬停内容统一成纯文本。
|
|
func (h Hover) Text() string {
|
|
if len(h.Contents) == 0 {
|
|
return ""
|
|
}
|
|
var m MarkupContent
|
|
if err := json.Unmarshal(h.Contents, &m); err == nil {
|
|
return m.Value
|
|
}
|
|
var list []MarkupContent
|
|
if err := json.Unmarshal(h.Contents, &list); err == nil {
|
|
out := ""
|
|
for _, x := range list {
|
|
out += x.Value + "\n\n"
|
|
}
|
|
return out
|
|
}
|
|
var arr []string
|
|
if err := json.Unmarshal(h.Contents, &arr); err == nil {
|
|
out := ""
|
|
for _, x := range arr {
|
|
out += x + "\n\n"
|
|
}
|
|
return out
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Diagnostic 是一条诊断。
|
|
type Diagnostic struct {
|
|
Range Range `json:"range"`
|
|
Severity int `json:"severity"`
|
|
Source string `json:"source"`
|
|
Message string `json:"message"`
|
|
// Data 是服务端附带的载荷,请求代码动作时必须原样带回去,
|
|
// 否则 gopls 认不出是哪条诊断,也就不会给出对应的快速修复。
|
|
Data json.RawMessage `json:"data,omitempty"`
|
|
}
|
|
|
|
// PublishDiagnosticsParams 是诊断通知的参数。
|
|
type PublishDiagnosticsParams struct {
|
|
URI string `json:"uri"`
|
|
Version int `json:"version"`
|
|
Diagnostics []Diagnostic `json:"diagnostics"`
|
|
}
|
|
|
|
// WorkspaceEdit 是一组跨文件的修改,两种形态择一出现。
|
|
type WorkspaceEdit struct {
|
|
Changes map[string][]TextEdit `json:"changes"`
|
|
// DocumentChanges 是带版本的形态,gopls 有时用它。
|
|
DocumentChanges []struct {
|
|
TextDocument struct {
|
|
URI string `json:"uri"`
|
|
} `json:"textDocument"`
|
|
Edits []TextEdit `json:"edits"`
|
|
} `json:"documentChanges"`
|
|
}
|
|
|
|
// Command 是服务端提供的一条可执行命令。
|
|
type Command struct {
|
|
Title string `json:"title"`
|
|
Command string `json:"command"`
|
|
Arguments []any `json:"arguments,omitempty"`
|
|
}
|
|
|
|
// CodeAction 是一条代码动作,改动可能直接给 edit,也可能给一条命令。
|
|
type CodeAction struct {
|
|
Title string `json:"title"`
|
|
Kind string `json:"kind"`
|
|
Edit *WorkspaceEdit `json:"edit,omitempty"`
|
|
Command *Command `json:"command,omitempty"`
|
|
Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
|
|
IsPreferred bool `json:"isPreferred,omitempty"`
|
|
}
|
|
|
|
// ByFile 把两种形态合并成 uri → 修改列表。
|
|
func (w WorkspaceEdit) ByFile() map[string][]TextEdit {
|
|
out := map[string][]TextEdit{}
|
|
for uri, edits := range w.Changes {
|
|
out[uri] = append(out[uri], edits...)
|
|
}
|
|
for _, dc := range w.DocumentChanges {
|
|
if dc.TextDocument.URI == "" {
|
|
continue
|
|
}
|
|
out[dc.TextDocument.URI] = append(out[dc.TextDocument.URI], dc.Edits...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// DocumentSymbol 是一个符号,可能带子符号。
|
|
type DocumentSymbol struct {
|
|
Name string `json:"name"`
|
|
Detail string `json:"detail"`
|
|
Kind int `json:"kind"`
|
|
Range Range `json:"range"`
|
|
SelectionRange Range `json:"selectionRange"`
|
|
Children []DocumentSymbol `json:"children"`
|
|
}
|
|
|
|
// FormattingOptions 是格式化请求的参数。
|
|
type FormattingOptions struct {
|
|
TabSize int `json:"tabSize"`
|
|
InsertSpaces bool `json:"insertSpaces"`
|
|
}
|
|
|
|
// WorkspaceFolder 是工作区目录。
|
|
type WorkspaceFolder struct {
|
|
URI string `json:"uri"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// InitializeParams 是初始化请求的参数,只填 mygo 用得到的字段。
|
|
type InitializeParams struct {
|
|
ProcessID int `json:"processId"`
|
|
ClientInfo *ClientInfo `json:"clientInfo,omitempty"`
|
|
RootURI string `json:"rootUri"`
|
|
RootPath string `json:"rootPath,omitempty"`
|
|
WorkspaceFolders []WorkspaceFolder `json:"workspaceFolders,omitempty"`
|
|
Capabilities ClientCapabilities `json:"capabilities"`
|
|
InitializationOptions map[string]any `json:"initializationOptions,omitempty"`
|
|
}
|
|
|
|
// ClientInfo 描述客户端。
|
|
type ClientInfo struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
// ClientCapabilities 是客户端能力声明。
|
|
type ClientCapabilities struct {
|
|
Workspace WorkspaceCapabilities `json:"workspace"`
|
|
TextDocument TextDocumentCapabilities `json:"textDocument"`
|
|
Window map[string]any `json:"window,omitempty"`
|
|
}
|
|
|
|
// WorkspaceCapabilities 声明工作区能力。
|
|
type WorkspaceCapabilities struct {
|
|
WorkspaceFolders bool `json:"workspaceFolders"`
|
|
Configuration bool `json:"configuration"`
|
|
}
|
|
|
|
// TextDocumentCapabilities 声明文档能力。
|
|
type TextDocumentCapabilities struct {
|
|
Synchronization SynchronizationCapabilities `json:"synchronization"`
|
|
Completion map[string]any `json:"completion,omitempty"`
|
|
Hover map[string]any `json:"hover,omitempty"`
|
|
Definition map[string]any `json:"definition,omitempty"`
|
|
PublishDiagnostics map[string]any `json:"publishDiagnostics,omitempty"`
|
|
Formatting map[string]any `json:"formatting,omitempty"`
|
|
DocumentSymbol map[string]any `json:"documentSymbol,omitempty"`
|
|
// InlayHint 不能省:gopls 只有在客户端声明了这个能力时才回内联提示,
|
|
// 没声明时 textDocument/inlayHint 只会返回空数组。
|
|
InlayHint map[string]any `json:"inlayHint,omitempty"`
|
|
}
|
|
|
|
// SynchronizationCapabilities 声明同步能力。
|
|
type SynchronizationCapabilities struct {
|
|
DynamicRegistration bool `json:"dynamicRegistration"`
|
|
DidSave bool `json:"didSave"`
|
|
WillSave bool `json:"willSave"`
|
|
}
|
|
|
|
// InitializeResult 是初始化结果。
|
|
type InitializeResult struct {
|
|
Capabilities json.RawMessage `json:"capabilities"`
|
|
ServerInfo *ServerInfo `json:"serverInfo,omitempty"`
|
|
}
|
|
|
|
// ServerInfo 描述服务端。
|
|
type ServerInfo struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
// defaultCapabilities 返回 MyGoland 声明的能力集合。
|
|
func defaultCapabilities() ClientCapabilities {
|
|
return ClientCapabilities{
|
|
Workspace: WorkspaceCapabilities{WorkspaceFolders: true, Configuration: true},
|
|
TextDocument: TextDocumentCapabilities{
|
|
Synchronization: SynchronizationCapabilities{DidSave: true},
|
|
Completion: map[string]any{
|
|
"completionItem": map[string]any{
|
|
"snippetSupport": false,
|
|
"documentationFormat": []string{"markdown", "plaintext"},
|
|
},
|
|
"contextSupport": true,
|
|
},
|
|
Hover: map[string]any{"contentFormat": []string{"markdown", "plaintext"}},
|
|
Definition: map[string]any{"linkSupport": false},
|
|
PublishDiagnostics: map[string]any{"relatedInformation": false},
|
|
Formatting: map[string]any{},
|
|
DocumentSymbol: map[string]any{"hierarchicalDocumentSymbolSupport": true},
|
|
InlayHint: map[string]any{
|
|
"dynamicRegistration": false,
|
|
"resolveSupport": map[string]any{
|
|
"properties": []string{
|
|
"tooltip", "textEdits", "label.tooltip", "label.location", "label.command",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// InlayHint 是一条内联提示(参数名、类型等)。
|
|
type InlayHint struct {
|
|
Position Position `json:"position"`
|
|
Label InlayLabel `json:"label"`
|
|
Kind int `json:"kind,omitempty"`
|
|
PaddingLeft bool `json:"paddingLeft,omitempty"`
|
|
PaddingRight bool `json:"paddingRight,omitempty"`
|
|
}
|
|
|
|
// InlayLabel 是提示文字:协议允许一个字符串,或者若干带位置的部分。
|
|
type InlayLabel struct {
|
|
Parts []string
|
|
}
|
|
|
|
func (l *InlayLabel) UnmarshalJSON(data []byte) error {
|
|
var s string
|
|
if err := json.Unmarshal(data, &s); err == nil {
|
|
l.Parts = []string{s}
|
|
return nil
|
|
}
|
|
var parts []struct {
|
|
Value string `json:"value"`
|
|
}
|
|
if err := json.Unmarshal(data, &parts); err != nil {
|
|
return err
|
|
}
|
|
l.Parts = l.Parts[:0]
|
|
for _, p := range parts {
|
|
l.Parts = append(l.Parts, p.Value)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Text 把各段拼成一行文字。
|
|
func (l InlayLabel) Text() string {
|
|
return strings.Join(l.Parts, "")
|
|
}
|
|
|
|
// SymbolInformation 是 workspace/symbol 返回的一条符号。
|
|
type SymbolInformation struct {
|
|
Name string `json:"name"`
|
|
Kind int `json:"kind"`
|
|
ContainerName string `json:"containerName,omitempty"`
|
|
Location struct {
|
|
URI string `json:"uri"`
|
|
Range Range `json:"range"`
|
|
} `json:"location"`
|
|
// 新版协议允许 WorkspaceSymbol 用 location.uri,也可能给 targetUri。
|
|
TargetURI string `json:"targetUri,omitempty"`
|
|
}
|