-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsp
executable file
·379 lines (341 loc) · 11 KB
/
lsp
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
#!/usr/bin/env elvish
use file
use str
use path
# pipe between input thread(writer), file tracking thread(writer) and server thread(reader)
var p = (file:pipe)
var content = (put [&jsonrpc=2.0 &id=(num 1) &method=initialize ¶ms=[
&rootUri=file://$pwd
&processId=(num $pid)
&capabilities=[
&general=[
&positionEncodings=[utf-8]
]
&textDocument=[
&publishDiagnostics=[&]
]
]
]] | to-json)
put "-------------> main thread send: "$content
printf "Content-Length: %d\r\n\r\n%s" (count $content) $content > $p
set content = (put [&jsonrpc=2.0 &method=initialized ¶ms=[&]] | to-json)
put "-------------> main thread send: "$content
printf "Content-Length: %d\r\n\r\n%s" (count $content) $content > $p
var mnt = '/tmp/acme'
var winid
var winname = (echo $pwd | 9 tr .@ _)/+LSP
var opens = [&]
fn checkwin {
try {
var @parts = (str:fields (9p read acme/index | grep -e $winname))
set winid = $parts[0]
} catch e {
# window not exist, just create one
set winid = (str:fields (9p read acme/new/ctl) | take 1)
echo 'name '$winname | 9p write acme/$winid/ctl
echo 'add_env lsp_root='$pwd | 9p write acme/$winid/ctl
# close current window when tmux session exits.
tmux set-hook -p 'alert-silence['$winid']' 'run "echo delete | 9p write acme/'$winid'/ctl > /dev/null || true"'
}
}
fn append {|s|
echo $s | 9p write acme/$winid/body
echo clean | 9p write acme/$winid/ctl
}
fn endoffset {
var f = (file:open-output &also-input=$true &if-not-exists=error &if-exists=update $mnt/$winid/addr)
print '$' > $f
var @parts = (str:fields (cat < $f))
put $parts[1]
file:close $f
}
fn show {|begin end|
print '#'$begin,'#'$end | 9p write acme/$winid/addr
echo dot=addr | 9p write acme/$winid/ctl
echo show | 9p write acme/$winid/ctl
}
fn read_line {|f l|
var @lines = (cat $f | from-lines)
put $lines[$l]
}
var log = (file:open $mnt/log)
var id = (num 1)
var cbs = [&]
run-parallel {
# server thread
eval (str:join ' ' $args) < $p | while $true {
var m
try {
var n = (read-upto "\n")[16..-2] # get content length
read-upto "\n" | nop # skip "\r\n"
set m = (echo (read-bytes $n) | from-json)
} catch e {
put 'server thread error:'
pprint $e
put 'server thread exit'
file:close $p[r]
break
}
try {
if (and (has-key $m method) (==s $m[method] textDocument/publishDiagnostics)) {
if (not (eq $m[params][diagnostics] [])) {
var file = $m[params][uri][7..] # skip "file://"
var cur_file = (str:fields (9p read acme/(9 dial unix!(9 namespace)/acmefocused)/tag) | take 1)
if (==s $file $cur_file) {
var begin = (endoffset)
for entry $m[params][diagnostics] {
var msg = (printf "%s:%d+#%d,%d+#%d: %s" $file^
$entry[range][start][line] $entry[range][start][character]^
$entry[range][end][line] $entry[range][end][character]^
$entry[message] | slurp)
append $msg
}
show $begin (endoffset)
}
}
} else {
$cbs[$m[id]] $m[result]
del cbs[$m[id]]
}
} catch e {
pprint $m
}
}
} {
# file tracking thread
var content
fn handle {|winid cmd @rest|
# skip if there's no file path
if (eq $rest []) {
return
}
var name = $rest[0]
# skip non-regular file
# TODO: symlink?
if (not (path:is-regular $name)) {
return
}
if (and (==s del $cmd) (has-key $opens $name)) {
set content = (put [&jsonrpc=2.0 &method=textDocument/didClose ¶ms=[
&textDocument=[
&uri=file://$name
]
]] | to-json)
put "-------------> file tracking thread send: "$content
printf "Content-Length: %d\r\n\r\n%s" (count $content) $content > $p
del opens[$name]
tmux set-hook -pu 'alert-silence['$winid']'
} elif (and (==s put $cmd) (has-key $opens $name)) {
set content = (put [&jsonrpc=2.0 &method=textDocument/didChange ¶ms=[
&textDocument=[
&uri=file://$name
&version=(num 0)
]
&contentChanges=[[&text=(9p read acme/$winid/body | slurp)]]
]] | to-json)
put "-------------> file tracking thread send: "$content
printf "Content-Length: %d\r\n\r\n%s" (count $content) $content > $p
set content = (put [&jsonrpc=2.0 &method=textDocument/didSave ¶ms=[
&textDocument=[
&uri=file://$name
]
]] | to-json)
put "-------------> file tracking thread send: "$content
printf "Content-Length: %d\r\n\r\n%s" (count $content) $content > $p
if (eq $E:_lsp_format_on_save 1) {
set id = (+ $id 1)
# register a edit callback
set cbs[$id] = {|m|
var f = (file:open-output &if-not-exists=error &if-exists=update $mnt/$winid/addr)
defer {
file:close $f
}
fn apply {|m|
printf '%d+#%d,%d+#%d'^
$m[range][start][line] $m[range][start][character]^
$m[range][end][line] $m[range][end][character]^
> $f
print $m[newText] | 9p write acme/$winid/data
}
if (==s (kind-of $m) list) {
var @entries = (order &less-than={|a b|
if (== $a[range][start][line] $b[range][start][line]) {
put (< $a[range][start][character] $b[range][start][character])
} else {
put (< $a[range][start][line] $b[range][start][line])
}
} &reverse=$true $m)
for entry $entries {
apply $entry
}
} elif (not (is $m $nil)) {
apply $m
}
}
set content = (put [&jsonrpc=2.0 &id=$id &method=textDocument/formatting ¶ms=[
&textDocument=[
&uri=file://$name
]
&options=[
&tabSize=(num 4)
&insertSpaces=$false
]
]] | to-json)
put "-------------> file tracking thread send: "$content
printf "Content-Length: %d\r\n\r\n%s" (count $content) $content > $p
}
}
}
from-lines < $log | each {|l|
try {
handle (str:fields $l)
} catch e {
put $l
put 'file tracking thread: error:'
put $e
put 'file tracking thread: exit'
break
}
}
} {
# Input reader thread (also the main thread)
while $true {
try {
var l = (read-line)
var @parts = (str:fields $l)
var cmd = $parts[0]
var file = $parts[1]
put 'recv: '$l
if (==s $cmd def) {
if (< (count $parts) 4) {
put "not enough parameters for definition, skip: "$l
continue
}
var line = (num $parts[2])
var char = (num $parts[3])
set id = (+ $id 1)
set content = (put [&jsonrpc=2.0 &id=$id &method=textDocument/definition ¶ms=[
&textDocument=[
&uri=file://$file
]
&position=[
&line=$line
&character=$char
]
]] | to-json)
checkwin
append (printf "\n%s:%d def: %s" $file (+ 1 $parts[2]) (read_line $file $parts[2]) | slurp)
} elif (==s $cmd ref) {
if (< (count $parts) 4) {
put "not enough parameters for reference, skip: "$l
continue
}
var line = (num $parts[2])
var char = (num $parts[3])
set id = (+ $id 1)
set content = (put [&jsonrpc=2.0 &id=$id &method=textDocument/references ¶ms=[
&textDocument=[
&uri=file://$file
]
&position=[
&line=$line
&character=$char
]
&context=[
&includeDeclaration=$true
]
]] | to-json)
checkwin
append (printf "\n%s:%d refs: %s" $file (+ 1 $parts[2]) (read_line $file $parts[2]) | slurp)
} elif (==s $cmd imp) {
if (< (count $parts) 4) {
put "not enough parameters for implementation, skip: "$l
continue
}
var line = (num $parts[2])
var char = (num $parts[3])
set id = (+ $id 1)
set content = (put [&jsonrpc=2.0 &id=$id &method=textDocument/implementation ¶ms=[
&textDocument=[
&uri=file://$file
]
&position=[
&line=$line
&character=$char
]
]] | to-json)
checkwin
append (printf "\n%s:%d impls: %s" $file (+ 1 $parts[2]) (read_line $file $parts[2]) | slurp)
} elif (==s $cmd open) {
if (has-key $opens $file) {
continue
}
if (< (count $parts) 3) {
put "not enough parameters for open, skip: "$l
continue
}
var winid = $parts[2]
set content = (put [&jsonrpc=2.0 &method=textDocument/didOpen ¶ms=[
&textDocument=[
&uri=file://$file
&languageId=(try { put (path:ext $file)[1..] } catch e { put unknown })
&version=(num 0)
&text=(9p read acme/$winid/body | slurp)
]
]] | to-json)
set opens[$file] = $nil
# close current window when tmux session exits.
tmux set-hook -p 'alert-silence['$winid']' 'run "echo del | 9p write acme/'$winid'/ctl > /dev/null || true"'
} else {
put "skip unknown input: "$l
continue
}
# register a display callback
set cbs[$id] = {|m|
var count = 0
var addr
var begin = (endoffset)
var file
if (==s (kind-of $m) list) {
for entry $m {
set file = $entry[uri][7..]
set addr = (printf "%s:%d+#%d,%d+#%d" $file^
$entry[range][start][line] $entry[range][start][character]^
$entry[range][end][line] $entry[range][end][character]^
)
append (printf "%s %s\n" $addr (read_line $file $entry[range][start][line]))
set count = (+ $count 1)
}
} else {
set file = $m[uri][7..]
set addr = (printf "%s:%d+#%d,%d+#%d" $file^
$m[range][start][line] $m[range][start][character]^
$m[range][end][line] $m[range][end][character]^
)
append (printf "%s %s\n" $addr (read_line $file $m[range][start][line]))
set count = (+ $count 1)
}
# jump to it if there's only one result
if (== $count 1) {
9 plumb -s lsp -d edit -a 'envs=lsp_root:'$pwd $addr
}
# make sure the results are visible
show $begin (endoffset)
}
put "-------------> input thread send: "$content
printf "Content-Length: %d\r\n\r\n%s" (count $content) $content > $p
} catch e {
var reason = $e[reason]
if (and (has-key $e[reason] type) (==s $reason[type] flow) (==s $reason[name] continue)) {
continue
} else {
put 'input thread: error:'
pprint $e
break
}
}
}
put 'input thread: exit'
file:close $p[w] # terminate server thread
file:close $log # terminate file tracking thread
}