-
Notifications
You must be signed in to change notification settings - Fork 3
/
tyd.ty
executable file
·72 lines (60 loc) · 1.82 KB
/
tyd.ty
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
#!/usr/bin/env ty
import io (stdin)
import json
import sh (sh)
let log = io.open('tyd.log', 'w+')
let i = 0
while let &words ~> &[-1] ~> int ~> $n = stdin.nextLine() {
// Skip empty line
stdin.nextLine()
let msg = json.parse(stdin.next(n).str())
log.print(msg)
log.flush()
let s = json.encode({
id: 1,
result: {
capabilities: {
definitionProvider: true
},
serverInfo: {
name: 'tyd',
version: '0.0.1'
}
}
})
match msg['method'] {
'initialize' => {
io.stdout.write("Content-Length: {#s}\r\n\r\n{s}")
io.stdout.flush()
},
'textDocument/didSave' => {
let path = msg['params']['textDocument']['uri'].slice(7)
log.print("path = {path}")
log.flush()
let out, status = sh("./ty -c '{path}'", combineOutput: true)
log.print("out = {out}")
log.print("status = {status}")
log.flush()
let diagnostics = if status != 0 and let [_, text, int ~> line, int ~> char] = out.match!(/^([^\n]+)(?:.|\n)*at[^:]+:(\d+):(\d+)/) {[{
range: {
start: { line: line - 1, character: char - 1 },
end: { line: line - 1, character: char - 1 }
},
message: text,
severity: 1
}]} else {
[]
}
let s = json.encode({
method: 'textDocument/publishDiagnostics',
params: {
uri: "file://{path}",
diagnostics
},
})
io.stdout.write("Content-Length: {#s}\r\n\r\n{s}")
io.stdout.flush()
},
_ => { }
}
}