gitstack

grindlemire/go-tui code browser

9.6 KB Go 399 lines 2026-06-03 · d15bb9f raw
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
package lsp

import (
	"testing"
)

func TestResolveCursorContext_ForLoop(t *testing.T) {
	src := `package test

templ List(items []string) {
	<div>
		for _, item := range items {
			<span>{item}</span>
		}
	</div>
}
`
	doc := parseTestDoc(src)

	// Cursor on "for" keyword line (line 4)
	// "\t\tfor" — 'f' at col 2
	ctx := ResolveCursorContext(doc, Position{Line: 4, Character: 3})

	if ctx.NodeKind != NodeKindForLoop {
		t.Errorf("expected NodeKindForLoop, got %s", ctx.NodeKind)
	}
}

func TestResolveCursorContext_IfStmt(t *testing.T) {
	src := `package test

templ Conditional(show bool) {
	<div>
		if show {
			<span>visible</span>
		}
	</div>
}
`
	doc := parseTestDoc(src)

	// Cursor on "if" line (line 4)
	ctx := ResolveCursorContext(doc, Position{Line: 4, Character: 3})

	if ctx.NodeKind != NodeKindIfStmt {
		t.Errorf("expected NodeKindIfStmt, got %s", ctx.NodeKind)
	}
}

func TestResolveCursorContext_LetBinding(t *testing.T) {
	src := `package test

templ Example() {
	header := <div>title</div>
	{header}
}
`
	doc := parseTestDoc(src)

	// Cursor on "header" — "\theader := <div>..." — 'header' starts at col 1
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 2})

	if ctx.NodeKind != NodeKindLetBinding {
		t.Errorf("expected NodeKindLetBinding, got %s", ctx.NodeKind)
	}
}

func TestResolveCursorContext_ComponentCall(t *testing.T) {
	src := `package test

templ Page() {
	@Header("My Title")
}

templ Header(title string) {
	<div>{title}</div>
}
`
	doc := parseTestDoc(src)

	// Cursor on "@Header" (line 3)
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 2})

	if ctx.NodeKind != NodeKindComponentCall {
		t.Errorf("expected NodeKindComponentCall, got %s", ctx.NodeKind)
	}
}

func TestResolveCursorContext_Function(t *testing.T) {
	src := `package test

func helper(s string) string {
	return s
}
`
	doc := parseTestDoc(src)

	// Cursor on "func" line (line 2)
	ctx := ResolveCursorContext(doc, Position{Line: 2, Character: 5})

	if ctx.NodeKind != NodeKindFunction {
		t.Errorf("expected NodeKindFunction, got %s", ctx.NodeKind)
	}
}

func TestResolveCursorContext_GoExpr(t *testing.T) {
	src := `package test

templ Display(count int) {
	<span>{fmt.Sprintf("%d", count)}</span>
}
`
	doc := parseTestDoc(src)

	// Cursor inside {fmt.Sprintf...} — inside the braces
	// "\t<span>{fmt.Sprintf(..." — '{' is around col 7
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 10})

	if !ctx.InGoExpr {
		t.Error("expected InGoExpr to be true")
	}
}

func TestResolveCursorContext_TailwindClass(t *testing.T) {
	src := `package test

templ Box() {
	<div class="flex-col p-2">content</div>
}
`
	doc := parseTestDoc(src)

	// Cursor inside class="flex-col" — on "flex-col"
	// "\t<div class=\"flex-col p-2\">" — need position inside the class value
	line := getLineText(doc.Content, 3)
	classIdx := -1
	for i := 0; i < len(line)-6; i++ {
		if line[i:i+6] == `class=` {
			classIdx = i
			break
		}
	}
	if classIdx < 0 {
		t.Fatal("could not find class= in line")
	}
	// Position right after class=" — cursor on 'f' of flex-col
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: classIdx + 7})

	if !ctx.InClassAttr {
		t.Error("expected InClassAttr to be true")
	}
}

func TestResolveCursorContext_InElement(t *testing.T) {
	src := `package test

templ Box() {
	<div class="p-1">content</div>
}
`
	doc := parseTestDoc(src)

	// Cursor inside element tag (between < and >), on "class"
	line := getLineText(doc.Content, 3)
	classIdx := -1
	for i := 0; i < len(line)-5; i++ {
		if line[i:i+5] == "class" {
			classIdx = i
			break
		}
	}
	if classIdx < 0 {
		t.Fatal("could not find 'class' in line")
	}
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: classIdx})

	if !ctx.InElement {
		t.Error("expected InElement to be true")
	}
}

func TestResolveCursorContext_Keyword(t *testing.T) {
	src := `package test
`
	doc := parseTestDoc(src)

	// Cursor on "package" (line 0, character 3)
	ctx := ResolveCursorContext(doc, Position{Line: 0, Character: 3})

	if ctx.NodeKind != NodeKindKeyword {
		t.Errorf("expected NodeKindKeyword, got %s", ctx.NodeKind)
	}
	if ctx.Word != "package" {
		t.Errorf("expected Word 'package', got %q", ctx.Word)
	}
}

func TestResolveCursorContext_ScopeCollectsRefs(t *testing.T) {
	src := `package test

templ Layout() {
	<div ref={header} class="p-1">title</div>
	<div ref={footer} class="p-1">footer</div>
}
`
	doc := parseTestDoc(src)

	// Cursor anywhere inside the component body
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 2})

	if ctx.Scope.Component == nil {
		t.Fatal("expected Scope.Component to be set")
	}
	if len(ctx.Scope.Refs) < 2 {
		t.Errorf("expected at least 2 refs in scope, got %d", len(ctx.Scope.Refs))
	}

	// Verify ref names
	refNames := make(map[string]bool)
	for _, ref := range ctx.Scope.Refs {
		refNames[ref.Name] = true
	}
	if !refNames["header"] {
		t.Error("expected 'header' in refs")
	}
	if !refNames["footer"] {
		t.Error("expected 'footer' in refs")
	}
}

