-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.red
522 lines (477 loc) · 11.7 KB
/
server.red
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
Red [
Title: "Red server for Visual Studio Code"
Author: "bitbegin"
File: %server.red
Tabs: 4
Rights: "Copyright (C) 2011-2019 Red Foundation. All rights reserved."
License: "BSD-3 - https://github.com/red/red/blob/origin/BSD-3-License.txt"
]
#include %lsp-const.red
#include %system-words.red
#include %lexer.red
#include %semantic.red
logger: none
open-logger?: false
debug-on?: false
client-caps: none
shutdown?: no
versions: []
workspace-folder: []
excluded-folder: ""
init-logger: func [_logger [file! none!]][
logger: _logger
if logger [
if exists? _logger [
txt: read _logger
write %logger.bak txt
]
delete logger ;-- in case, the file not deleted
write logger "^/"
]
]
write-newline: does [
#either config/OS = 'Windows [
write-stdout "^/"
][
write-stdout "^M^/"
]
]
write-response: function [resp][
write-stdout "Content-Length: "
write-stdout to string! length? nresp: to binary! resp
write-newline
write-stdout {Content-Type: application/vscode-jsonrpc; charset=utf-8}
write-newline write-newline
write-stdout nresp
]
write-log: function [str [string!]][
if logger [
unless empty? str [write/append logger str]
write/append logger "^/"
]
]
json-body: none
process: function [data [string!]][
script: load-json data
set 'json-body copy #()
json-body/jsonrpc: "2.0"
if script/id [
json-body/id: script/id
]
if script/method [
dispatch-method script/method script/params
]
true
]
response: function [][
resp: to-json json-body
write-response resp
write-log rejoin ["[NOW] " mold now/precise]
write-log rejoin ["[OUTPUT] Content-Length: " length? resp]
write-log resp write-log ""
]
lsp-read: function [][
len: 0
until [
header: trim input-stdin
if find header "Content-Length: " [
len: to integer! trim/all find/tail header "Content-Length: "
write-log rejoin ["[INPUT] Content-Length: " len]
]
empty? header
]
n: 0
bin: make binary! len
until [
read-stdin skip bin n len - n
n: length? bin
n = len
]
also str: to string! bin do [write-log str write-log ""]
]
dispatch-method: function [method [string!] params][
switch method [
"initialize" [on-initialize params]
"initialized" [on-initialized params]
"workspace/didChangeConfiguration" [on-didChangeConfiguration params]
"workspace/didChangeWorkspaceFolders" [on-didChangeWorkspaceFolders params]
"workspace/didChangeWatchedFiles" [on-didChangeWatchedFiles params]
"shutdown" [on-shutdown params]
"textDocument/didOpen" [on-textDocument-didOpen params]
"textDocument/didClose" [on-textDocument-didClose params]
"textDocument/didChange" [on-textDocument-didChange params]
"textDocument/didSave" [on-textDocument-didSave params]
"textDocument/completion" [on-textDocument-completion params]
"completionItem/resolve" [on-completionItem-resolve params]
"textDocument/documentSymbol" [on-textDocument-symbol params]
"textDocument/hover" [on-textDocument-hover params]
"textDocument/definition" [on-textDocument-definition params]
]
]
TextDocumentSyncKind: [
None 0
Full 1
Incremental 2
]
trigger-string: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/%.+-_=?*&~?`"
trigger-chars: []
forall trigger-string [
append trigger-chars to string! trigger-string/1
]
on-initialize: function [params [map!]][
set 'client-caps params
if params/initializationOptions [
set 'excluded-folder params/initializationOptions/excludedPath
]
if ws: params/workspaceFolders [
forall ws [
append workspace-folder ws/1/uri
]
]
caps: copy #()
put caps 'textDocumentSync TextDocumentSyncKind/Incremental
put caps 'hoverProvider true
put caps 'completionProvider
make map! reduce [
'resolveProvider true
'triggerCharacters trigger-chars
]
put caps 'definitionProvider true
put caps 'documentSymbolProvider true
;-- slow for now
;put caps 'workspace make map! reduce [
; 'workspaceFolders make map! reduce [
; 'supported true
; 'changeNotifications true
; ]
;]
json-body/result: make map! reduce [
'capabilities caps
]
;json-body/result: make map! reduce [
; 'capabilities make map! reduce [
; 'textDocumentSync TextDocumentSyncKind/Full
; ;'textDocumentSync make map! reduce [
; ; 'openClose true
; ; 'change 0
; ; 'willSave false
; ; 'willSaveWaitUntil false
; ; 'save make map! reduce ['includeText true]
; ;]
; ;'documentFormattingProvider true
; ;'documentRangeFormattingProvider true
; ;'documentOnTypeFormattingProvider make map! reduce ['firstTriggerCharacter "{" 'moreTriggerCharacter ""]
; ;'codeActionProvider true
; 'completionProvider make map! reduce ['resolveProvider true 'triggerCharacters trigger-chars]
; ;'signatureHelpProvider make map! reduce ['triggerCharacters ["."]]
; ;'definitionProvider true
; ;'documentHighlightProvider true
; 'hoverProvider true
; ;'renameProvider true
; ;'documentSymbolProvider true
; ;'workspaceSymbolProvider true
; ;'referencesProvider true
; ;'executeCommandProvider make map! reduce ['commands "Red.applyFix"]
; ]
;]
response
]
register-watched-files: function [][
json-body/id: "didChangeWatchedFiles"
json-body/result: none
json-body/method: "client/registerCapability"
json-body/params: make map! reduce [
'registrations reduce [
make map! reduce [
'id "didChangeWatchedFiles"
'method "workspace/didChangeWatchedFiles"
'registerOptions make map! reduce [
'watchers reduce [
make map! reduce [
'globPattern "**/*.{red,reds}"
'kind 7
]
]
]
]
]
]
response
]
on-initialized: function [params [map! none!]][
exit ;-- slow for now
diags: semantic/add-folder workspace-folder excluded-folder
if empty? diags [
exit
]
forall diags [
json-body/method: "textDocument/publishDiagnostics"
json-body/params: diags/1
response
]
]
on-didChangeConfiguration: function [params [map! none!]][
if open-logger? <> params/settings/red/rls-debug [
open-logger?: params/settings/red/rls-debug
unless debug-on? [
either open-logger? [
init-logger %logger.txt
][
init-logger none
]
]
]
;-- slow for now
;register-watched-files
]
on-didChangeWorkspaceFolders: function [params [map! none!]][
added: params/event/added
removed: params/event/removed
unless empty? added [
added-folder: make block! 4
forall added [
append added-folder added/1/uri
]
diags: semantic/add-folder added-folder excluded-folder
if empty? diags [
exit
]
forall diags [
json-body/method: "textDocument/publishDiagnostics"
json-body/params: diags/1
response
]
]
unless empty? removed [
removed-folder: make block! 4
forall removed [
append removed-folder removed/1/uri
]
diags: semantic/remove-folder removed-folder
if empty? diags [
exit
]
forall diags [
json-body/method: "textDocument/publishDiagnostics"
json-body/params: diags/1
response
]
]
]
on-didChangeWatchedFiles: function [params [map! none!]][
changes: params/changes
forall changes [
uri: changes/1/uri
either changes/1/type = 3 [
if vs: find-uri uri [
remove vs
]
if item: semantic/find-source uri [
write-log rejoin ["[INFO]: remove " uri]
remove item
]
clear-diag uri
][
if exists? file: lexer/uri-to-file uri [
source: read file
diags: semantic/add-source uri source
resp-diags diags uri
]
]
]
]
on-shutdown: function [params [map! none!]][
set 'shutdown? yes
]
clear-diag: function [uri [string!]][
json-body/method: "textDocument/publishDiagnostics"
json-body/params: make map! reduce [
'uri uri
'diagnostics []
]
response
]
resp-diags: function [diags [block!] uri [string!]][
if empty? diags [
clear-diag uri
exit
]
forall diags [
json-body/method: "textDocument/publishDiagnostics"
json-body/params: diags/1
response
]
]
find-uri: function [uri [string!]][
vs: versions
forall vs [
if vs/1/uri = uri [
return vs
]
]
none
]
on-textDocument-didOpen: function [params [map!]][
source: params/textDocument/text
uri: params/textDocument/uri
version: params/textDocument/version
either vs: find-uri uri [
vs/1/version: version
][
repend/only versions ['uri uri 'version version]
]
diags: semantic/add-source uri source
resp-diags diags uri
]
on-textDocument-didClose: function [params [map!]][
uri: params/textDocument/uri
if vs: find-uri uri [
remove vs
]
if all [
not semantic/workspace-file? uri
item: semantic/find-source uri
][
write-log rejoin ["[INFO]: remove " uri]
remove item
]
clear-diag uri
]
on-textDocument-didChange: function [params [map!]][
uri: params/textDocument/uri
unless params/contentChanges/1/range [
source: params/contentChanges/1/text
diags: semantic/add-source uri source
resp-diags diags uri
exit
]
version: params/textDocument/version
contentChanges: params/contentChanges
if all [
vs: find-uri uri
(vs/1/version + 1) = version
][
unless diags: semantic/update-source uri contentChanges [
json-body/error: make map! reduce [
'code -32002
'message "create ast error, please reopen this file!"
]
write-log "** unknown lexer bug **"
response
exit
]
resp-diags diags uri
vs/1/version: version
exit
]
write-log "** lost some text **"
response
]
on-textDocument-didSave: function [params [map!]][
uri: params/textDocument/uri
unless exists? file: lexer/uri-to-file uri [
write-log "** can't find file: **"
write-log mold file
exit
]
source: read file
if top: semantic/find-top uri [
;source: top/1/source
either not empty? diags: semantic/add-source uri source [
forall diags [
json-body/method: "textDocument/publishDiagnostics"
json-body/params: diags/1
response
]
][
clear-diag uri
]
]
]
on-textDocument-completion: function [params [map!]][
uri: params/textDocument/uri
line: params/position/line
column: params/position/character
comps: completion/complete uri line + 1 column + 1
json-body/result: make map! reduce [
;'isIncomplete true
'items comps
]
response
]
on-completionItem-resolve: function [params [map!]][
hstr: completion/resolve params
put params 'documentation either hstr [hstr][""]
json-body/result: params
response
]
on-textDocument-hover: function [params [map!]][
uri: params/textDocument/uri
line: params/position/line
column: params/position/character
result: completion/hover uri line + 1 column + 1
json-body/result: make map! reduce [
'contents either result [rejoin ["```^/" result "^/```"]][""]
'range none
]
response
]
on-textDocument-definition: function [params [map!]][
uri: params/textDocument/uri
line: params/position/line
column: params/position/character
unless result: completion/definition uri line + 1 column + 1 [result: []]
json-body/result: result
response
]
on-textDocument-symbol: function [params [map!]][
uri: params/textDocument/uri
unless result: completion/symbols uri [result: []]
json-body/result: result
response
]
init-logger %logger.txt
semantic/write-log: :write-log
write-log mold system/options/args
red-version-error: [
json-body/id: 0
json-body/result: none
json-body/method: none
json-body/params: none
json-body/error: make map! reduce [
'code -32002
'message "Can't work with this 'Red' version^/Please make sure that your Red toolchain newer than red-09jan19-acf34929.exe!"
]
response
exit
]
unless value? 'input-stdin [
write-log "console not support `input-stdin`"
do red-version-error
exit
]
unless value? 'read-stdin [
write-log "console not support `read-stdin`"
do red-version-error
exit
]
either all [
system/options/args
system/options/args/1 <> "debug-on"
][
init-logger none
open-logger?: false
debug-on?: false
][
open-logger?: true
debug-on?: true
]
watch: has [res] [
while [not shutdown?][
if error? res: try [process lsp-read][
write-log mold res
]
]
write-log "[shutdown]"
]
watch