gitstack

grindlemire/go-tui code browser

9.0 KB Go 434 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
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
package lsp

import (
	"encoding/json"
	"testing"
)

func TestWorkspaceSymbolDirect(t *testing.T) {
	type tc struct {
		contents    map[string]string
		query       string
		wantSymbols int
	}

	tests := map[string]tc{
		"empty query returns all": {
			contents: map[string]string{
				"file:///a.gsx": `package main

templ Hello() {
	<span>Hello</span>
}
`,
				"file:///b.gsx": `package main

templ World() {
	<span>World</span>
}
`,
			},
			query:       "",
			wantSymbols: 2,
		},
		"filter by query": {
			contents: map[string]string{
				"file:///a.gsx": `package main

templ Hello() {
	<span>Hello</span>
}
`,
				"file:///b.gsx": `package main

templ World() {
	<span>World</span>
}
`,
			},
			query:       "Hello",
			wantSymbols: 1,
		},
		"case insensitive query": {
			contents: map[string]string{
				"file:///a.gsx": `package main

templ HelloWorld() {
	<span>Hello</span>
}
`,
			},
			query:       "hello",
			wantSymbols: 1,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			server := NewServer(nil, nil)

			for uri, content := range tt.contents {
				doc := server.docs.Open(uri, content, 1)
				server.index.IndexDocument(uri, doc.AST)
			}

			params, _ := json.Marshal(WorkspaceSymbolParams{
				Query: tt.query,
			})

			result, rpcErr := server.router.Route(Request{Method: "workspace/symbol", Params: params})

			if rpcErr != nil {
				t.Fatalf("handleWorkspaceSymbol error: %v", rpcErr)
			}

			symbols, ok := result.([]SymbolInformation)
			if !ok {
				t.Fatalf("expected []SymbolInformation, got %T", result)
			}

			if len(symbols) != tt.wantSymbols {
				t.Errorf("got %d symbols, want %d", len(symbols), tt.wantSymbols)
			}
		})
	}
}

func TestGetElementAttributes(t *testing.T) {
	type tc struct {
		tag       string
		wantAttrs bool
	}

	tests := map[string]tc{
		"div element": {
			tag:       "div",
			wantAttrs: true,
		},
		"span element": {
			tag:       "span",
			wantAttrs: true,
		},
		"input element": {
			tag:       "input",
			wantAttrs: true,
		},
		"button element": {
			tag:       "button",
			wantAttrs: true,
		},
		"unknown element": {
			tag:       "unknown",
			wantAttrs: false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			attrs := getElementAttributes(tt.tag)
			if tt.wantAttrs && len(attrs) == 0 {
				t.Error("expected attributes, got none")
			}
			if !tt.wantAttrs && len(attrs) > 0 {
				t.Errorf("expected no attributes, got %d", len(attrs))
			}
		})
	}
}

func TestIsElementTag(t *testing.T) {
	type tc struct {
		word string
		want bool
	}

	tests := map[string]tc{
		"div":      {word: "div", want: true},
		"span":     {word: "span", want: true},
		"p":        {word: "p", want: true},
		"ul":       {word: "ul", want: true},
		"li":       {word: "li", want: true},
		"button":   {word: "button", want: true},
		"input":    {word: "input", want: true},
		"table":    {word: "table", want: true},
		"progress": {word: "progress", want: true},
		"unknown":  {word: "unknown", want: false},
		"empty":    {word: "", want: false},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := isElementTag(tt.word)
			if got != tt.want {
				t.Errorf("isElementTag(%q) = %v, want %v", tt.word, got, tt.want)
			}
		})
	}
}

func TestIsInClassAttribute(t *testing.T) {
	type tc struct {
		content    string
		line       int
		character  int
		wantInAttr bool
		wantPrefix string
	}

	tests := map[string]tc{
		"inside class attribute empty": {
			content: `package main

templ Hello() {
	<div class="">
	</div>
}
`,
			line:       3,
			character:  13, // Right after the opening quote
			wantInAttr: true,
			wantPrefix: "",
		},
		"inside class attribute with prefix": {
			content: `package main

templ Hello() {
	<div class="flex">
	</div>
}
`,
			line:       3,
			character:  17, // After "flex"
			wantInAttr: true,
			wantPrefix: "flex",
		},
		"inside class attribute partial class after space": {
			content: `package main

templ Hello() {
	<div class="flex-col gap">
	</div>
}
`,
			line:       3,
			character:  25, // After "gap"
			wantInAttr: true,
			wantPrefix: "gap",
		},
		"inside class attribute at space": {
			content: `package main

templ Hello() {
	<div class="flex-col ">
	</div>
}
`,
			line:       3,
			character:  22, // After space after "flex-col "
			wantInAttr: true,
			wantPrefix: "",
		},
		"not in class attribute - in id": {
			content: `package main

templ Hello() {
	<div id="test">
	</div>
}
`,
			line:       3,
			character:  13, // Inside id attribute
			wantInAttr: false,
			wantPrefix: "",
		},
		"not in class attribute - outside quotes": {
			content: `package main

templ Hello() {
	<div class="flex">
	</div>
}
`,
			line:       3,
			character:  6, // On "div"
			wantInAttr: false,
			wantPrefix: "",
		},
		"not in class attribute - different line": {
			content: `package main

templ Hello() {
	<div class="flex">
		<span>Hello</span>
	</div>
}
`,
			line:       4,
			character:  10, // Inside span
			wantInAttr: false,
			wantPrefix: "",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			server := NewServer(nil, nil)
			server.docs.Open("file:///test.gsx", tt.content, 1)
			doc := server.docs.Get("file:///test.gsx")

			gotInAttr, gotPrefix := server.isInClassAttribute(doc, Position{Line: tt.line, Character: tt.character})

			if gotInAttr != tt.wantInAttr {
				t.Errorf("isInClassAttribute() inAttr = %v, want %v", gotInAttr, tt.wantInAttr)
			}
			if gotPrefix != tt.wantPrefix {
				t.Errorf("isInClassAttribute() prefix = %q, want %q", gotPrefix, tt.wantPrefix)
			}
		})
	}
}

