- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"mygoland/internal/lsp"
|
|
)
|
|
|
|
// CodeActions 请求光标所在行的代码动作并列进命令面板。
|
|
func (a *App) CodeActions() {
|
|
d := a.doc()
|
|
c := a.lsp
|
|
if d == nil || c == nil || !lsp.IsGoFile(d.path) {
|
|
a.alert("代码动作", "先把光标放到 Go 文件里")
|
|
return
|
|
}
|
|
pos := d.st.CursorPos()
|
|
line := d.buf.Line(pos.Line)
|
|
col := pos.Col
|
|
if col > len(line) {
|
|
col = len(line)
|
|
}
|
|
rng := lsp.Range{
|
|
Start: lsp.Position{Line: pos.Line, Character: 0},
|
|
End: lsp.Position{Line: pos.Line, Character: lsp.UTF16Len(line)},
|
|
}
|
|
|
|
// 把这一行的诊断**原样**带上:gopls 的「添加 import」「删除未使用变量」
|
|
// 这类快速修复靠诊断里的 data 字段匹配,重新拼一份就没用了。
|
|
var diags []lsp.Diagnostic
|
|
for _, dd := range d.diags {
|
|
if dd.Range.Start.Line <= pos.Line && pos.Line <= dd.Range.End.Line {
|
|
diags = append(diags, dd)
|
|
}
|
|
}
|
|
|
|
uri := d.uri
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
defer cancel()
|
|
acts, err := c.CodeActions(ctx, uri, rng, diags)
|
|
if err != nil {
|
|
a.win.Update(func() { a.alert("代码动作失败", err.Error()) })
|
|
return
|
|
}
|
|
if len(acts) == 0 {
|
|
a.win.Update(func() { a.alert("代码动作", "这里没有可用的动作") })
|
|
return
|
|
}
|
|
items := make([]paletteItem, 0, len(acts))
|
|
for i, act := range acts {
|
|
items = append(items, paletteItem{
|
|
Kind: itemFile,
|
|
Label: act.Title,
|
|
Hint: actionKindLabel(act.Kind),
|
|
Idx: i,
|
|
})
|
|
}
|
|
a.win.Update(func() {
|
|
a.actions = acts
|
|
a.pal = paletteState{open: true, action: true, refsAll: items, items: items}
|
|
a.win.Update(func() {})
|
|
})
|
|
}()
|
|
}
|
|
|
|
// actionKindLabel 把 LSP 的 kind 翻成给人看的分类。
|
|
func actionKindLabel(kind string) string {
|
|
switch {
|
|
case strings.HasPrefix(kind, "quickfix"):
|
|
return "快速修复"
|
|
case strings.HasPrefix(kind, "refactor.extract"):
|
|
return "提取"
|
|
case strings.HasPrefix(kind, "refactor.inline"):
|
|
return "内联"
|
|
case strings.HasPrefix(kind, "refactor.rewrite"):
|
|
return "重写"
|
|
case strings.HasPrefix(kind, "refactor"):
|
|
return "重构"
|
|
case strings.HasPrefix(kind, "source"):
|
|
return "源码"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// applyAction 执行选中的代码动作。
|
|
func (a *App) applyAction(i int) {
|
|
if i < 0 || i >= len(a.actions) {
|
|
return
|
|
}
|
|
act := a.actions[i]
|
|
// 自带改动的,直接落到缓冲区。
|
|
if act.Edit != nil {
|
|
changes := act.Edit.ByFile()
|
|
if len(changes) > 0 {
|
|
a.applyWorkspaceEdits(changes)
|
|
return
|
|
}
|
|
}
|
|
// 只有命令的,交给服务端执行,完成后重新同步一遍。
|
|
if act.Command != nil {
|
|
c := a.lsp
|
|
if c == nil {
|
|
return
|
|
}
|
|
cmd := *act.Command
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
defer cancel()
|
|
if err := c.ExecuteCommand(ctx, cmd); err != nil {
|
|
a.win.Update(func() { a.alert("执行命令失败", err.Error()) })
|
|
return
|
|
}
|
|
// gopls 的命令(如整理 import)会通过 workspace/applyEdit 回推改动,
|
|
// 这里稍等一拍让改动落地。
|
|
time.Sleep(200 * time.Millisecond)
|
|
a.win.Update(func() { a.win.Update(func() {}) })
|
|
}()
|
|
}
|
|
}
|