1. 标签页右键菜单接回(回归修复) tabmenu.go 的 160 行实现全库零调用点,tabBar 只处理 Clicked。 补 RightClicked → openPop(tabEntries(path)),右键时先设 active (菜单无标题,靠标签高亮传达「关闭左边」相对谁算)。 新增 tabmenu_test.go 8 个单测。 2. 中文按字节截断导致乱码 palette.go trimmed[:160]、rename.go s[:140] 会把多字节字符切断。 trimTo 从 gitlog.go 提到 strutil.go(三个调用方共用),两处改用它; n<=0 返回空串。strutil_test.go 6 个单测(含真实 linePreview 路径)。 3. 覆盖率运行不再吞测试输出 原来 lines 收集后 _ = lines 丢弃,测试失败只看到覆盖率数字。 改用 ExitCode() 判失败,只回放 FAIL/--- FAIL/panic/# 包/file.go:12 行 (上限 200),覆盖率页顶部给红底摘要 + 「查看测试输出」按钮。 coverageFailLines/Summary 纯函数 + 5 个单测。 4. 索引与搜索感知 .gitignore 新增 ignore.go:dir/、*.ext、/rootonly、带路径、**、! 取反、注释。 内置跳过扩到 vendor/dist/build/out/target/.next(文件树仍用保守 skipDir)。 indexState 加 truncated,快速打开底部提示「索引已截断至 N 个文件」。 ignore_test.go 10 个单测 + 真实遍历集成测试。 验证:go vet ./... 干净;go test ./internal/... 7 包全绿; e2e 实例 tools/tree 确认菜单 5 项齐全 + tools/shot 截图核对。
205 lines
4.6 KiB
Go
205 lines
4.6 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"mygoland/internal/lsp"
|
|
)
|
|
|
|
// renameState 是重命名弹窗的状态。
|
|
type renameState struct {
|
|
open bool
|
|
openDlg bool
|
|
from string // 原名,用于提示
|
|
to string // 输入框里的新名
|
|
uri string
|
|
pos lsp.Position
|
|
path string
|
|
}
|
|
|
|
// FindReferences 把光标处符号的所有引用列进命令面板。
|
|
func (a *App) FindReferences() {
|
|
d := a.doc()
|
|
c := a.lsp
|
|
if d == nil || c == nil || !lsp.IsGoFile(d.path) {
|
|
a.alert("查找引用", "先把光标放到 Go 文件的符号上")
|
|
return
|
|
}
|
|
pos := positionAt(d, d.st.Cursor())
|
|
uri := d.uri
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
defer cancel()
|
|
locs, err := c.References(ctx, uri, pos, true)
|
|
if err != nil {
|
|
a.win.Update(func() { a.alert("查找引用失败", err.Error()) })
|
|
return
|
|
}
|
|
items := make([]paletteItem, 0, len(locs))
|
|
for _, loc := range locs {
|
|
p := lsp.URIToPath(loc.URI)
|
|
if p == "" {
|
|
continue
|
|
}
|
|
line := loc.Range.Start.Line + 1
|
|
label := p
|
|
if a.root != "" {
|
|
if rel, err := filepath.Rel(a.root, p); err == nil {
|
|
label = filepath.ToSlash(rel)
|
|
}
|
|
}
|
|
items = append(items, paletteItem{
|
|
Kind: itemLine,
|
|
Path: p,
|
|
Line: line,
|
|
Label: label,
|
|
Text: linePreview(p, line),
|
|
})
|
|
}
|
|
a.win.Update(func() {
|
|
a.pal = paletteState{open: true, refs: true, refsAll: items, items: items}
|
|
if len(items) == 0 {
|
|
a.pal.items = nil
|
|
}
|
|
a.win.Update(func() {})
|
|
})
|
|
}()
|
|
}
|
|
|
|
// linePreview 取文件某一行的内容用做列表预览。
|
|
func linePreview(path string, line int) string {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
lines := strings.Split(string(data), "\n")
|
|
if line < 1 || line > len(lines) {
|
|
return ""
|
|
}
|
|
s := strings.TrimSpace(lines[line-1])
|
|
return trimTo(s, 140)
|
|
}
|
|
|
|
// RenameSymbol 打开重命名弹窗,光标处的词作为默认值。
|
|
func (a *App) RenameSymbol() {
|
|
d := a.doc()
|
|
c := a.lsp
|
|
if d == nil || c == nil || !lsp.IsGoFile(d.path) {
|
|
a.alert("重命名", "先把光标放到 Go 文件的符号上")
|
|
return
|
|
}
|
|
off := d.st.Cursor()
|
|
start, end := wordBounds(d, off)
|
|
word := ""
|
|
if end > start {
|
|
word = d.buf.String()[start:end]
|
|
}
|
|
if word == "" {
|
|
a.alert("重命名", "光标处没有可重命名的符号")
|
|
return
|
|
}
|
|
a.ren = renameState{
|
|
open: true,
|
|
from: word,
|
|
to: word,
|
|
uri: d.uri,
|
|
pos: positionAt(d, off),
|
|
path: d.path,
|
|
}
|
|
a.win.Update(func() {})
|
|
}
|
|
|
|
// renameDialog 画重命名弹窗。
|
|
func (a *App) renameDialog(c *ui.Context) {
|
|
if !a.ren.open {
|
|
return
|
|
}
|
|
t := c.Theme()
|
|
|
|
ui.Modal(c, &a.ren.open, func() {
|
|
ui.Column(c).Width(420).Gap(10).Children(func() {
|
|
ui.Text(c, "重命名符号").FontSize(13).Bold().TextColor(t.Text)
|
|
ui.Text(c, "当前:"+a.ren.from).FontSize(10).TextColor(t.TextMuted).SingleLine()
|
|
|
|
inp := ui.TextInput(c, &a.ren.to).FillWidth().FontSize(13).AutoFocus()
|
|
if inp.Submitted() {
|
|
a.confirmRename()
|
|
}
|
|
|
|
ui.Row(c).Gap(8).Justify(ui.End).Children(func() {
|
|
if ui.Button(c, "取消").Clicked() {
|
|
a.ren.open = false
|
|
}
|
|
if ui.Button(c, "确定").Clicked() {
|
|
a.confirmRename()
|
|
}
|
|
})
|
|
})
|
|
})
|
|
|
|
if !a.ren.open && a.ren.openDlg {
|
|
// 弹窗关掉后把焦点还给编辑器。
|
|
a.ren.openDlg = false
|
|
a.focusEditorSoon()
|
|
}
|
|
if !a.ren.openDlg && a.ren.open {
|
|
a.ren.openDlg = true
|
|
a.win.Update(func() {})
|
|
}
|
|
}
|
|
|
|
// confirmRename 发请求并把结果应用到各个文件。
|
|
func (a *App) confirmRename() {
|
|
name := strings.TrimSpace(a.ren.to)
|
|
if name == "" || name == a.ren.from {
|
|
a.ren.open = false
|
|
return
|
|
}
|
|
c := a.lsp
|
|
uri, pos := a.ren.uri, a.ren.pos
|
|
a.ren.open = false
|
|
a.win.Update(func() {})
|
|
if c == nil {
|
|
return
|
|
}
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
defer cancel()
|
|
we, err := c.Rename(ctx, uri, pos, name)
|
|
if err != nil {
|
|
a.win.Update(func() { a.alert("重命名失败", err.Error()) })
|
|
return
|
|
}
|
|
changes := we.ByFile()
|
|
a.win.Update(func() { a.applyWorkspaceEdits(changes) })
|
|
}()
|
|
}
|
|
|
|
// applyWorkspaceEdits 把跨文件修改落到各个缓冲区上(不自动保存,留给用户检查)。
|
|
func (a *App) applyWorkspaceEdits(changes map[string][]lsp.TextEdit) {
|
|
n := 0
|
|
for uri, edits := range changes {
|
|
if len(edits) == 0 {
|
|
continue
|
|
}
|
|
path := lsp.URIToPath(uri)
|
|
if path == "" {
|
|
continue
|
|
}
|
|
d, err := a.loadDoc(path)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
a.applyEdits(d, edits)
|
|
n++
|
|
}
|
|
a.syncTitle()
|
|
a.saveSession()
|
|
logf("rename: %d 个文件已更新", n)
|
|
}
|