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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
package provider
import (
"fmt"
"strings"
"github.com/grindlemire/go-tui/internal/lsp/gopls"
"github.com/grindlemire/go-tui/internal/lsp/log"
"github.com/grindlemire/go-tui/internal/lsp/schema"
"github.com/grindlemire/go-tui/internal/tuigen"
)
type hoverProvider struct {
index ComponentIndex
goplsProxy GoplsProxyAccessor
virtualFiles VirtualFileAccessor
}
func NewHoverProvider(index ComponentIndex, proxy GoplsProxyAccessor, vf VirtualFileAccessor) HoverProvider {
return &hoverProvider{
index: index,
goplsProxy: proxy,
virtualFiles: vf,
}
}
func (h *hoverProvider) Hover(ctx *CursorContext) (*Hover, error) {
log.Server("Hover provider: NodeKind=%s, Word=%q, InGoExpr=%v, InClassAttr=%v",
ctx.NodeKind, ctx.Word, ctx.InGoExpr, ctx.InClassAttr)
if ctx.InGoExpr {
hover, err := h.getGoplsHover(ctx)
if err != nil {
log.Server("gopls hover error: %v", err)
} else if hover != nil {
return hover, nil
}
}
switch ctx.NodeKind {
case NodeKindComponent:
hover, err := h.getGoplsHover(ctx)
if err == nil && hover != nil {
return hover, nil
}
return h.hoverComponent(ctx)
case NodeKindElement:
return h.hoverElement(ctx)
case NodeKindAttribute:
return h.hoverAttribute(ctx)
case NodeKindEventHandler:
return h.hoverEventHandler(ctx)
case NodeKindParameter:
return h.hoverParameter(ctx)
case NodeKindKeyword:
return h.hoverKeyword(ctx)
case NodeKindForLoop:
return h.hoverKeyword(ctx)
case NodeKindIfStmt:
return h.hoverKeyword(ctx)
case NodeKindLetBinding:
return h.hoverKeyword(ctx)
case NodeKindFunction:
hover, err := h.getGoplsHover(ctx)
if err == nil && hover != nil {
return hover, nil
}
return h.hoverFunction(ctx)
case NodeKindComponentCall:
return h.hoverComponentCall(ctx)
case NodeKindRefAttr:
return h.hoverRefAttr(ctx)
case NodeKindStateDecl:
return h.hoverStateDecl(ctx)
case NodeKindStateAccess:
return h.hoverStateAccess(ctx)
case NodeKindTailwindClass:
return h.hoverTailwindClass(ctx)
case NodeKindGoExpr:
}
word := ctx.Word
if word == "" {
return nil, nil
}
if hover := h.hoverForKeyword(word); hover != nil {
return hover, nil
}
componentName := strings.TrimPrefix(word, "@")
if info, ok := h.index.Lookup(componentName); ok {
return hoverForComponentInfo(info), nil
}
if funcInfo, ok := h.index.LookupFunc(word); ok {
return hoverForFuncInfo(funcInfo), nil
}
if ctx.Scope.Component != nil {
if paramInfo, ok := h.index.LookupParam(ctx.Scope.Component.Name, word); ok {
return hoverForParamInfo(paramInfo), nil
}
}
if elem := schema.GetElement(word); elem != nil {
return hoverForElement(elem), nil
}
if ctx.InClassAttr {
return h.hoverForTailwindWord(ctx)
}
if ctx.InElement && ctx.AttrTag != "" {
if attr := schema.GetAttribute(ctx.AttrTag, word); attr != nil {
return hoverForAttributeDef(ctx.AttrTag, attr), nil
}
}
return nil, nil
}
func (h *hoverProvider) hoverComponent(ctx *CursorContext) (*Hover, error) {
comp, ok := ctx.Node.(*tuigen.Component)
if !ok || comp == nil {
return nil, nil
}
if info, ok := h.index.Lookup(comp.Name); ok {
return hoverForComponentInfo(info), nil
}
var params []string
for _, p := range comp.Params {
params = append(params, fmt.Sprintf("%s %s", p.Name, p.Type))
}
sig := fmt.Sprintf("func %s(%s) *element.Element", comp.Name, strings.Join(params, ", "))
md := fmt.Sprintf("```go\n%s\n```\n\n**TUI Component**", sig)
return markdownHover(md), nil
}
func (h *hoverProvider) hoverElement(ctx *CursorContext) (*Hover, error) {
elem, ok := ctx.Node.(*tuigen.Element)
if !ok || elem == nil {
return nil, nil
}
def := schema.GetElement(elem.Tag)
if def == nil {
return nil, nil
}
return hoverForElement(def), nil
}
func (h *hoverProvider) hoverAttribute(ctx *CursorContext) (*Hover, error) {
if ctx.AttrTag == "" || ctx.AttrName == "" {
return nil, nil
}
attr := schema.GetAttribute(ctx.AttrTag, ctx.AttrName)
if attr != nil {
return hoverForAttributeDef(ctx.AttrTag, attr), nil
}
return markdownHover(fmt.Sprintf("**%s** attribute on `<%s>`", ctx.AttrName, ctx.AttrTag)), nil
}
func (h *hoverProvider) hoverEventHandler(ctx *CursorContext) (*Hover, error) {
handler := schema.GetEventHandler(ctx.AttrName)
if handler != nil {
md := fmt.Sprintf("**Event Handler** `%s`\n\nType: `%s`\n\n%s",
handler.Name, handler.Signature, handler.Description)
return markdownHover(md), nil
}
return nil, nil
}
func (h *hoverProvider) hoverParameter(ctx *CursorContext) (*Hover, error) {
param, ok := ctx.Node.(*tuigen.Param)
if !ok || param == nil {
return nil, nil
}
compName := ""
if ctx.Scope.Component != nil {
compName = ctx.Scope.Component.Name
}
md := fmt.Sprintf("```go\n%s %s\n```\n\n**Parameter** of component `%s`",
param.Name, param.Type, compName)
return markdownHover(md), nil
}
func (h *hoverProvider) hoverKeyword(ctx *CursorContext) (*Hover, error) {
return h.hoverForKeyword(ctx.Word), nil
}
func (h *hoverProvider) hoverFunction(ctx *CursorContext) (*Hover, error) {
fn, ok := ctx.Node.(*tuigen.GoFunc)
if !ok || fn == nil {
return nil, nil
}
word := ctx.Word
if funcInfo, ok := h.index.LookupFunc(word); ok {
return hoverForFuncInfo(funcInfo), nil
}
return nil, nil
}
func (h *hoverProvider) hoverComponentCall(ctx *CursorContext) (*Hover, error) {
call, ok := ctx.Node.(*tuigen.ComponentCall)
if !ok || call == nil {
return nil, nil
}
if info, ok := h.index.Lookup(call.Name); ok {
return hoverForComponentInfo(info), nil
}
return nil, nil
}
func (h *hoverProvider) hoverRefAttr(ctx *CursorContext) (*Hover, error) {
elem, ok := ctx.Node.(*tuigen.Element)
if !ok || elem == nil || elem.RefExpr == nil {
return nil, nil
}
refName := elem.RefExpr.Code
exportName := refName
if len(refName) > 0 {
exportName = strings.ToUpper(refName[:1]) + refName[1:]
}
refType := "`*tui.Element`"
refContext := "Simple (direct access)"
accessPattern := fmt.Sprintf("`view.%s`", exportName)
for _, ref := range ctx.Scope.Refs {
if ref.Name == refName {
if ref.InLoop {
if ref.KeyExpr != "" {
refType = "`map[KeyType]*tui.Element`"
refContext = "Keyed (map access)"
accessPattern = fmt.Sprintf("`view.%s[key]`", exportName)
} else {
refType = "`[]*tui.Element`"
refContext = "Loop (slice access)"
accessPattern = fmt.Sprintf("`view.%s[i]`", exportName)
}
}
if ref.InConditional {
refContext += " (nullable)"
}
break
}
}
md := fmt.Sprintf("**Element Ref** `%s`\n\nType: %s\n\nContext: %s\n\nAccess via view struct: %s",
refName, refType, refContext, accessPattern)
return markdownHover(md), nil
}
func (h *hoverProvider) hoverStateDecl(ctx *CursorContext) (*Hover, error) {
for _, sv := range ctx.Scope.StateVars {
if sv.Name == ctx.Word {
md := fmt.Sprintf("**State Variable** `%s`\n\nType: `*tui.State[%s]`\n\nInitial: `%s`\n\nMethods: Get(), Set(), Update(), Bind(), Batch()",
sv.Name, sv.Type, sv.InitExpr)
return markdownHover(md), nil
}
}
md := "**State Declaration** (`tui.NewState`)\n\nCreates a reactive state variable."
return markdownHover(md), nil
}
func (h *hoverProvider) hoverStateAccess(ctx *CursorContext) (*Hover, error) {
word := ctx.Word
if strings.HasSuffix(word, "Get") || word == "Get" {
return markdownHover("**State.Get()** — Returns the current value of the state variable."), nil
}
if strings.HasSuffix(word, "Set") || word == "Set" {
return markdownHover("**State.Set(value)** — Sets a new value for the state variable."), nil
}
if strings.HasSuffix(word, "Update") || word == "Update" {
return markdownHover("**State.Update(fn)** — Updates the state using a function that receives the current value."), nil
}
if strings.HasSuffix(word, "Bind") || word == "Bind" {
return markdownHover("**State.Bind(fn)** — Registers a callback that is called when the state changes."), nil
}
if strings.HasSuffix(word, "Batch") || word == "Batch" {
return markdownHover("**State.Batch(fn)** — Batches multiple state updates into a single re-render."), nil
}
return markdownHover("**State Access** — Reactive state method call."), nil
}
func (h *hoverProvider) hoverTailwindClass(ctx *CursorContext) (*Hover, error) {
return h.hoverForTailwindWord(ctx)
}
func (h *hoverProvider) hoverForTailwindWord(ctx *CursorContext) (*Hover, error) {
offset := ctx.Offset
content := ctx.Document.Content
searchStart := max(offset-maxClassAttrSearchDistance, 0)
segment := content[searchStart:offset]
classIdx := strings.LastIndex(segment, `class="`)
if classIdx == -1 {
return nil, nil
}
afterClass := segment[classIdx+7:]
if strings.Contains(afterClass, `"`) {
return nil, nil
}
classStart := searchStart + classIdx + 7
classContent := content[classStart:offset]
lastSpace := strings.LastIndex(classContent, " ")
var className string
if lastSpace == -1 {
className = classContent
} else {
className = classContent[lastSpace+1:]
}
endOffset := offset
for endOffset < len(content) && content[endOffset] != ' ' && content[endOffset] != '"' {
endOffset++
}
if endOffset > offset {
className += content[offset:endOffset]
}
className = strings.TrimSpace(className)
if className == "" {
return nil, nil
}
doc := schema.GetClassDoc(className)
if doc == "" {
return nil, nil
}
return markdownHover(fmt.Sprintf("**`%s`**\n\n%s", className, doc)), nil
}
func (h *hoverProvider) hoverForKeyword(word string) *Hover {
kw := schema.GetKeyword(word)
if kw == nil {
return nil
}
return markdownHover(kw.Documentation)
}
func hoverForComponentInfo(info *ComponentInfo) *Hover {
var params []string
for _, p := range info.Params {
params = append(params, fmt.Sprintf("%s %s", p.Name, p.Type))
}
sig := fmt.Sprintf("func %s(%s) *element.Element", info.Name, strings.Join(params, ", "))
md := fmt.Sprintf("```go\n%s\n```\n\n**TUI Component**", sig)
return markdownHover(md)
}
func hoverForFuncInfo(info *FuncInfo) *Hover {
md := fmt.Sprintf("```go\n%s\n```\n\n**Helper Function**", info.Signature)
return markdownHover(md)
}
func hoverForParamInfo(info *ParamInfo) *Hover {
md := fmt.Sprintf("```go\n%s %s\n```\n\n**Parameter** of component `%s`",
info.Name, info.Type, info.ComponentName)
return markdownHover(md)
}
func hoverForElement(def *schema.ElementDef) *Hover {
var lines []string
lines = append(lines, fmt.Sprintf("## `<%s>`", def.Tag))
lines = append(lines, "")
lines = append(lines, def.Description)
lines = append(lines, "")
lines = append(lines, "**Available attributes:**")
for _, attr := range def.Attributes {
lines = append(lines, fmt.Sprintf("- `%s` (%s): %s", attr.Name, attr.Type, attr.Description))
}
return markdownHover(strings.Join(lines, "\n"))
}
func hoverForAttributeDef(tag string, attr *schema.AttributeDef) *Hover {
md := fmt.Sprintf("**%s** (`%s`) on `<%s>`\n\n%s", attr.Name, attr.Type, tag, attr.Description)
return markdownHover(md)
}
func markdownHover(content string) *Hover {
return &Hover{
Contents: MarkupContent{
Kind: "markdown",
Value: content,
},
}
}
func (h *hoverProvider) getGoplsHover(ctx *CursorContext) (*Hover, error) {
proxy := h.goplsProxy.GetProxy()
if proxy == nil {
return nil, nil
}
cached := h.virtualFiles.GetVirtualFile(ctx.Document.URI)
if cached == nil || cached.SourceMap == nil {
return nil, nil
}
goLine, goCol, found := cached.SourceMap.TuiToGo(ctx.Position.Line, ctx.Position.Character)
if !found {
return nil, nil
}
goplsHover, err := proxy.Hover(cached.GoURI, gopls.Position{
Line: goLine,
Character: goCol,
})
if err != nil {
return nil, err
}
if goplsHover == nil {
return nil, nil
}
hover := &Hover{
Contents: MarkupContent{
Kind: goplsHover.Contents.Kind,
Value: goplsHover.Contents.Value,
},
}
if goplsHover.Range != nil {
tuiStartLine, tuiStartCol, startFound := cached.SourceMap.GoToTui(goplsHover.Range.Start.Line, goplsHover.Range.Start.Character)
tuiEndLine, tuiEndCol, endFound := cached.SourceMap.GoToTui(goplsHover.Range.End.Line, goplsHover.Range.End.Character)
if startFound && endFound {
hover.Range = &Range{
Start: Position{Line: tuiStartLine, Character: tuiStartCol},
End: Position{Line: tuiEndLine, Character: tuiEndCol},
}
}
}
return hover, nil
}
|