gitstack

grindlemire/go-tui code browser

4.5 KB Go 184 lines 2026-02-06 · 3ad544c 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
package lsp

import (
	"testing"

	"github.com/grindlemire/go-tui/internal/tuigen"
)

// parseTestDoc parses a .gsx source string into a Document for testing.
func parseTestDoc(src string) *Document {
	doc := &Document{
		URI:     "file:///test.gsx",
		Content: src,
		Version: 1,
	}
	lexer := tuigen.NewLexer("test.gsx", src)
	parser := tuigen.NewParser(lexer)
	ast, _ := parser.ParseFile()
	doc.AST = ast
	return doc
}

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

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

	// Cursor on "Header" (line 2, within the name)
	// Component position is at "templ" (tuigen col 1). nameEnd = 1 + len("Header") = 7.
	// The resolver checks tuigen col >= nameStart(1) && col <= nameEnd(7).
	// LSP Character 0 maps to tuigen col 1. So Character 0-6 should match.
	ctx := ResolveCursorContext(doc, Position{Line: 2, Character: 3})

	if ctx.NodeKind != NodeKindComponent {
		t.Errorf("expected NodeKindComponent, got %s", ctx.NodeKind)
	}
	if ctx.Scope.Component == nil {
		t.Fatal("expected Scope.Component to be set")
	}
	if ctx.Scope.Component.Name != "Header" {
		t.Errorf("expected component name 'Header', got %q", ctx.Scope.Component.Name)
	}
}

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

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

	// Cursor on "title" parameter — "templ Header(" is 14 chars, "title" starts at col 14
	ctx := ResolveCursorContext(doc, Position{Line: 2, Character: 14})

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

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

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

	// Cursor on "div" tag (line 3, the <div>)
	// "\t<div>" — tab is col 0, '<' is col 1, 'div' starts at col 2
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 2})

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

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

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

	// Cursor on "class" attribute — "\t<div class=..." — '<' is col 1, 'div' at col 2, space at col 5, 'class' starts at col 6
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 7})

	if ctx.NodeKind != NodeKindAttribute {
		t.Errorf("expected NodeKindAttribute, got %s", ctx.NodeKind)
	}
	if ctx.AttrTag != "div" {
		t.Errorf("expected AttrTag 'div', got %q", ctx.AttrTag)
	}
	if ctx.AttrName != "class" {
		t.Errorf("expected AttrName 'class', got %q", ctx.AttrName)
	}
}

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

templ Button(label string) {
	<button onFocus={handleFocus}>{label}</button>
}
`
	doc := parseTestDoc(src)

	// Cursor on "onFocus" — "\t<button onFocus=..." — 'onFocus' starts after '<button '
	// '<' is col 1, 'button' is col 2-7, space at col 8, 'onFocus' starts at col 9
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: 10})

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

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

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

	// Cursor on "ref={header}" — find "ref=" in the line
	line := getLineText(doc.Content, 3)
	refIdx := -1
	for i := 0; i < len(line)-4; i++ {
		if line[i:i+4] == "ref=" {
			refIdx = i
			break
		}
	}
	if refIdx < 0 {
		t.Fatal("could not find ref= in line")
	}

	// Position on the value part (inside ref={header})
	ctx := ResolveCursorContext(doc, Position{Line: 3, Character: refIdx + 5})

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

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

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

	// Cursor on "ref={header}" on its own line (line 4)
	line := getLineText(doc.Content, 4)
	refIdx := -1
	for i := 0; i < len(line)-4; i++ {
		if line[i:i+4] == "ref=" {
			refIdx = i
			break
		}
	}
	if refIdx < 0 {
		t.Fatal("could not find ref= in line")
	}

	ctx := ResolveCursorContext(doc, Position{Line: 4, Character: refIdx + 5})

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