/
symbolsview.go
381 lines (332 loc) · 10.1 KB
/
symbolsview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package code
import (
"log"
"sort"
"strings"
"cogentcore.org/core/core"
"cogentcore.org/core/events"
"cogentcore.org/core/icons"
"cogentcore.org/core/parse/lexer"
"cogentcore.org/core/parse/syms"
"cogentcore.org/core/parse/token"
"cogentcore.org/core/styles"
"cogentcore.org/core/texteditor/textbuf"
"cogentcore.org/core/tree"
"cogentcore.org/core/views"
)
// SymbolsParams are parameters for structure view of file or package
type SymbolsParams struct {
// scope of symbols to list
Scope SymScopes
}
// SymbolsView is a widget that displays results of a file or package parse
type SymbolsView struct {
core.Layout
// parent code project
Code Code `json:"-" xml:"-"`
// params for structure display
SymParams SymbolsParams
// all the symbols for the file or package in a tree
Syms *SymNode
// only show symbols that match this string
Match string
}
// Params returns the symbols params
func (sv *SymbolsView) Params() *SymbolsParams {
return &sv.Code.ProjectSettings().Symbols
}
//////////////////////////////////////////////////////////////////////////////////////
// GUI config
// Config configures the view
func (sv *SymbolsView) ConfigSymbolsView(ge Code, sp SymbolsParams) {
sv.Code = ge
sv.SymParams = sp
if sv.HasChildren() {
return
}
sv.Style(func(s *styles.Style) {
s.Direction = styles.Column
s.Grow.Set(1, 1)
})
core.NewToolbar(sv, "sym-toolbar")
svfr := core.NewFrame(sv, "sym-frame")
svfr.Style(func(s *styles.Style) {
s.Grow.Set(1, 1)
s.Overflow.Set(styles.OverflowAuto)
})
sv.ConfigToolbar()
sb := sv.ScopeChooser()
sb.SetCurrentIndex(int(sv.Params().Scope))
sv.ConfigTree(sp.Scope)
}
// Toolbar returns the symbols toolbar
func (sv *SymbolsView) Toolbar() *core.Toolbar {
return sv.ChildByName("sym-toolbar", 0).(*core.Toolbar)
}
// Toolbar returns the spell toolbar
func (sv *SymbolsView) Frame() *core.Frame {
return sv.ChildByName("sym-frame", 0).(*core.Frame)
}
// ScopeChooser returns the scope Chooser
func (sv *SymbolsView) ScopeChooser() *core.Chooser {
return sv.Toolbar().ChildByName("scope-chooser", 5).(*core.Chooser)
}
// SearchText returns the unknown word textfield from toolbar
func (sv *SymbolsView) SearchText() *core.TextField {
return sv.Toolbar().ChildByName("search-str", 1).(*core.TextField)
}
// ConfigToolbar adds toolbar.
func (sv *SymbolsView) ConfigToolbar() {
tb := sv.Toolbar()
if tb.HasChildren() {
return
}
core.NewButton(tb).SetText("Refresh").SetIcon(icons.Update).
SetTooltip("refresh symbols for current file and scope").
OnClick(func(e events.Event) {
sv.RefreshAction()
})
sl := core.NewLabel(tb).SetText("Scope:").SetTooltip("scope symbols to:")
ch := core.NewChooser(tb, "scope-chooser").SetEnum(sv.Params().Scope)
ch.SetTooltip(sl.Tooltip)
ch.OnChange(func(e events.Event) {
sv.Params().Scope = ch.CurrentItem.Value.(SymScopes)
sv.ConfigTree(sv.Params().Scope)
sv.SearchText().SetFocusEvent()
})
ch.SetCurrentValue(sv.Params().Scope)
core.NewLabel(tb).SetText("Search:").
SetTooltip("narrow symbols list to symbols containing text you enter here")
tf := core.NewTextField(tb, "search-str")
tf.SetTooltip("narrow symbols list by entering a search string -- case is ignored if string is all lowercase -- otherwise case is matched")
tf.OnChange(func(e events.Event) {
sv.Match = tf.Text()
sv.ConfigTree(sv.Params().Scope)
tf.CursorEnd()
tf.SetFocusEvent()
})
}
// RefreshAction loads symbols for current file and scope
func (sv *SymbolsView) RefreshAction() {
sv.ConfigTree(SymScopes(sv.Params().Scope))
sv.SearchText().SetFocusEvent()
}
// ConfigTree adds a treeview to the symbolsview.
// This is called for refresh action.
func (sv *SymbolsView) ConfigTree(scope SymScopes) {
sfr := sv.Frame()
var tv *SymTreeView
if sv.Syms == nil {
sv.Syms = &SymNode{}
sv.Syms.InitName(sv.Syms, "syms")
tv = NewSymTreeView(sfr)
tv.SyncTree(sv.Syms)
tv.OnSelect(func(e events.Event) {
if len(tv.SelectedNodes) == 0 {
return
}
sn := tv.SelectedNodes[0].AsTreeView().SyncNode.(*SymNode)
if sn != nil {
SelectSymbol(sv.Code, sn.Symbol)
}
})
} else {
tv = sfr.Child(0).(*SymTreeView)
}
if scope == SymScopePackage {
sv.OpenPackage()
} else {
sv.OpenFile()
}
tv.ReSync()
tv.OpenAll()
sfr.NeedsLayout()
}
func SelectSymbol(ge Code, ssym syms.Symbol) {
tv := ge.ActiveTextEditor()
if tv == nil || tv.Buffer == nil || string(tv.Buffer.Filename) != ssym.Filename {
var ok = false
tr := textbuf.NewRegion(ssym.SelectReg.St.Ln, ssym.SelectReg.St.Ch, ssym.SelectReg.Ed.Ln, ssym.SelectReg.Ed.Ch)
tv, ok = ge.OpenFileAtRegion(core.Filename(ssym.Filename), tr)
if !ok {
log.Printf("CodeView SelectSymbol: OpenFileAtRegion returned false: %v\n", ssym.Filename)
}
return
}
tv.Highlights = tv.Highlights[:0]
tr := textbuf.NewRegion(ssym.SelectReg.St.Ln, ssym.SelectReg.St.Ch, ssym.SelectReg.Ed.Ln, ssym.SelectReg.Ed.Ch)
tv.Highlights = append(tv.Highlights, tr)
tv.SetCursorTarget(tr.Start)
tv.SetFocusEvent()
ge.FocusOnTabs()
tv.NeedsLayout()
}
// OpenPackage opens package-level symbols for current active texteditor
func (sv *SymbolsView) OpenPackage() {
ge := sv.Code
tv := ge.ActiveTextEditor()
if sv.Syms == nil || tv == nil || tv.Buffer == nil || !tv.Buffer.Hi.UsingParse() {
return
}
pfs := tv.Buffer.ParseState.Done()
if len(pfs.ParseState.Scopes) == 0 {
core.MessageSnackbar(sv, "Symbols not yet parsed -- try again in a few moments")
return
}
pkg := pfs.ParseState.Scopes[0] // first scope of parse state is the full set of package symbols
sv.Syms.OpenSyms(pkg, "", sv.Match)
}
// OpenFile opens file-level symbols for current active texteditor
func (sv *SymbolsView) OpenFile() {
ge := sv.Code
tv := ge.ActiveTextEditor()
if sv.Syms == nil || tv == nil || tv.Buffer == nil || !tv.Buffer.Hi.UsingParse() {
return
}
pfs := tv.Buffer.ParseState.Done()
if len(pfs.ParseState.Scopes) == 0 {
core.MessageSnackbar(sv, "Symbols not yet parsed -- try again in a few moments")
return
}
pkg := pfs.ParseState.Scopes[0] // first scope of parse state is the full set of package symbols
sv.Syms.OpenSyms(pkg, string(tv.Buffer.Filename), sv.Match)
}
func symMatch(str, match string, ignoreCase bool) bool {
if match == "" {
return true
}
if ignoreCase {
return strings.Contains(strings.ToLower(str), match)
}
return strings.Contains(str, match)
}
// OpenSyms opens symbols from given symbol map (assumed to be package-level symbols)
// filtered by filename and match -- called on root node of tree.
func (sn *SymNode) OpenSyms(pkg *syms.Symbol, fname, match string) {
sn.DeleteChildren()
gvars := []syms.Symbol{} // collect and list global vars first
funcs := []syms.Symbol{} // collect and add functions (no receiver) to end
ignoreCase := !lexer.HasUpperCase(match)
sls := pkg.Children.Slice(true)
for _, sy := range sls {
if fname != "" {
if sy.Filename != fname { // this is what restricts to single file
continue
}
}
if sy.Name == "" || sy.Name[0] == '_' {
continue
}
switch {
case sy.Kind.SubCat() == token.NameFunction:
if symMatch(sy.Name, match, ignoreCase) {
funcs = append(funcs, *sy)
}
case sy.Kind.SubCat() == token.NameVar:
if symMatch(sy.Name, match, ignoreCase) {
gvars = append(gvars, *sy)
}
case sy.Kind.SubCat() == token.NameType:
var methods []syms.Symbol
var fields []syms.Symbol
for _, x := range sy.Children {
if symMatch(x.Name, match, ignoreCase) {
if x.Kind == token.NameMethod {
methods = append(methods, *x)
} else if x.Kind == token.NameField {
fields = append(fields, *x)
}
}
}
if symMatch(sy.Name, match, ignoreCase) || len(methods) > 0 || len(fields) > 0 {
kn := NewSymNode(sn, sy.Name).SetSymbol(*sy)
sort.Slice(fields, func(i, j int) bool {
return fields[i].Name < fields[j].Name
})
sort.Slice(methods, func(i, j int) bool {
return methods[i].Name < methods[j].Name
})
for _, fld := range fields {
NewSymNode(kn, fld.Label()).SetSymbol(fld)
}
for _, mth := range methods {
NewSymNode(kn, mth.Label()).SetSymbol(mth)
}
}
}
}
for _, fn := range funcs {
NewSymNode(sn, fn.Label()).SetSymbol(fn)
}
for _, vr := range gvars {
NewSymNode(sn, vr.Label()).SetSymbol(vr)
}
}
/////////////////////////////////////////////////////////////////////////////
// SymNode
// SymScopes corresponds to the search scope
type SymScopes int32 //enums:enum -trim-prefix SymScope
const (
// SymScopePackage scopes list of symbols to the package of the active file
SymScopePackage SymScopes = iota
// SymScopeFile restricts the list of symbols to the active file
SymScopeFile
)
// SymNode represents a language symbol -- the name of the node is
// the name of the symbol. Some symbols, e.g. type have children
type SymNode struct {
tree.NodeBase
// the symbol
Symbol syms.Symbol
}
// GetIcon returns the appropriate Icon for this symbol type
func (sy *SymNode) GetIcon() icons.Icon {
ic := icons.Blank
switch sy.Symbol.Kind {
case token.NameType:
ic = icons.Title
case token.NameVar, token.NameVarGlobal:
ic = icons.Variable
case token.NameMethod:
ic = icons.Method
case token.NameFunction:
ic = icons.Function
case token.NameField:
ic = icons.Field
case token.NameConstant:
ic = icons.Constant
}
return ic
}
/////////////////////////////////////////////////////////////////////////////
// SymTreeView
// SymTreeView is a TreeView that knows how to operate on FileNode nodes
type SymTreeView struct {
views.TreeView
}
// SymNode returns the SrcNode as a *code* SymNode
func (st *SymTreeView) SymNode() *SymNode {
return st.SyncNode.(*SymNode)
}
func (st *SymTreeView) OnInit() {
st.TreeView.OnInit()
}
func (st *SymTreeView) UpdateBranchIcons() {
st.SetSymIcon()
}
func (st *SymTreeView) SetSymIcon() {
ic := st.SymNode().GetIcon()
if _, ok := st.BranchPart(); !ok {
st.Update()
}
if bp, ok := st.BranchPart(); ok {
if bp.IconIndeterminate != ic {
bp.IconIndeterminate = ic
bp.Update()
st.NeedsRender()
}
}
}