-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
425 lines (387 loc) · 15.1 KB
/
server.go
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
package server
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"sync"
"github.com/huderlem/poryscript-pls/config"
"github.com/huderlem/poryscript-pls/lsp"
"github.com/huderlem/poryscript-pls/parse"
"github.com/huderlem/poryscript/lexer"
"github.com/huderlem/poryscript/parser"
"github.com/huderlem/poryscript/token"
"github.com/sourcegraph/jsonrpc2"
)
type LspServer interface {
Run()
}
func New() LspServer {
server := poryscriptServer{
config: config.New(),
cachedDocuments: map[string]string{},
cachedCommands: map[string]map[string]parse.Command{},
cachedConstants: map[string]map[string]parse.ConstantSymbol{},
cachedSymbols: map[string]map[string]parse.Symbol{},
cachedMiscTokens: map[string]map[string]parse.MiscToken{},
}
// Wrap with AsyncHandler to allow for calling client requests in the middle of
// handling a request. Otherwise, a channel deadlock will occur and cause a panic.
handler := jsonrpc2.AsyncHandler(jsonrpc2.HandlerWithError(server.handle))
server.connection = jsonrpc2.NewConn(context.Background(), jsonrpc2.NewBufferedStream(StdioRWC{}, jsonrpc2.VSCodeObjectCodec{}), handler)
return &server
}
func (server *poryscriptServer) handle(ctx context.Context, conn *jsonrpc2.Conn, request *jsonrpc2.Request) (interface{}, error) {
switch request.Method {
case "initialize":
params := lsp.InitializeParams{}
if err := json.Unmarshal(*request.Params, ¶ms); err != nil {
return nil, err
}
return server.onInitialize(ctx, params), nil
case "initialized":
return nil, server.onInitialized(ctx)
case "textDocument/completion":
params := lsp.CompletionParams{}
if err := json.Unmarshal(*request.Params, ¶ms); err != nil {
return nil, err
}
return server.onCompletion(ctx, params)
case "textDocument/definition":
params := lsp.DefinitionParams{}
if err := json.Unmarshal(*request.Params, ¶ms); err != nil {
return nil, err
}
return server.onDefinition(ctx, params)
case "textDocument/signatureHelp":
params := lsp.SignatureHelpParams{}
if err := json.Unmarshal(*request.Params, ¶ms); err != nil {
return nil, err
}
return server.onSignatureHelp(ctx, params)
case "textDocument/semanticTokens/full":
params := lsp.SemanticTokensParams{}
if err := json.Unmarshal(*request.Params, ¶ms); err != nil {
return nil, err
}
return server.onSemanticTokensFull(ctx, params)
case "textDocument/didOpen":
params := lsp.DidOpenTextDocumentParams{}
if err := json.Unmarshal(*request.Params, ¶ms); err != nil {
return nil, err
}
return nil, server.onTextDocumentDidOpen(ctx, params)
case "textDocument/didChange":
params := lsp.DidChangeTextDocumentParams{}
if err := json.Unmarshal(*request.Params, ¶ms); err != nil {
return nil, err
}
return nil, server.onTextDocumentDidChange(ctx, params)
case "workspace/didChangeConfiguration":
params := lsp.DidChangeConfigurationParams{}
if err := json.Unmarshal(*request.Params, ¶ms); err != nil {
return nil, err
}
return nil, server.onDidChangeConfiguration(ctx, params)
case "workspace/didChangeWatchedFiles":
params := lsp.DidChangeWatchedFilesParams{}
if err := json.Unmarshal(*request.Params, ¶ms); err != nil {
return nil, err
}
return nil, server.onDidChangeWatchedFiles(ctx, params)
default:
return nil, fmt.Errorf("unsupported request method '%s'", request.Method)
}
}
// poryscriptServer is the main handler for the Poryscript LSP server. It implements the
// LspServer interface.
type poryscriptServer struct {
connection *jsonrpc2.Conn
config config.Config
cachedDocuments map[string]string
cachedCommands map[string]map[string]parse.Command
cachedConstants map[string]map[string]parse.ConstantSymbol
cachedSymbols map[string]map[string]parse.Symbol
cachedMiscTokens map[string]map[string]parse.MiscToken
cachedAutovarCommands map[string]parser.CommandConfig
documentsMutex sync.Mutex
commandsMutex sync.Mutex
constantsMutex sync.Mutex
symbolsMutex sync.Mutex
miscTokensMutex sync.Mutex
commandConfigMutex sync.Mutex
}
// Runs the LSP server indefinitely.
func (s *poryscriptServer) Run() {
<-s.connection.DisconnectNotify()
}
// Handles an incoming LSP 'initialize' request.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#initialize
func (s *poryscriptServer) onInitialize(ctx context.Context, params lsp.InitializeParams) *lsp.InitializeResult {
s.config.HasConfigCapability = params.Capabilities.Workspace.Configuration
s.config.HasWorkspaceFolderCapability = params.Capabilities.Workspace.WorkspaceFolders
return &lsp.InitializeResult{
Capabilities: lsp.ServerCapabilities{
TextDocumentSync: &lsp.TextDocumentSyncOptionsOrKind{
Options: &lsp.TextDocumentSyncOptions{
OpenClose: true,
Change: lsp.TDSKFull,
},
},
CompletionProvider: &lsp.CompletionOptions{},
SignatureHelpProvider: &lsp.SignatureHelpOptions{
TriggerCharacters: []string{"(", ","},
},
SemanticTokensProvider: &lsp.SemanticTokensOptions{
Full: lsp.STPFFull,
Range: false,
Legend: lsp.SemanticTokensLegend{
TokenTypes: []string{"keyword", "function", "enumMember", "variable"},
},
},
DefinitionProvider: true,
},
}
}
// Handles an incoming LSP 'initialized' notification.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#initialized
func (s *poryscriptServer) onInitialized(ctx context.Context) error {
if s.config.HasConfigCapability {
params := lsp.RegistrationParams{
Registrations: []lsp.Registration{
{
ID: "workspace/didChangeConfiguration",
Method: "workspace/didChangeConfiguration",
},
},
}
var result interface{}
s.connection.Call(ctx, "client/registerCapability", params, &result)
}
if s.config.HasWorkspaceFolderCapability {
params := lsp.RegistrationParams{
Registrations: []lsp.Registration{
{
ID: "workspace/didChangeWorkspaceFolders",
Method: "workspace/didChangeWorkspaceFolders",
},
},
}
var result interface{}
s.connection.Call(ctx, "client/registerCapability", params, &result)
}
var filepaths []string
if err := s.connection.Call(ctx, "poryscript/getPoryscriptFiles", nil, &filepaths); err != nil {
os.Stderr.WriteString(err.Error())
}
for _, filepath := range filepaths {
if _, err := s.getSymbolsInFile(ctx, "file://"+filepath); err != nil {
os.Stderr.WriteString(err.Error())
}
}
return nil
}
// Handles an incoming LSP 'textDocument/completion' request.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
func (s *poryscriptServer) onCompletion(ctx context.Context, req lsp.CompletionParams) ([]lsp.CompletionItem, error) {
commands, _ := s.getCommands(ctx, string(req.TextDocument.URI))
constants, _ := s.getConstantsInFile(ctx, string(req.TextDocument.URI))
miscTokens, _ := s.getMiscTokens(ctx, string(req.TextDocument.URI))
s.getSymbolsInFile(ctx, string(req.TextDocument.URI))
symbols := []parse.Symbol{}
s.symbolsMutex.Lock()
for _, fileSymbols := range s.cachedSymbols {
for _, s := range fileSymbols {
symbols = append(symbols, s)
}
}
s.symbolsMutex.Unlock()
completionItems := []lsp.CompletionItem{}
for _, command := range commands {
completionItems = append(completionItems, command.ToCompletionItem())
}
for _, constant := range constants {
completionItems = append(completionItems, constant.ToCompletionItem())
}
for _, symbol := range symbols {
completionItems = append(completionItems, symbol.ToCompletionItem())
}
for _, miscToken := range miscTokens {
completionItems = append(completionItems, miscToken.ToCompletionItem())
}
return completionItems, nil
}
// Handles an incoming LSP 'textDocument/definition' request.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
func (s *poryscriptServer) onDefinition(ctx context.Context, req lsp.DefinitionParams) ([]lsp.Location, error) {
content, err := s.getDocumentContent(ctx, string(req.TextDocument.URI))
if err != nil {
return []lsp.Location{}, err
}
token := parse.GetTokenAt(content, req.Position.Line, req.Position.Character)
constants, _ := s.getConstantsInFile(ctx, string(req.TextDocument.URI))
if c, ok := constants[token]; ok {
return []lsp.Location{c.ToLocation()}, nil
}
s.getSymbolsInFile(ctx, string(req.TextDocument.URI))
symbols := map[string]parse.Symbol{}
s.symbolsMutex.Lock()
for _, fileSymbols := range s.cachedSymbols {
for _, s := range fileSymbols {
symbols[s.Name] = s
}
}
s.symbolsMutex.Unlock()
if s, ok := symbols[token]; ok {
return []lsp.Location{s.ToLocation()}, nil
}
miscTokens, _ := s.getMiscTokens(ctx, string(req.TextDocument.URI))
if t, ok := miscTokens[token]; ok {
return []lsp.Location{t.ToLocation()}, nil
}
return []lsp.Location{}, nil
}
// Handles an incoming LSP 'textDocument/signatureHelp' request.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_signatureHelp
func (s *poryscriptServer) onSignatureHelp(ctx context.Context, req lsp.SignatureHelpParams) (lsp.SignatureHelp, error) {
uri, _ := url.QueryUnescape(string(req.TextDocument.URI))
var content string
if err := s.connection.Call(ctx, "poryscript/readfs", uri, &content); err != nil {
return lsp.SignatureHelp{}, err
}
callInfo, err := parse.GetCommandCallParts(content, req.Position.Line, req.Position.Character)
if err != nil {
// TODO: log error?
return lsp.SignatureHelp{}, nil
}
commands, _ := s.getCommands(ctx, string(req.TextDocument.URI))
command, ok := commands[callInfo.Command]
if !ok || len(command.Parameters) == 0 {
return lsp.SignatureHelp{}, nil
}
if req.Position.Character < callInfo.OpenParen.Character+1 || req.Position.Character > callInfo.CloseParen.Character {
return lsp.SignatureHelp{}, nil
}
paramId := 0
for paramId < len(callInfo.Commas) && req.Position.Character > callInfo.Commas[paramId].Character {
paramId++
}
if paramId >= len(command.Parameters) && command.HasVarargParam() {
paramId = len(command.Parameters) - 1
}
return lsp.SignatureHelp{
ActiveParameter: paramId,
ActiveSignature: 0,
Signatures: []lsp.SignatureInformation{
{
Label: command.GetParamsLabel(),
Documentation: command.Documentation,
Parameters: command.GetParamInformation(),
},
},
}, nil
}
// Handles an incoming LSP 'textDocument/didOpen' request.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didOpen
func (s *poryscriptServer) onTextDocumentDidOpen(ctx context.Context, req lsp.DidOpenTextDocumentParams) error {
fileUri, _ := url.QueryUnescape(string(req.TextDocument.URI))
_, err := s.getDocumentContent(ctx, fileUri)
s.validatePoryscriptFile(ctx, fileUri)
return err
}
// Handles an incoming LSP 'textDocument/didChange' request.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didChange
func (s *poryscriptServer) onTextDocumentDidChange(ctx context.Context, req lsp.DidChangeTextDocumentParams) error {
if len(req.ContentChanges) == 0 {
return nil
}
fileUri, _ := url.QueryUnescape(string(req.TextDocument.URI))
s.clearCaches(fileUri)
s.validatePoryscriptFile(ctx, fileUri)
return nil
}
// Handles an incoming LSP 'workspace/didChangeConfiguration' request.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_didChangeConfiguration
func (s *poryscriptServer) onDidChangeConfiguration(ctx context.Context, req lsp.DidChangeConfigurationParams) error {
s.config.ClearSettings()
return nil
}
// Handles an incoming LSP 'workspace/didChangeWatchedFiles' request.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_didChangeWatchedFiles
func (s *poryscriptServer) onDidChangeWatchedFiles(ctx context.Context, req lsp.DidChangeWatchedFilesParams) error {
// TODO: this should only clear/update the cache for the actual watched files that changed.
// This approach that clears way more cached data than necessary.
s.clearWatchedFileCaches()
return nil
}
// Handles an incoming LSP 'textDocument/semanticTokens/full' request.
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_semanticTokens
func (s *poryscriptServer) onSemanticTokensFull(ctx context.Context, req lsp.SemanticTokensParams) (lsp.SemanticTokens, error) {
uri, _ := url.QueryUnescape(string(req.TextDocument.URI))
var content string
if err := s.connection.Call(ctx, "poryscript/readfs", uri, &content); err != nil {
return lsp.SemanticTokens{}, err
}
// Collect the tokens.
l := lexer.New(content)
tokens := []token.Token{}
for {
t := l.NextToken()
if t.Type == token.EOF {
break
}
tokens = append(tokens, t)
}
commands, _ := s.getCommands(ctx, string(req.TextDocument.URI))
constants, _ := s.getConstantsInFile(ctx, string(req.TextDocument.URI))
miscTokens, _ := s.getMiscTokens(ctx, string(req.TextDocument.URI))
s.getSymbolsInFile(ctx, string(req.TextDocument.URI))
symbols := map[string]parse.Symbol{}
s.symbolsMutex.Lock()
for _, fileSymbols := range s.cachedSymbols {
for _, s := range fileSymbols {
symbols[s.Name] = s
}
}
s.symbolsMutex.Unlock()
// TODO: use strongly-typed token types for AddToken(), rather than hardcoded integers
builder := lsp.SemanticTokenBuilder{}
for _, t := range tokens {
if command, ok := commands[t.Literal]; ok {
// 'switch' and 'case' are both Poryscript keywords and scripting commands.
if t.Literal != "switch" && t.Literal != "case" {
switch command.CompletionKind {
case lsp.CIKFunction:
builder.AddToken(t.LineNumber-1, t.StartUtf8CharIndex, t.EndUtf8CharIndex-t.StartUtf8CharIndex, 1, 0)
case lsp.CIKConstant:
builder.AddToken(t.LineNumber-1, t.StartUtf8CharIndex, t.EndUtf8CharIndex-t.StartUtf8CharIndex, 0, 0)
}
}
}
if constant, ok := constants[t.Literal]; ok {
if t.LineNumber-1 != constant.Position.Line {
builder.AddToken(t.LineNumber-1, t.StartUtf8CharIndex, t.EndUtf8CharIndex-t.StartUtf8CharIndex, 2, 0)
}
}
if symbol, ok := symbols[t.Literal]; ok {
switch symbol.Kind {
case parse.SymbolKindScript, parse.SymbolKindMapScripts:
builder.AddToken(t.LineNumber-1, t.StartUtf8CharIndex, t.EndUtf8CharIndex-t.StartUtf8CharIndex, 1, 0)
case parse.SymbolKindMovementScript, parse.SymbolKindMart, parse.SymbolKindText, parse.SymbolKindLabel:
builder.AddToken(t.LineNumber-1, t.StartUtf8CharIndex, t.EndUtf8CharIndex-t.StartUtf8CharIndex, 3, 0)
}
}
if miscToken, ok := miscTokens[t.Literal]; ok {
switch miscToken.Type {
case "special":
builder.AddToken(t.LineNumber-1, t.StartUtf8CharIndex, t.EndUtf8CharIndex-t.StartUtf8CharIndex, 1, 0)
case "define":
builder.AddToken(t.LineNumber-1, t.StartUtf8CharIndex, t.EndUtf8CharIndex-t.StartUtf8CharIndex, 2, 0)
default:
builder.AddToken(t.LineNumber-1, t.StartUtf8CharIndex, t.EndUtf8CharIndex-t.StartUtf8CharIndex, 0, 0)
}
}
}
return lsp.SemanticTokens{Data: builder.Build()}, nil
}