func TestGetTailwindCompletions(t *testing.T) {
	type tc struct {
		prefix    string
		wantCount int  // -1 means we just check > 0
		checkAll  bool // if true, check that all returned items have the prefix
	}

	tests := map[string]tc{
		"empty prefix returns all classes": {
			prefix:    "",
			wantCount: -1, // We don't know exact count, just check > 0
			checkAll:  false,
		},
		"flex prefix filters correctly": {
			prefix:    "flex",
			wantCount: -1,
			checkAll:  true,
		},
		"gap prefix filters correctly": {
			prefix:    "gap",
			wantCount: -1,
			checkAll:  true,
		},
		"no matches": {
			prefix:    "zzzznotaclass",
			wantCount: 0,
			checkAll:  false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			server := NewServer(nil, nil)
			items := server.getTailwindCompletions(tt.prefix)

			if tt.wantCount == -1 {
				if len(items) == 0 {
					t.Error("expected completion items, got none")
				}
			} else if len(items) != tt.wantCount {
				t.Errorf("got %d items, want %d", len(items), tt.wantCount)
			}

			// Check that all items have the prefix if requested
			if tt.checkAll {
				for _, item := range items {
					if len(item.Label) < len(tt.prefix) || item.Label[:len(tt.prefix)] != tt.prefix {
						t.Errorf("item %q does not have prefix %q", item.Label, tt.prefix)
					}
				}
			}

			// Check that completion items have documentation
			for _, item := range items {
				if item.Documentation == nil || item.Documentation.Value == "" {
					t.Errorf("item %q missing documentation", item.Label)
				}
				if item.Detail == "" {
					t.Errorf("item %q missing detail (category)", item.Label)
				}
			}
		})
	}
}

func TestTailwindCompletionInCompletion(t *testing.T) {
	type tc struct {
		content      string
		line         int
		character    int
		wantTailwind bool // true if we expect Tailwind completions
	}

	tests := map[string]tc{
		"inside class attribute": {
			content: `package main

templ Hello() {
	<div class="flex">
	</div>
}
`,
			line:         3,
			character:    13, // Inside class=""
			wantTailwind: true,
		},
		"not inside class attribute": {
			content: `package main

templ Hello() {
	<div id="test">
	</div>
}
`,
			line:         3,
			character:    13, // Inside id=""
			wantTailwind: false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			server := NewServer(nil, nil)

			doc := server.docs.Open("file:///test.gsx", tt.content, 1)
			server.index.IndexDocument("file:///test.gsx", doc.AST)

			completionParams := CompletionParams{
				TextDocument: TextDocumentIdentifier{URI: "file:///test.gsx"},
				Position:     Position{Line: tt.line, Character: tt.character},
			}

			params, _ := json.Marshal(completionParams)
			result, rpcErr := server.router.Route(Request{Method: "textDocument/completion", Params: params})

			if rpcErr != nil {
				t.Fatalf("handleCompletion error: %v", rpcErr)
			}

			list, ok := result.(*CompletionList)
			if !ok {
				t.Fatalf("expected CompletionList, got %T", result)
			}

			if tt.wantTailwind {
				if len(list.Items) == 0 {
					t.Error("expected Tailwind completion items, got none")
					return
				}
				// Check that we got Tailwind-style completions
				hasTailwindClass := false
				for _, item := range list.Items {
					if item.Label == "flex" || item.Label == "flex-col" || item.Label == "gap-1" {
						hasTailwindClass = true
						break
					}
				}
				if !hasTailwindClass {
					t.Error("expected Tailwind class completions, but didn't find expected classes")
				}
			}
		})
	}
}