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]) if len(s) > 140 { s = s[:140] + "…" } return s } // 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) }