Files
MyGolandRelease/internal/app/outline.go
T
zerorigin a80c0f886a MyGoland 首次提交:Go 轻量 IDE
- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲)
- 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表)
- 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记
- Git:状态着色、选择性暂存提交、pull/push、历史并排对比
- 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复
- 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈
- 设计文档与验证记录见 docs/plans/
2026-11-04 16:37:05 +08:00

202 lines
5.0 KiB
Go

package app
import (
"context"
"time"
"github.com/egoist/mygo/ui"
"github.com/egoist/mygo/ui/editor"
"mygoland/internal/lsp"
)
// outlineItem 是大纲里的一个符号,已经按层级铺平。
type outlineItem struct {
Name string
Kind int
Line int // 0 起
Col int
Depth int
}
// outlineDebounce 是重新取符号的最小间隔,避免每敲一个字就请求一次。
const outlineDebounce = 400 * time.Millisecond
// syncOutline 在需要时向语言服务器要当前文件的符号树。
func (a *App) syncOutline(d *Doc) {
if a.panelKind != 1 || d == nil || a.lsp == nil || !lsp.IsGoFile(d.path) {
return
}
ver := d.buf.Version()
if d.path == a.outlinePath && ver == a.outlineVer {
return
}
if time.Since(a.outlineAt) < outlineDebounce {
return
}
a.outlineAt = time.Now()
a.outlinePath = d.path
a.outlineVer = ver
uri := d.uri
c := a.lsp
go func() {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
syms, err := c.DocumentSymbol(ctx, uri)
if err != nil {
return
}
items := flattenSymbols(syms, 0, nil)
logf("outline: %d symbols from %s", len(items), uri)
a.win.Update(func() {
if a.doc() != d {
return
}
a.outline = items
})
}()
}
// flattenSymbols 把符号树铺成带缩进的列表。
func flattenSymbols(syms []lsp.DocumentSymbol, depth int, out []outlineItem) []outlineItem {
for _, s := range syms {
out = append(out, outlineItem{
Name: s.Name,
Kind: s.Kind,
Line: s.SelectionRange.Start.Line,
Col: s.SelectionRange.Start.Character,
Depth: depth,
})
if len(s.Children) > 0 {
out = flattenSymbols(s.Children, depth+1, out)
}
}
return out
}
// outlineView 是左侧「大纲」面板。
func (a *App) outlineView(c *ui.Context) {
t := c.Theme()
ui.Column(c).FillHeight().Radius(t.Radius).Background(t.Surface).Clip().Children(func() {
ui.Row(c).Padding(6, 8).Gap(4).AlignItems(ui.Center).
BorderWidth(0, 0, 1, 0).BorderColor(t.Border).Children(func() {
ui.Text(c, "大纲").FontSize(11).Bold().TextColor(t.TextMuted)
ui.Box(c).Grow(1)
ui.Textf(c, "%d", len(a.outline)).FontSize(10).TextColor(t.TextMuted)
})
d := a.doc()
if d == nil {
a.outlineEmpty(c, "没有打开的文件")
return
}
if a.lsp == nil {
a.outlineEmpty(c, "语言服务器未就绪")
return
}
if !lsp.IsGoFile(d.path) {
a.outlineEmpty(c, "大纲暂时只支持 Go 文件")
return
}
// 切了文件先清空,避免短暂显示上一个文件的符号。
if a.outlinePath != d.path {
a.outline = nil
}
a.syncOutline(d)
if len(a.outline) == 0 {
a.outlineEmpty(c, "正在分析…")
return
}
ui.Scroll(c).Grow(1).Children(func() {
ui.Column(c).Padding(2, 4).Gap(1).Children(func() {
a.selectedOutline = a.outlineRowIndex(d)
for i := range a.outline {
a.outlineRow(c, d, i)
}
})
})
})
}
// outlineEmpty 画一句占位说明。
func (a *App) outlineEmpty(c *ui.Context, msg string) {
t := c.Theme()
ui.Column(c).Grow(1).Padding(10, 10).Children(func() {
ui.Text(c, msg).FontSize(12).TextColor(t.TextMuted).SingleLine()
})
}
// outlineRow 画一个符号。
func (a *App) outlineRow(c *ui.Context, d *Doc, i int) {
t := c.Theme()
it := a.outline[i]
active := i == a.selectedOutline
row := ui.ButtonBase(c).FillWidth().Padding(1, 4).Gap(5).Radius(3).
Justify(ui.Start).AlignItems(ui.Center).Cursor(ui.CursorPointer)
bgT := ui.RGBA(0, 0, 0, 0)
if active {
bgT = t.Accent.Alpha(0.16)
} else if row.Hovered() {
bgT = t.SurfaceHover
}
bg := row.AnimateColor("olbg", bgT, ui.Animation{Duration: 120 * time.Millisecond})
row = row.Background(bg)
row.Children(func() {
if it.Depth > 0 {
ui.Box(c).Width(float32(it.Depth) * 12)
}
// 用一个小色块代替类型图标,颜色区分函数/类型/字段。
ui.Box(c).Width(6).Height(6).Radius(1).Background(symbolColor(t, it.Kind))
ui.Text(c, it.Name).FontSize(12).TextColor(condColor(t, active)).SingleLine()
})
if row.Clicked() {
a.selectedOutline = i
a.gotoOutline(d, it)
}
}
// gotoOutline 跳到符号所在位置。
func (a *App) gotoOutline(d *Doc, it outlineItem) {
if it.Line < 0 || it.Line >= d.buf.LineCount() {
return
}
line := d.buf.Line(it.Line)
col := lsp.UTF16ToByte(line, it.Col)
d.st.GoTo(editor.Pos{Line: it.Line, Col: col})
d.st.ScrollTo(it.Line)
a.win.Update(func() { a.active = d.path })
}
// outlineRowIndex 返回光标所在符号的行号,没有匹配则 -1。
func (a *App) outlineRowIndex(d *Doc) int {
if d == nil || len(a.outline) == 0 {
return -1
}
cur := d.st.CursorPos().Line
best := -1
for i, it := range a.outline {
if it.Line <= cur && (best < 0 || it.Line >= a.outline[best].Line) {
best = i
}
}
return best
}
// symbolColor 按符号类型给出颜色。
func symbolColor(t *ui.Theme, kind int) ui.Color {
switch kind {
case 5, 10, 11, 22, 23, 26: // 类、字段、枚举、结构、类型参数
return t.Warning
case 6, 9, 12: // 方法、构造函数、函数
return t.Accent
case 8, 13, 14: // 接口、枚举成员、常量
return t.Success
}
return t.TextMuted
}