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 }