package app import ( "sort" "strings" "mygoland/internal/lsp" ) // actionRequest 是一次 textDocument/codeAction 请求:范围、附带诊断, // 以及可选的 kind 过滤(only)。 type actionRequest struct { rng lsp.Range diags []lsp.Diagnostic only []string } // codeActionRequests 产出发给 gopls 的请求。 // // 为什么要两次(依据:2026-11-05 用真实 gopls v0.23 探测过,见 // internal/lsp 的 TestGoplsCodeActionRanges 记录): // // 1. 光标或选区那一次。有选区必须传选区——`refactor.extract.variable` // 和 `refactor.inline.call` 只在范围正好圈住表达式时才返回;从前一律 // 传整行,等于「选中了 foo() 却按整行提取」,选中态形同虚设。 // 2. 整文件那一次,只问 `refactor.rewrite`。`Add struct tags` 这类文件级 // 重构只有范围覆盖到它才出现,而它不在光标所在行。 // // 两次请求都是本地往返,串行执行;调用方本来就在后台协程里,不挡 UI。 func codeActionRequests(d *Doc) []actionRequest { here := cursorOrSelectionRange(d) return []actionRequest{ {rng: here, diags: diagsIn(d.diags, here)}, {rng: wholeFileRange(d), only: []string{"refactor.rewrite"}}, } } // cursorOrSelectionRange 有选区用选区,没有就用光标所在的整行。 // // 没有选区时沿用整行(既有行为),理由不是「零宽位置会丢快速修复」—— // 2026-11-05 用 gopls v0.23 实测过,行首零宽位置照样能拿到 quickfix, // gopls 会把位置摊到所属节点上。真正的理由是整行能给按节点展开的动作一个 // 范围(`refactor.extract.function` 就是这么来的),而且改小它没有任何好处。 func cursorOrSelectionRange(d *Doc) lsp.Range { if sel := d.st.Selection(); !sel.Empty() { return lsp.Range{ Start: positionAt(d, sel.Start()), End: positionAt(d, sel.End()), } } pos := d.st.CursorPos() line := d.buf.Line(pos.Line) return lsp.Range{ Start: lsp.Position{Line: pos.Line}, End: lsp.Position{Line: pos.Line, Character: lsp.UTF16Len(line)}, } } // wholeFileRange 覆盖整个缓冲区。 func wholeFileRange(d *Doc) lsp.Range { n := d.buf.LineCount() if n <= 0 { return lsp.Range{} } last := n - 1 return lsp.Range{ Start: lsp.Position{Line: 0}, End: lsp.Position{Line: last, Character: lsp.UTF16Len(d.buf.Line(last))}, } } // diagsIn 取与请求范围有交叠的诊断。 // // **必须带**:gopls 的快速修复靠 context.diagnostics 匹配,不带就一条都没有 // (实测见 internal/lsp 的 TestGoplsCodeActionMissingImportQuickfix)。 // // 只按行比、不细到列:gopls 会自己再按列筛一遍,卡得太死反而可能漏;带不相干 // **行**的诊断才会真的污染结果,所以按行过滤正好。 func diagsIn(diags []lsp.Diagnostic, r lsp.Range) []lsp.Diagnostic { var out []lsp.Diagnostic for _, d := range diags { if d.Range.Start.Line <= r.End.Line && r.Start.Line <= d.Range.End.Line { out = append(out, d) } } return out } // noiseCommands 是 gopls 那些「只打开浏览器或面板、不改一个字节」的命令。 // // 实测一次 Alt+Enter 能返回 6–9 项,其中 5 项是这类东西——打开 pkg.go.dev、 // 看汇编、看编译器优化细节、列未归类符号、直接开个 URL。它们把真正的 // 「Extract variable」挤到第七位。按**命令名**过滤而不是标题:标题是英文且 // 会随 gopls 版本变,命令名是协议标识,改得慢。 // // 白名单式的反向判断(只留已知会改代码的)更干净,但 gopls 加新能力时会把 // 新动作一起漏掉;所以这里只列已知噪音,其余一律放行。 var noiseCommands = map[string]bool{ "gopls.doc": true, // Browse documentation "gopls.assembly": true, // Browse assembly "gopls.free_symbols": true, // Browse free symbols "gopls.client_open_url": true, // 明文开浏览器的命令 "gopls.gc_details": true, // 编译器优化细节开关 } // actionNoise 判断这个动作是不是该从面板里拿掉。 func actionNoise(a lsp.CodeAction) bool { return a.Command != nil && noiseCommands[a.Command.Command] } // mergeActions 合并多次请求的结果:过滤噪音,同标题只留第一次出现的。 // // 「第一次出现的优先」是刻意的:第一个请求是光标/选区算出来的,它的动作 // 与用户眼前的范围一致;第二个是整文件算的,同名项(如 Extract function) // 的目标范围可能更大,顶掉前者会让用户选中一段却抽出整行。 func mergeActions(parts ...[]lsp.CodeAction) []lsp.CodeAction { seen := make(map[string]bool) var out []lsp.CodeAction for _, part := range parts { for _, a := range part { if actionNoise(a) || seen[a.Title] { continue } seen[a.Title] = true out = append(out, a) } } return out } // actionRank 给动作排优先级,数字小的靠前。 // // gopls 的返回顺序不带意图:实测「Extract variable」排在「Add test」和一堆 // 浏览命令之后。按 kind 重排一次,让「马上能改对代码」的项落在拇指下面。 func actionRank(a lsp.CodeAction) int { switch { case a.IsPreferred: return 0 case strings.HasPrefix(a.Kind, "quickfix"): return 1 case strings.HasPrefix(a.Kind, "refactor"): return 2 case strings.HasPrefix(a.Kind, "source"): return 3 } return 4 } // rankActions 按 actionRank 稳定排序:同档内保持 gopls 给的顺序。 func rankActions(acts []lsp.CodeAction) []lsp.CodeAction { out := make([]lsp.CodeAction, len(acts)) copy(out, acts) sort.SliceStable(out, func(i, j int) bool { return actionRank(out[i]) < actionRank(out[j]) }) return out }