gitstack

grindlemire/go-tui code browser

6.6 KB Go 261 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
package provider

import (
	"strings"

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

// --- Shared AST traversal helpers ---

// letBindingNameOffset returns the column offset from Position to the variable name
// for a LetBinding. For := short form, Position is already at the name (offset 0).
// For var, offset is 4 for "var ".
func letBindingNameOffset(b *tuigen.LetBinding) int {
	if b.IsShortForm {
		return 0 // Position is at the variable name
	}
	if b.IsVarForm {
		return len("var ") // Position is at "var"
	}
	panic("letBindingNameOffset: unrecognised LetBinding variant (neither IsShortForm nor IsVarForm)")
}

// findLetBindingInNodes finds a let binding by name in AST nodes.
func findLetBindingInNodes(nodes []tuigen.Node, name string) *tuigen.LetBinding {
	for _, node := range nodes {
		switch n := node.(type) {
		case *tuigen.LetBinding:
			if n != nil && n.Name == name {
				return n
			}
		case *tuigen.Element:
			if n != nil {
				if found := findLetBindingInNodes(n.Children, name); found != nil {
					return found
				}
			}
		case *tuigen.ForLoop:
			if n != nil {
				if found := findLetBindingInNodes(n.Body, name); found != nil {
					return found
				}
			}
		case *tuigen.IfStmt:
			if n != nil {
				if found := findLetBindingInNodes(n.Then, name); found != nil {
					return found
				}
				if found := findLetBindingInNodes(n.Else, name); found != nil {
					return found
				}
			}
		case *tuigen.ComponentCall:
			if n != nil {
				if found := findLetBindingInNodes(n.Children, name); found != nil {
					return found
				}
			}
		}
	}
	return nil
}

// findForLoopWithVariable finds a for loop that declares the given variable.
func findForLoopWithVariable(nodes []tuigen.Node, varName string) *tuigen.ForLoop {
	for _, node := range nodes {
		switch n := node.(type) {
		case *tuigen.ForLoop:
			if n != nil && (n.Index == varName || n.Value == varName) {
				return n
			}
			if n != nil {
				if found := findForLoopWithVariable(n.Body, varName); found != nil {
					return found
				}
			}
		case *tuigen.Element:
			if n != nil {
				if found := findForLoopWithVariable(n.Children, varName); found != nil {
					return found
				}
			}
		case *tuigen.IfStmt:
			if n != nil {
				if found := findForLoopWithVariable(n.Then, varName); found != nil {
					return found
				}
				if found := findForLoopWithVariable(n.Else, varName); found != nil {
					return found
				}
			}
		case *tuigen.ComponentCall:
			if n != nil {
				if found := findForLoopWithVariable(n.Children, varName); found != nil {
					return found
				}
			}
		}
	}
	return nil
}

// findGoCodeWithVariable finds a GoCode node that declares the given variable.
func findGoCodeWithVariable(nodes []tuigen.Node, varName string) *tuigen.GoCode {
	for _, node := range nodes {
		switch n := node.(type) {
		case *tuigen.GoCode:
			if n != nil && containsVarDecl(n.Code, varName) {
				return n
			}
		case *tuigen.Element:
			if n != nil {
				if found := findGoCodeWithVariable(n.Children, varName); found != nil {
					return found
				}
			}
		case *tuigen.ForLoop:
			if n != nil {
				if found := findGoCodeWithVariable(n.Body, varName); found != nil {
					return found
				}
			}
		case *tuigen.IfStmt:
			if n != nil {
				if found := findGoCodeWithVariable(n.Then, varName); found != nil {
					return found
				}
				if found := findGoCodeWithVariable(n.Else, varName); found != nil {
					return found
				}
			}
		case *tuigen.ComponentCall:
			if n != nil {
				if found := findGoCodeWithVariable(n.Children, varName); found != nil {
					return found
				}
			}
		case *tuigen.LetBinding:
			if n != nil && n.Element != nil {
				if found := findGoCodeWithVariable(n.Element.Children, varName); found != nil {
					return found
				}
			}
		}
	}
	return nil
}

// containsVarDecl checks if code declares the given variable.
// Uses exact identifier matching (not substring) for each declared name.
func containsVarDecl(code, varName string) bool {
	if idx := strings.Index(code, ":="); idx > 0 {
		lhs := code[:idx]
		parts := strings.SplitSeq(lhs, ",")
		for part := range parts {
			if strings.TrimSpace(part) == varName {
				return true
			}
		}
	}

	if after, ok := strings.CutPrefix(strings.TrimSpace(code), "var "); ok {
		rest := after
		if idx := strings.Index(rest, "="); idx > 0 {
			lhs := rest[:idx]
			parts := strings.SplitSeq(lhs, ",")
			for part := range parts {
				part = strings.TrimSpace(part)
				fields := strings.Fields(part)
				if len(fields) > 0 && fields[0] == varName {
					return true
				}
			}
		}
	}

	return false
}

// isWordBoundary checks if position i in s is at a word boundary for the given word length.
func isWordBoundary(s string, i, wordLen int) bool {
	before := i == 0 || !IsWordChar(s[i-1])
	after := i+wordLen >= len(s) || !IsWordChar(s[i+wordLen])
	return before && after
}

// findVarDeclPosition finds the position of a variable declaration in code.
// Uses word-boundary-aware search so "count" doesn't match inside "accountCount".
func findVarDeclPosition(code, varName string) int {
	if idx := strings.Index(code, ":="); idx > 0 {
		lhs := code[:idx]
		parts := strings.Split(lhs, ",")
		pos := 0
		for _, part := range parts {
			trimmed := strings.TrimSpace(part)
			if trimmed == varName {
				partStart := strings.Index(lhs[pos:], trimmed)
				if partStart >= 0 {
					return pos + partStart
				}
			}
			pos += len(part) + 1
		}
	}

	if strings.HasPrefix(strings.TrimSpace(code), "var ") {
		varIdx := strings.Index(code, "var ")
		rest := code[varIdx+4:]
		if idx := strings.Index(rest, "="); idx > 0 {
			lhs := rest[:idx]
			// Use word-boundary search within the LHS
			offset := indexWholeWordIn(lhs, varName)
			if offset >= 0 {
				return varIdx + 4 + offset
			}
		}
	}

	return -1
}

// indexWholeWordIn finds the first whole-word occurrence of word in s.
func indexWholeWordIn(s, word string) int {
	idx := 0
	for {
		i := strings.Index(s[idx:], word)
		if i < 0 {
			return -1
		}
		absIdx := idx + i
		if isWordBoundary(s, absIdx, len(word)) {
			return absIdx
		}
		idx = absIdx + len(word)
	}
}

// parseFuncName extracts the function name from a Go function definition.
// Handles both plain functions ("func Name(") and methods ("func (r *T) Name(").
func parseFuncName(code string) string {
	_, after, ok := strings.Cut(code, "func ")
	if !ok {
		return ""
	}
	rest := strings.TrimSpace(after)

	// Skip receiver type: func (r *Receiver) Name(...)
	if len(rest) > 0 && rest[0] == '(' {
		closeIdx := strings.Index(rest, ")")
		if closeIdx < 0 {
			return ""
		}
		rest = strings.TrimSpace(rest[closeIdx+1:])
	}

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