func TestResolveCursorContext_ScopeCollectsLetBindings(t *testing.T) {
	src := `package test

templ Example() {
	title := <span>Hello</span>
	{title}
}
`
	doc := parseTestDoc(src)

	// Cursor inside the component body
	ctx := ResolveCursorContext(doc, Position{Line: 4, Character: 2})

	if ctx.Scope.Component == nil {
		t.Fatal("expected Scope.Component to be set")
	}
	if len(ctx.Scope.LetBinds) < 1 {
		t.Errorf("expected at least 1 let binding in scope, got %d", len(ctx.Scope.LetBinds))
	}
}

func TestResolveCursorContext_ScopeCollectsParams(t *testing.T) {
	src := `package test

templ Header(title string, subtitle string) {
	<div>{title}</div>
}
`
	doc := parseTestDoc(src)

	// Cursor inside the component body
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 2})

	if len(ctx.Scope.Params) != 2 {
		t.Errorf("expected 2 params in scope, got %d", len(ctx.Scope.Params))
	}
}

func TestResolveCursorContext_NilAST(t *testing.T) {
	// Test graceful handling when AST is nil (unparseable file)
	doc := &Document{
		URI:     "file:///broken.gsx",
		Content: "this is not valid gsx at all {{{{",
		Version: 1,
		AST:     nil,
	}

	ctx := ResolveCursorContext(doc, Position{Line: 0, Character: 0})

	// Should not panic, should return a valid context
	if ctx == nil {
		t.Fatal("expected non-nil context")
	}
	if ctx.Document != doc {
		t.Error("expected Document to be set")
	}
}

func TestResolveCursorContext_EmptyDocument(t *testing.T) {
	doc := &Document{
		URI:     "file:///empty.gsx",
		Content: "",
		Version: 1,
		AST:     nil,
	}

	ctx := ResolveCursorContext(doc, Position{Line: 0, Character: 0})

	if ctx == nil {
		t.Fatal("expected non-nil context")
	}
	if ctx.NodeKind != NodeKindUnknown {
		t.Errorf("expected NodeKindUnknown for empty document, got %s", ctx.NodeKind)
	}
}

func TestResolveCursorContext_StateDecl(t *testing.T) {
	src := `package test

templ Counter() {
	count := tui.NewState(0)
	<span>{count.Get()}</span>
}
`
	doc := parseTestDoc(src)

	// Cursor on the state declaration line (line 3)
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 5})

	if ctx.NodeKind != NodeKindStateDecl {
		t.Errorf("expected NodeKindStateDecl, got %s", ctx.NodeKind)
	}
}

func TestResolveCursorContext_StateAccess(t *testing.T) {
	src := `package test

templ Counter() {
	count := tui.NewState(0)
	<span>{count.Get()}</span>
}
`
	doc := parseTestDoc(src)

	// Cursor on "count" inside {count.Get()} expression (line 4)
	// "\t<span>{count.Get()}</span>" — '{' is at col 7, 'count' starts at col 8
	ctx := ResolveCursorContext(doc, Position{Line: 4, Character: 9})

	if ctx.NodeKind != NodeKindStateAccess {
		t.Errorf("expected NodeKindStateAccess, got %s", ctx.NodeKind)
	}
}

func TestResolveCursorContext_TextContent(t *testing.T) {
	src := `package test

templ Simple() {
	<div>Hello World</div>
}
`
	doc := parseTestDoc(src)

	// Cursor on "Hello World" text content (line 3)
	// This is tricky because text content and element tag are on the same line.
	// The inline text may be parsed as part of the element's children.
	// Let's test the word extraction instead.
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 8})

	if ctx.Word == "" {
		t.Error("expected Word to be non-empty on text content")
	}
}

func TestNodeKind_String(t *testing.T) {
	type tc struct {
		kind NodeKind
		want string
	}

	tests := map[string]tc{
		"component":      {kind: NodeKindComponent, want: "Component"},
		"element":        {kind: NodeKindElement, want: "Element"},
		"attribute":      {kind: NodeKindAttribute, want: "Attribute"},
		"ref attr":       {kind: NodeKindRefAttr, want: "RefAttr"},
		"go expr":        {kind: NodeKindGoExpr, want: "GoExpr"},
		"for loop":       {kind: NodeKindForLoop, want: "ForLoop"},
		"if stmt":        {kind: NodeKindIfStmt, want: "IfStmt"},
		"let binding":    {kind: NodeKindLetBinding, want: "LetBinding"},
		"state decl":     {kind: NodeKindStateDecl, want: "StateDecl"},
		"state access":   {kind: NodeKindStateAccess, want: "StateAccess"},
		"parameter":      {kind: NodeKindParameter, want: "Parameter"},
		"function":       {kind: NodeKindFunction, want: "Function"},
		"component call": {kind: NodeKindComponentCall, want: "ComponentCall"},
		"event handler":  {kind: NodeKindEventHandler, want: "EventHandler"},
		"text":           {kind: NodeKindText, want: "Text"},
		"keyword":        {kind: NodeKindKeyword, want: "Keyword"},
		"tailwind class": {kind: NodeKindTailwindClass, want: "TailwindClass"},
		"unknown":        {kind: NodeKindUnknown, want: "Unknown"},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := tt.kind.String()
			if got != tt.want {
				t.Errorf("NodeKind(%d).String() = %q, want %q", tt.kind, got, tt.want)
			}
		})
	}
}