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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
package lsp
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"github.com/grindlemire/go-tui/internal/lsp/log"
"github.com/grindlemire/go-tui/internal/tuigen"
)
type InitializeParams struct {
ProcessID *int `json:"processId"`
RootURI string `json:"rootUri"`
RootPath string `json:"rootPath"`
Capabilities ClientCapabilities `json:"capabilities"`
InitializationOptions json.RawMessage `json:"initializationOptions,omitempty"`
}
type ClientCapabilities struct {
TextDocument TextDocumentClientCapabilities `json:"textDocument"`
}
type TextDocumentClientCapabilities struct {
Synchronization *SynchronizationCapabilities `json:"synchronization,omitempty"`
Completion *CompletionCapabilities `json:"completion,omitempty"`
Hover *HoverCapabilities `json:"hover,omitempty"`
Definition *DefinitionCapabilities `json:"definition,omitempty"`
PublishDiagnostics *PublishDiagnostics `json:"publishDiagnostics,omitempty"`
}
type SynchronizationCapabilities struct {
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
WillSave bool `json:"willSave,omitempty"`
WillSaveWaitUntil bool `json:"willSaveWaitUntil,omitempty"`
DidSave bool `json:"didSave,omitempty"`
}
type CompletionCapabilities struct {
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
type HoverCapabilities struct {
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
type DefinitionCapabilities struct {
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
type PublishDiagnostics struct {
RelatedInformation bool `json:"relatedInformation,omitempty"`
}
type InitializeResult struct {
Capabilities ServerCapabilities `json:"capabilities"`
}
type ServerCapabilities struct {
TextDocumentSync *TextDocumentSyncOptions `json:"textDocumentSync,omitempty"`
CompletionProvider *CompletionOptions `json:"completionProvider,omitempty"`
HoverProvider bool `json:"hoverProvider,omitempty"`
DefinitionProvider bool `json:"definitionProvider,omitempty"`
ReferencesProvider bool `json:"referencesProvider,omitempty"`
DocumentSymbolProvider bool `json:"documentSymbolProvider,omitempty"`
WorkspaceSymbolProvider bool `json:"workspaceSymbolProvider,omitempty"`
DocumentFormattingProvider bool `json:"documentFormattingProvider,omitempty"`
SemanticTokensProvider *SemanticTokensOptions `json:"semanticTokensProvider,omitempty"`
}
type SemanticTokensOptions struct {
Legend SemanticTokensLegend `json:"legend"`
Full bool `json:"full"`
}
type SemanticTokensLegend struct {
TokenTypes []string `json:"tokenTypes"`
TokenModifiers []string `json:"tokenModifiers"`
}
type TextDocumentSyncOptions struct {
OpenClose bool `json:"openClose"`
Change TextDocumentSyncKind `json:"change"`
Save *SaveOptions `json:"save,omitempty"`
}
type TextDocumentSyncKind int
const (
TextDocumentSyncKindNone TextDocumentSyncKind = 0
TextDocumentSyncKindFull TextDocumentSyncKind = 1
TextDocumentSyncKindIncremental TextDocumentSyncKind = 2
)
type SaveOptions struct {
IncludeText bool `json:"includeText,omitempty"`
}
type CompletionOptions struct {
TriggerCharacters []string `json:"triggerCharacters,omitempty"`
ResolveProvider bool `json:"resolveProvider,omitempty"`
}
func (s *Server) handleInitialize(params json.RawMessage) (any, *Error) {
var p InitializeParams
if err := json.Unmarshal(params, &p); err != nil {
return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
}
s.rootURI = p.RootURI
log.Server("Initialize with root: %s", s.rootURI)
result := InitializeResult{
Capabilities: ServerCapabilities{
TextDocumentSync: &TextDocumentSyncOptions{
OpenClose: true,
Change: TextDocumentSyncKindFull,
Save: &SaveOptions{
IncludeText: true,
},
},
CompletionProvider: &CompletionOptions{
TriggerCharacters: []string{"@", "<", "{", "."},
},
HoverProvider: true,
DefinitionProvider: true,
ReferencesProvider: true,
DocumentSymbolProvider: true,
WorkspaceSymbolProvider: true,
DocumentFormattingProvider: true,
SemanticTokensProvider: &SemanticTokensOptions{
Legend: SemanticTokensLegend{
TokenTypes: []string{
"namespace",
"type",
"class",
"function",
"parameter",
"variable",
"property",
"keyword",
"string",
"number",
"operator",
"decorator",
"regexp",
"comment",
"label",
"typeParameter",
},
TokenModifiers: []string{
"declaration",
"definition",
"readonly",
"modification",
"defaultLibrary",
},
},
Full: true,
},
},
}
return result, nil
}
func (s *Server) handleInitialized() (any, *Error) {
s.initialized = true
log.Server("Server initialized")
go s.indexWorkspace()
go s.InitGopls()
return nil, nil
}
func (s *Server) handleShutdown() (any, *Error) {
log.Server("Shutdown requested")
s.shutdown = true
s.ShutdownGopls()
return nil, nil
}
func (s *Server) handleExit() {
log.Server("Exit requested")
}
type DidOpenParams struct {
TextDocument TextDocumentItem `json:"textDocument"`
}
type TextDocumentItem struct {
URI string `json:"uri"`
LanguageID string `json:"languageId"`
Version int `json:"version"`
Text string `json:"text"`
}
func (s *Server) handleDidOpen(params json.RawMessage) (any, *Error) {
var p DidOpenParams
if err := json.Unmarshal(params, &p); err != nil {
return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
}
log.Server("Document opened: %s", p.TextDocument.URI)
doc := s.docs.Open(p.TextDocument.URI, p.TextDocument.Text, p.TextDocument.Version)
s.index.IndexDocument(p.TextDocument.URI, doc.AST)
s.workspaceASTsMu.Lock()
delete(s.workspaceASTs, p.TextDocument.URI)
s.workspaceASTsMu.Unlock()
s.UpdateVirtualFile(doc)
s.publishDiagnostics(doc)
return nil, nil
}
type DidChangeParams struct {
TextDocument VersionedTextDocumentIdentifier `json:"textDocument"`
ContentChanges []TextDocumentContentChangeEvent `json:"contentChanges"`
}
type VersionedTextDocumentIdentifier struct {
URI string `json:"uri"`
Version int `json:"version"`
}
type TextDocumentContentChangeEvent struct {
Text string `json:"text"`
}
func (s *Server) handleDidChange(params json.RawMessage) (any, *Error) {
var p DidChangeParams
if err := json.Unmarshal(params, &p); err != nil {
return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
}
log.Server("Document changed: %s", p.TextDocument.URI)
if len(p.ContentChanges) == 0 {
return nil, nil
}
newContent := p.ContentChanges[len(p.ContentChanges)-1].Text
doc := s.docs.Update(p.TextDocument.URI, newContent, p.TextDocument.Version)
if doc != nil {
s.index.IndexDocument(p.TextDocument.URI, doc.AST)
s.UpdateVirtualFile(doc)
s.publishDiagnostics(doc)
}
return nil, nil
}
type DidCloseParams struct {
TextDocument TextDocumentIdentifier `json:"textDocument"`
}
type TextDocumentIdentifier struct {
URI string `json:"uri"`
}
func (s *Server) handleDidClose(params json.RawMessage) (any, *Error) {
var p DidCloseParams
if err := json.Unmarshal(params, &p); err != nil {
return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
}
log.Server("Document closed: %s", p.TextDocument.URI)
doc := s.docs.Get(p.TextDocument.URI)
var ast *tuigen.File
if doc != nil {
ast = doc.AST
}
s.docs.Close(p.TextDocument.URI)
if ast != nil {
s.workspaceASTsMu.Lock()
s.workspaceASTs[p.TextDocument.URI] = ast
s.workspaceASTsMu.Unlock()
s.index.IndexDocument(p.TextDocument.URI, ast)
} else {
s.index.Remove(p.TextDocument.URI)
}
s.CloseVirtualFile(p.TextDocument.URI)
s.sendNotification("textDocument/publishDiagnostics", PublishDiagnosticsParams{
URI: p.TextDocument.URI,
Diagnostics: []Diagnostic{},
})
return nil, nil
}
type DidSaveParams struct {
TextDocument TextDocumentIdentifier `json:"textDocument"`
Text *string `json:"text,omitempty"`
}
func (s *Server) handleDidSave(params json.RawMessage) (any, *Error) {
var p DidSaveParams
if err := json.Unmarshal(params, &p); err != nil {
return nil, &Error{Code: CodeInvalidParams, Message: err.Error()}
}
log.Server("Document saved: %s", p.TextDocument.URI)
if p.Text != nil {
doc := s.docs.Get(p.TextDocument.URI)
if doc != nil {
doc = s.docs.Update(p.TextDocument.URI, *p.Text, doc.Version+1)
s.index.IndexDocument(p.TextDocument.URI, doc.AST)
s.publishDiagnostics(doc)
}
}
return nil, nil
}
func (s *Server) indexWorkspace() {
if s.rootURI == "" {
log.Server("Cannot index workspace: no rootURI")
return
}
rootPath := strings.TrimPrefix(s.rootURI, "file://")
log.Server("=== WORKSPACE INDEXING START ===")
log.Server("rootURI: %s", s.rootURI)
log.Server("rootPath: %s", rootPath)
count := 0
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
name := info.Name()
if strings.HasPrefix(name, ".") || name == "node_modules" || name == "vendor" {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".gsx") {
return nil
}
uri := "file://" + path
if s.docs.Get(uri) != nil {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
log.Server("Failed to read %s: %v", path, err)
return nil
}
lexer := tuigen.NewLexer(path, string(content))
parser := tuigen.NewParser(lexer)
ast, err := parser.ParseFile()
if err != nil {
log.Server("Parse error in %s: %v", path, err)
}
if ast != nil {
s.index.IndexDocument(uri, ast)
s.workspaceASTsMu.Lock()
s.workspaceASTs[uri] = ast
s.workspaceASTsMu.Unlock()
count++
log.Server("Indexed %s: %d components, %d functions",
path, len(ast.Components), len(ast.Funcs))
}
return nil
})
if err != nil {
log.Server("Error walking workspace: %v", err)
}
log.Server("Workspace indexing complete: %d files indexed", count)
log.Server("All indexed functions: %v", s.index.AllFunctions())
log.Server("All indexed components: %v", s.index.All())
log.Server("=== WORKSPACE INDEXING END ===")
}
|