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
|
package lsp
import (
"regexp"
"strings"
"sync"
"github.com/grindlemire/go-tui/internal/tuigen"
)
type ComponentInfo struct {
Name string
Location Location
Params []*tuigen.Param
}
type FuncInfo struct {
Name string
Location Location
Signature string
Params []FuncParam
Returns string
}
type FuncParam struct {
Name string
Type string
Position Position
}
type ParamInfo struct {
Name string
Type string
ComponentName string
Location Location
}
type ComponentIndex struct {
mu sync.RWMutex
Components map[string]*ComponentInfo
FileComponents map[string][]string
Functions map[string]*FuncInfo
FileFunctions map[string][]string
Params map[string]*ParamInfo
FileParams map[string][]string
}
func NewComponentIndex() *ComponentIndex {
return &ComponentIndex{
Components: make(map[string]*ComponentInfo),
FileComponents: make(map[string][]string),
Functions: make(map[string]*FuncInfo),
FileFunctions: make(map[string][]string),
Params: make(map[string]*ParamInfo),
FileParams: make(map[string][]string),
}
}
func (idx *ComponentIndex) Add(uri string, comp *tuigen.Component) {
idx.mu.Lock()
defer idx.mu.Unlock()
info := &ComponentInfo{
Name: comp.Name,
Params: comp.Params,
Location: Location{
URI: uri,
Range: Range{
Start: Position{
Line: comp.Position.Line - 1,
Character: comp.Position.Column - 1,
},
End: Position{
Line: comp.Position.Line - 1,
Character: comp.Position.Column - 1 + len("@component") + 1 + len(comp.Name),
},
},
},
}
idx.Components[comp.Name] = info
idx.FileComponents[uri] = append(idx.FileComponents[uri], comp.Name)
}
func (idx *ComponentIndex) Remove(uri string) {
idx.mu.Lock()
defer idx.mu.Unlock()
for _, name := range idx.FileComponents[uri] {
delete(idx.Components, name)
}
delete(idx.FileComponents, uri)
for _, name := range idx.FileFunctions[uri] {
delete(idx.Functions, name)
}
delete(idx.FileFunctions, uri)
for _, key := range idx.FileParams[uri] {
delete(idx.Params, key)
}
delete(idx.FileParams, uri)
}
func (idx *ComponentIndex) Lookup(name string) (*ComponentInfo, bool) {
idx.mu.RLock()
defer idx.mu.RUnlock()
info, ok := idx.Components[name]
return info, ok
}
func (idx *ComponentIndex) All() []string {
idx.mu.RLock()
defer idx.mu.RUnlock()
names := make([]string, 0, len(idx.Components))
for name := range idx.Components {
names = append(names, name)
}
return names
}
func (idx *ComponentIndex) ComponentsInFile(uri string) []string {
idx.mu.RLock()
defer idx.mu.RUnlock()
return idx.FileComponents[uri]
}
func (idx *ComponentIndex) IndexDocument(uri string, ast *tuigen.File) {
idx.Remove(uri)
if ast == nil {
return
}
for _, comp := range ast.Components {
idx.Add(uri, comp)
idx.AddComponentParams(uri, comp)
}
for _, fn := range ast.Funcs {
idx.AddFunc(uri, fn)
}
}
func (idx *ComponentIndex) GetInfo(name string) *ComponentInfo {
idx.mu.RLock()
defer idx.mu.RUnlock()
return idx.Components[name]
}
func (idx *ComponentIndex) AddFunc(uri string, fn *tuigen.GoFunc) {
idx.mu.Lock()
defer idx.mu.Unlock()
name, sig, params, returns := parseFuncSignature(fn.Code)
if name == "" {
return
}
for i := range params {
params[i].Position = Position{
Line: fn.Position.Line - 1,
Character: fn.Position.Column - 1 + params[i].Position.Character,
}
}
info := &FuncInfo{
Name: name,
Signature: sig,
Params: params,
Returns: returns,
Location: Location{
URI: uri,
Range: Range{
Start: Position{
Line: fn.Position.Line - 1,
Character: fn.Position.Column - 1,
},
End: Position{
Line: fn.Position.Line - 1,
Character: fn.Position.Column - 1 + len("func") + 1 + len(name),
},
},
},
}
idx.Functions[name] = info
idx.FileFunctions[uri] = append(idx.FileFunctions[uri], name)
}
func (idx *ComponentIndex) AddComponentParams(uri string, comp *tuigen.Component) {
idx.mu.Lock()
defer idx.mu.Unlock()
for _, p := range comp.Params {
key := comp.Name + "." + p.Name
info := &ParamInfo{
Name: p.Name,
Type: p.Type,
ComponentName: comp.Name,
Location: Location{
URI: uri,
Range: Range{
Start: Position{
Line: p.Position.Line - 1,
Character: p.Position.Column - 1,
},
End: Position{
Line: p.Position.Line - 1,
Character: p.Position.Column - 1 + len(p.Name),
},
},
},
}
idx.Params[key] = info
idx.FileParams[uri] = append(idx.FileParams[uri], key)
}
}
func (idx *ComponentIndex) LookupFunc(name string) (*FuncInfo, bool) {
idx.mu.RLock()
defer idx.mu.RUnlock()
info, ok := idx.Functions[name]
return info, ok
}
func (idx *ComponentIndex) LookupParam(componentName, paramName string) (*ParamInfo, bool) {
idx.mu.RLock()
defer idx.mu.RUnlock()
key := componentName + "." + paramName
info, ok := idx.Params[key]
return info, ok
}
func (idx *ComponentIndex) LookupParamInAnyComponent(paramName string) (*ParamInfo, bool) {
idx.mu.RLock()
defer idx.mu.RUnlock()
for _, info := range idx.Params {
if info.Name == paramName {
return info, true
}
}
return nil, false
}
func (idx *ComponentIndex) LookupFuncParam(funcName, paramName string) (*FuncParam, string, bool) {
idx.mu.RLock()
defer idx.mu.RUnlock()
funcInfo, ok := idx.Functions[funcName]
if !ok {
return nil, "", false
}
for i := range funcInfo.Params {
if funcInfo.Params[i].Name == paramName {
return &funcInfo.Params[i], funcInfo.Location.URI, true
}
}
return nil, "", false
}
func (idx *ComponentIndex) AllFunctions() []string {
idx.mu.RLock()
defer idx.mu.RUnlock()
names := make([]string, 0, len(idx.Functions))
for name := range idx.Functions {
names = append(names, name)
}
return names
}
func parseFuncSignature(code string) (name, signature string, params []FuncParam, returns string) {
re := regexp.MustCompile(`func\s+(\w+)\s*\(([^)]*)\)\s*([^{]*)`)
matches := re.FindStringSubmatch(code)
if len(matches) < 2 {
return "", "", nil, ""
}
name = matches[1]
paramStr := ""
if len(matches) > 2 {
paramStr = strings.TrimSpace(matches[2])
}
if len(matches) > 3 {
returns = strings.TrimSpace(matches[3])
}
signature = "func " + name + "(" + paramStr + ")"
if returns != "" {
signature += " " + returns
}
if paramStr != "" {
parenIdx := strings.Index(code, "(")
paramContentStart := parenIdx + 1
paramParts := strings.Split(paramStr, ",")
offset := 0
for _, rawPart := range paramParts {
trimmed := strings.TrimSpace(rawPart)
fields := strings.Fields(trimmed)
if len(fields) >= 2 {
nameInPart := strings.Index(rawPart, fields[0])
charPos := paramContentStart + offset + nameInPart
params = append(params, FuncParam{
Name: fields[0],
Type: strings.Join(fields[1:], " "),
Position: Position{
Character: charPos,
},
})
} else if len(fields) == 1 {
params = append(params, FuncParam{
Name: fields[0],
})
}
offset += len(rawPart) + 1
}
}
return name, signature, params, returns
}
|