gitstack

grindlemire/go-tui code browser

9.7 KB Go 357 lines 2026-02-07 · de2aea1 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
// Package provider implements LSP feature providers that the router dispatches to.
// Each provider handles a specific LSP capability (hover, definition, references, etc.)
// using the CursorContext for position resolution and the schema for language knowledge.
//
// Providers depend on abstractions (interfaces) from the parent lsp package to avoid
// circular imports. The lsp.Server injects concrete implementations when constructing
// providers.
package provider

import (
	"github.com/grindlemire/go-tui/internal/lsp/gopls"
	"github.com/grindlemire/go-tui/internal/tuigen"
)

// --- Type aliases for parent-package types ---
// These let provider code reference types without importing the parent lsp package.

// CursorContext is the resolved cursor context, mirroring lsp.CursorContext.
// This type is duplicated here to avoid circular imports between lsp and provider.
// The lsp package's router converts its CursorContext to this type before dispatch.
// Changes to either CursorContext must be reflected in both packages.
type CursorContext struct {
	Document *Document
	Position Position
	Offset   int

	Node        tuigen.Node
	NodeKind    NodeKind
	Scope       *Scope
	ParentChain []tuigen.Node

	Word        string
	Line        string
	InGoExpr    bool
	InClassAttr bool
	InElement   bool

	AttrTag  string
	AttrName string

	ImportPath string
}

// NodeKind classifies the cursor position. Mirrors lsp.NodeKind — both enums
// must stay in sync. See CursorContext comment above for the duplication rationale.
type NodeKind int

const (
	NodeKindUnknown NodeKind = iota
	NodeKindComponent
	NodeKindElement
	NodeKindAttribute
	NodeKindRefAttr // Cursor on ref={} attribute value
	NodeKindGoExpr
	NodeKindForLoop
	NodeKindIfStmt
	NodeKindLetBinding
	NodeKindStateDecl
	NodeKindStateAccess
	NodeKindParameter
	NodeKindFunction
	NodeKindGoDecl // Top-level var, type, or const declaration
	NodeKindComponentCall
	NodeKindEventHandler
	NodeKindText
	NodeKindKeyword
	NodeKindTailwindClass
	NodeKindImportPath
)

// String returns a human-readable name for the NodeKind.
func (k NodeKind) String() string {
	switch k {
	case NodeKindComponent:
		return "Component"
	case NodeKindElement:
		return "Element"
	case NodeKindAttribute:
		return "Attribute"
	case NodeKindRefAttr:
		return "RefAttr"
	case NodeKindGoExpr:
		return "GoExpr"
	case NodeKindForLoop:
		return "ForLoop"
	case NodeKindIfStmt:
		return "IfStmt"
	case NodeKindLetBinding:
		return "LetBinding"
	case NodeKindStateDecl:
		return "StateDecl"
	case NodeKindStateAccess:
		return "StateAccess"
	case NodeKindParameter:
		return "Parameter"
	case NodeKindFunction:
		return "Function"
	case NodeKindGoDecl:
		return "GoDecl"
	case NodeKindComponentCall:
		return "ComponentCall"
	case NodeKindEventHandler:
		return "EventHandler"
	case NodeKindText:
		return "Text"
	case NodeKindKeyword:
		return "Keyword"
	case NodeKindTailwindClass:
		return "TailwindClass"
	case NodeKindImportPath:
		return "ImportPath"
	default:
		return "Unknown"
	}
}

// Scope holds enclosing scope information. Mirrors lsp.Scope.
type Scope struct {
	Component *tuigen.Component
	Function  *tuigen.GoFunc
	ForLoop   *tuigen.ForLoop
	IfStmt    *tuigen.IfStmt
	Refs      []tuigen.RefInfo
	StateVars []tuigen.StateVar
	LetBinds  []*tuigen.LetBinding
	Params    []*tuigen.Param
}

// Document represents an open document in the editor.
type Document struct {
	URI     string
	Content string
	Version int
	AST     *tuigen.File
	Errors  []*tuigen.Error
}

// --- LSP protocol types ---

// Position is a 0-indexed line and character in a document.
type Position struct {
	Line      int `json:"line"`
	Character int `json:"character"`
}

// Range is a span in a document.
type Range struct {
	Start Position `json:"start"`
	End   Position `json:"end"`
}

// Location is a position in a specific document.
type Location struct {
	URI   string `json:"uri"`
	Range Range  `json:"range"`
}

// Hover is the result of a hover request.
type Hover struct {
	Contents MarkupContent `json:"contents"`
	Range    *Range        `json:"range,omitempty"`
}

// MarkupContent represents markup content.
type MarkupContent struct {
	Kind  string `json:"kind"`
	Value string `json:"value"`
}

// --- Dependency interfaces ---
// These let providers access server state without importing the lsp package.

// ComponentIndex provides lookup of workspace-wide component and function definitions.
type ComponentIndex interface {
	Lookup(name string) (*ComponentInfo, bool)
	LookupFunc(name string) (*FuncInfo, bool)
	LookupParam(componentName, paramName string) (*ParamInfo, bool)
	LookupFuncParam(funcName, paramName string) (*FuncParamInfo, bool)
	All() []string
	AllFunctions() []string
}

// ComponentInfo stores information about a component.
type ComponentInfo struct {
	Name     string
	Location Location
	Params   []*tuigen.Param
}

