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
import (
"github.com/grindlemire/go-tui/internal/lsp/gopls"
"github.com/grindlemire/go-tui/internal/tuigen"
)
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
}
type NodeKind int
const (
NodeKindUnknown NodeKind = iota
NodeKindComponent
NodeKindElement
NodeKindAttribute
NodeKindRefAttr
NodeKindGoExpr
NodeKindForLoop
NodeKindIfStmt
NodeKindLetBinding
NodeKindStateDecl
NodeKindStateAccess
NodeKindParameter
NodeKindFunction
NodeKindGoDecl
NodeKindComponentCall
NodeKindEventHandler
NodeKindText
NodeKindKeyword
NodeKindTailwindClass
NodeKindImportPath
)
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"
}
}
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
}
type Document struct {
URI string
Content string
Version int
AST *tuigen.File
Errors []*tuigen.Error
}
type Position struct {
Line int `json:"line"`
Character int `json:"character"`
}
type Range struct {
Start Position `json:"start"`
End Position `json:"end"`
}
type Location struct {
URI string `json:"uri"`
Range Range `json:"range"`
}
type Hover struct {
Contents MarkupContent `json:"contents"`
Range *Range `json:"range,omitempty"`
}
type MarkupContent struct {
Kind string `json:"kind"`
Value string `json:"value"`
}
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
}
type ComponentInfo struct {
Name string
Location Location
Params []*tuigen.Param
}
type FuncInfo struct {
Name string
Location Location
Signature string
Returns string
}
type ParamInfo struct {
Name string
Type string
ComponentName string
Location Location
}
type FuncParamInfo struct {
Name string
Type string
FuncName string
Location Location
}
type GoplsProxyAccessor interface {
GetProxy() *gopls.GoplsProxy
}
type VirtualFileAccessor interface {
GetVirtualFile(uri string) *gopls.CachedVirtualFile
}
type DocumentAccessor interface {
GetDocument(uri string) *Document
AllDocuments() []*Document
}
type WorkspaceASTAccessor interface {
GetWorkspaceAST(uri string) *tuigen.File
AllWorkspaceASTs() map[string]*tuigen.File
}
type HoverProvider interface {
Hover(ctx *CursorContext) (*Hover, error)
}
type DefinitionProvider interface {
Definition(ctx *CursorContext) ([]Location, error)
}
type ReferencesProvider interface {
References(ctx *CursorContext, includeDecl bool) ([]Location, error)
}
type CompletionProvider interface {
Complete(ctx *CursorContext) (*CompletionList, error)
}
type DocumentSymbolProvider interface {
DocumentSymbols(doc *Document) ([]DocumentSymbol, error)
}
type WorkspaceSymbolProvider interface {
WorkspaceSymbols(query string) ([]SymbolInformation, error)
}
type DiagnosticsProvider interface {
Diagnose(doc *Document) ([]Diagnostic, error)
}
type FormattingProvider interface {
Format(doc *Document, opts FormattingOptions) ([]TextEdit, error)
}
type SemanticTokensProvider interface {
SemanticTokensFull(doc *Document) (*SemanticTokens, error)
}
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"`
}
type SymbolInformation struct {
Name string `json:"name"`
Kind SymbolKind `json:"kind"`
Location Location `json:"location"`
ContainerName string `json:"containerName,omitempty"`
}
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
)
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)
}
func IsWordChar(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'
}
|