gitstack

grindlemire/go-tui code browser

8.5 KB Go 321 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
package provider

import (
	"fmt"
	"strings"

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

// documentSymbolProvider implements DocumentSymbolProvider.
type documentSymbolProvider struct{}

// NewDocumentSymbolProvider creates a new document symbol provider.
func NewDocumentSymbolProvider() DocumentSymbolProvider {
	return &documentSymbolProvider{}
}

func (s *documentSymbolProvider) DocumentSymbols(doc *Document) ([]DocumentSymbol, error) {
	log.Server("DocumentSymbol provider for %s", doc.URI)

	if doc.AST == nil {
		return []DocumentSymbol{}, nil
	}

	var symbols []DocumentSymbol

	for _, comp := range doc.AST.Components {
		symbol := componentToSymbol(comp, doc.Content)
		symbols = append(symbols, symbol)
	}

	for _, fn := range doc.AST.Funcs {
		symbol := funcToSymbol(fn, doc.Content)
		symbols = append(symbols, symbol)
	}

	return symbols, nil
}

// componentToSymbol converts a component AST node to a DocumentSymbol.
func componentToSymbol(comp *tuigen.Component, content string) DocumentSymbol {
	// Build detail from parameters
	var params []string
	for _, p := range comp.Params {
		params = append(params, fmt.Sprintf("%s %s", p.Name, p.Type))
	}
	detail := fmt.Sprintf("(%s)", strings.Join(params, ", "))

	startPos := Position{
		Line:      comp.Position.Line - 1,
		Character: comp.Position.Column - 1,
	}

	nameEndPos := Position{
		Line:      comp.Position.Line - 1,
		Character: comp.Position.Column - 1 + len("templ") + 1 + len(comp.Name),
	}

	endPos := findComponentEnd(comp, content)

	fullRange := Range{Start: startPos, End: endPos}
	selRange := clampSelectionRange(fullRange, Range{Start: startPos, End: nameEndPos})

	symbol := DocumentSymbol{
		Name:           comp.Name,
		Detail:         detail,
		Kind:           SymbolKindFunction,
		Range:          fullRange,
		SelectionRange: selRange,
	}

	// Add child symbols (let bindings, elements with IDs)
	for _, node := range comp.Body {
		if child := nodeToSymbol(node); child != nil {
			symbol.Children = append(symbol.Children, *child)
		}
	}

	return symbol
}

// funcToSymbol converts a Go function to a DocumentSymbol.
func funcToSymbol(fn *tuigen.GoFunc, content string) DocumentSymbol {
	name := extractFuncName(fn.Code)

	startPos := Position{
		Line:      fn.Position.Line - 1,
		Character: fn.Position.Column - 1,
	}

	// Find end of function by searching for matching close brace
	endPos := startPos
	offset := PositionToOffset(content, startPos)
	if offset < len(content) {
		braceCount := 0
		started := false
		for i := offset; i < len(content); i++ {
			if content[i] == '{' {
				braceCount++
				started = true
			} else if content[i] == '}' {
				braceCount--
				if started && braceCount == 0 {
					endPos = offsetToPosition(content, i+1)
					break
				}
			}
		}
	}

	fullRange := Range{Start: startPos, End: endPos}
	selRange := clampSelectionRange(fullRange, Range{
		Start: startPos,
		End:   Position{Line: startPos.Line, Character: startPos.Character + len("func") + 1 + len(name)},
	})

	return DocumentSymbol{
		Name:           name,
		Detail:         "func",
		Kind:           SymbolKindFunction,
		Range:          fullRange,
		SelectionRange: selRange,
	}
}

// extractFuncName extracts the function name from Go function code.
func extractFuncName(code string) string {
	code = strings.TrimPrefix(strings.TrimSpace(code), "func ")

	// Handle methods: (receiver) Name(...)
	// Skip past the receiver before looking for the name.
	if strings.HasPrefix(code, "(") {
		closeIdx := strings.Index(code, ")")
		if closeIdx != -1 {
			code = strings.TrimSpace(code[closeIdx+1:])
		}
	}

	before, _, ok := strings.Cut(code, "(")
	if !ok {
		return "unknown"
	}

	name := strings.TrimSpace(before)
	if name == "" {
		return "unknown"
	}

	return name
}

// nodeToSymbol converts an AST node to a DocumentSymbol if applicable.
func nodeToSymbol(node tuigen.Node) *DocumentSymbol {
	switch n := node.(type) {
	case *tuigen.LetBinding:
		nameOffset := letBindingNameOffset(n)
		fullRange := tuigenPosToRange(n.Position, len(n.Name)+nameOffset)
		selRange := clampSelectionRange(fullRange, Range{
			Start: Position{Line: n.Position.Line - 1, Character: n.Position.Column - 1 + nameOffset},
			End:   Position{Line: n.Position.Line - 1, Character: n.Position.Column - 1 + nameOffset + len(n.Name)},
		})
		return &DocumentSymbol{
			Name:           n.Name,
			Detail:         "let binding",
			Kind:           SymbolKindVariable,
			Range:          fullRange,
			SelectionRange: selRange,
		}
	case *tuigen.Element:
		// Only create symbol for elements with an ID attribute
		for _, attr := range n.Attributes {
			if attr.Name == "id" {
				if str, ok := attr.Value.(*tuigen.StringLit); ok {
					fullRange := tuigenPosToRange(n.Position, len(n.Tag)+2) // "<tag>"
					selRange := clampSelectionRange(fullRange, Range{
						Start: Position{Line: attr.Position.Line - 1, Character: attr.Position.Column - 1},
						End:   Position{Line: attr.Position.Line - 1, Character: attr.Position.Column - 1 + len(attr.Name) + 2 + len(str.Value)},
					})
					return &DocumentSymbol{
						Name:           str.Value,
						Detail:         fmt.Sprintf("<%s>", n.Tag),
						Kind:           SymbolKindField,
						Range:          fullRange,
						SelectionRange: selRange,
					}
				}
			}
		}
	}
	return nil
}

// --- Workspace symbol provider ---

// workspaceSymbolProvider implements WorkspaceSymbolProvider.
type workspaceSymbolProvider struct {
	index ComponentIndex
}

// NewWorkspaceSymbolProvider creates a new workspace symbol provider.
func NewWorkspaceSymbolProvider(index ComponentIndex) WorkspaceSymbolProvider {
	return &workspaceSymbolProvider{index: index}
}

func (w *workspaceSymbolProvider) WorkspaceSymbols(query string) ([]SymbolInformation, error) {
	log.Server("WorkspaceSymbol provider: query=%q", query)

	q := strings.ToLower(query)
	var symbols []SymbolInformation

	// Search all indexed components
	for _, name := range w.index.All() {
		if q == "" || strings.Contains(strings.ToLower(name), q) {
			info, ok := w.index.Lookup(name)
			if ok && info != nil {
				symbols = append(symbols, SymbolInformation{
					Name:     name,
					Kind:     SymbolKindFunction,
					Location: info.Location,
				})
			}
		}
	}

	// Search all indexed functions
	for _, name := range w.index.AllFunctions() {
		if q == "" || strings.Contains(strings.ToLower(name), q) {
			info, ok := w.index.LookupFunc(name)
			if ok && info != nil {
				symbols = append(symbols, SymbolInformation{
					Name:     name,
					Kind:     SymbolKindFunction,
					Location: info.Location,
				})
			}
		}
	}

	return symbols, nil
}

// --- Range helpers ---

func findComponentEnd(comp *tuigen.Component, content string) Position {
	startOffset := PositionToOffset(content, Position{
		Line:      comp.Position.Line - 1,
		Character: comp.Position.Column - 1,
	})

	braceCount := 0
	started := false
	for i := startOffset; i < len(content); i++ {
		switch content[i] {
		case '{':
			braceCount++
			started = true
		case '}':
			braceCount--
			if started && braceCount == 0 {
				return offsetToPosition(content, i+1)
			}
		}
	}

	// Fallback
	return Position{Line: comp.Position.Line - 1 + 5, Character: 0}
}

// positionBefore returns true if a is before b.
func positionBefore(a, b Position) bool {
	if a.Line != b.Line {
		return a.Line < b.Line
	}
	return a.Character < b.Character
}

// positionAfter returns true if a is after b.
func positionAfter(a, b Position) bool {
	if a.Line != b.Line {
		return a.Line > b.Line
	}
	return a.Character > b.Character
}

// clampSelectionRange ensures selectionRange is contained within fullRange.
func clampSelectionRange(fullRange, selectionRange Range) Range {
	if positionBefore(selectionRange.Start, fullRange.Start) {
		selectionRange.Start = fullRange.Start
	}
	if positionAfter(selectionRange.End, fullRange.End) {
		selectionRange.End = fullRange.End
	}
	if positionAfter(selectionRange.Start, selectionRange.End) {
		selectionRange.Start = selectionRange.End
	}
	return selectionRange
}

// tuigenPosToRange converts a tuigen.Position (1-indexed) to a provider.Range (0-indexed).
func tuigenPosToRange(pos tuigen.Position, length int) Range {
	return Range{
		Start: Position{Line: pos.Line - 1, Character: pos.Column - 1},
		End:   Position{Line: pos.Line - 1, Character: pos.Column - 1 + length},
	}
}

// offsetToPosition converts a byte offset to a 0-indexed Position.
func offsetToPosition(content string, offset int) Position {
	line := 0
	col := 0
	for i := 0; i < offset && i < len(content); i++ {
		if content[i] == '\n' {
			line++
			col = 0
		} else {
			col++
		}
	}
	return Position{Line: line, Character: col}
}