// FuncInfo stores information about a function.
type FuncInfo struct {
	Name      string
	Location  Location
	Signature string
	Returns   string
}

// ParamInfo stores information about a component parameter.
type ParamInfo struct {
	Name          string
	Type          string
	ComponentName string
	Location      Location
}

// FuncParamInfo stores information about a function parameter.
type FuncParamInfo struct {
	Name     string
	Type     string
	FuncName string
	Location Location
}

// GoplsProxyAccessor provides access to the gopls proxy.
type GoplsProxyAccessor interface {
	GetProxy() *gopls.GoplsProxy
}

// VirtualFileAccessor provides access to the virtual file cache.
type VirtualFileAccessor interface {
	GetVirtualFile(uri string) *gopls.CachedVirtualFile
}

// DocumentAccessor provides access to open documents and workspace ASTs.
type DocumentAccessor interface {
	GetDocument(uri string) *Document
	AllDocuments() []*Document
}

// WorkspaceASTAccessor provides access to cached workspace ASTs.
type WorkspaceASTAccessor interface {
	GetWorkspaceAST(uri string) *tuigen.File
	AllWorkspaceASTs() map[string]*tuigen.File
}

// --- Provider interfaces ---

// HoverProvider produces hover documentation.
type HoverProvider interface {
	Hover(ctx *CursorContext) (*Hover, error)
}

// DefinitionProvider resolves go-to-definition.
type DefinitionProvider interface {
	Definition(ctx *CursorContext) ([]Location, error)
}

// ReferencesProvider finds all references to a symbol.
type ReferencesProvider interface {
	References(ctx *CursorContext, includeDecl bool) ([]Location, error)
}

// CompletionProvider produces completion suggestions.
type CompletionProvider interface {
	Complete(ctx *CursorContext) (*CompletionList, error)
}

// DocumentSymbolProvider returns the symbol hierarchy for a document.
type DocumentSymbolProvider interface {
	DocumentSymbols(doc *Document) ([]DocumentSymbol, error)
}

// WorkspaceSymbolProvider searches for symbols across the workspace.
type WorkspaceSymbolProvider interface {
	WorkspaceSymbols(query string) ([]SymbolInformation, error)
}

// DiagnosticsProvider produces diagnostics for a document.
type DiagnosticsProvider interface {
	Diagnose(doc *Document) ([]Diagnostic, error)
}

// FormattingProvider formats a document.
type FormattingProvider interface {
	Format(doc *Document, opts FormattingOptions) ([]TextEdit, error)
}

// SemanticTokensProvider produces semantic tokens for syntax highlighting.
type SemanticTokensProvider interface {
	SemanticTokensFull(doc *Document) (*SemanticTokens, error)
}

// --- Symbol types ---

// DocumentSymbol represents a symbol found in a document.
type DocumentSymbol struct {
	Name           string           `json:"name"`
	Detail         string           `json:"detail,omitempty"`
	Kind           SymbolKind       `json:"kind"`
	Range          Range            `json:"range"`
	SelectionRange Range            `json:"selectionRange"`
	Children       []DocumentSymbol `json:"children,omitempty"`
}

// SymbolInformation represents a symbol for workspace symbols.
type SymbolInformation struct {
	Name          string     `json:"name"`
	Kind          SymbolKind `json:"kind"`
	Location      Location   `json:"location"`
	ContainerName string     `json:"containerName,omitempty"`
}

// SymbolKind represents the kind of symbol.
type SymbolKind int

const (
	SymbolKindFile          SymbolKind = 1
	SymbolKindModule        SymbolKind = 2
	SymbolKindNamespace     SymbolKind = 3
	SymbolKindPackage       SymbolKind = 4
	SymbolKindClass         SymbolKind = 5
	SymbolKindMethod        SymbolKind = 6
	SymbolKindProperty      SymbolKind = 7
	SymbolKindField         SymbolKind = 8
	SymbolKindConstructor   SymbolKind = 9
	SymbolKindEnum          SymbolKind = 10
	SymbolKindInterface     SymbolKind = 11
	SymbolKindFunction      SymbolKind = 12
	SymbolKindVariable      SymbolKind = 13
	SymbolKindConstant      SymbolKind = 14
	SymbolKindString        SymbolKind = 15
	SymbolKindNumber        SymbolKind = 16
	SymbolKindBoolean       SymbolKind = 17
	SymbolKindArray         SymbolKind = 18
	SymbolKindObject        SymbolKind = 19
	SymbolKindKey           SymbolKind = 20
	SymbolKindNull          SymbolKind = 21
	SymbolKindEnumMember    SymbolKind = 22
	SymbolKindStruct        SymbolKind = 23
	SymbolKindEvent         SymbolKind = 24
	SymbolKindOperator      SymbolKind = 25
	SymbolKindTypeParameter SymbolKind = 26
)

// --- Helper functions ---

// PositionToOffset converts a 0-indexed line/character to a byte offset in the content.
func PositionToOffset(content string, pos Position) int {
	line := 0
	for i, ch := range content {
		if line == pos.Line {
			return i + pos.Character
		}
		if ch == '\n' {
			line++
		}
	}
	if line == pos.Line {
		return len(content) + pos.Character
	}
	return len(content)
}

// IsWordChar returns true if c is a valid identifier character.
func IsWordChar(c byte) bool {
	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'
}