Source file lsp_base.ml
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
open Lplib
open Common
open Handle
let std_protocol = ref true
module J = Yojson.Basic
let l = if !std_protocol then [] else l
let _parse_uri str =
let l = String.length str - 7 in
String.(sub str 7 l)
let mk_reply ~id ~result =
`Assoc [ "jsonrpc", `String "2.0"; "id", `Int id; "result", result ]
let mk_event m p =
`Assoc [ "jsonrpc", `String "2.0"; "method", `String m; "params", `Assoc p ]
let mk_showMessage ?(t=1) m =
`Assoc
[ "jsonrpc", `String "2.0";
"method", `String "window/showMessage";
"params", `Assoc [
"type", `Int t;
"message", `String m
]
]
let json_of_range l1 c1 l2 c2 : J.t =
let max0 n = if n < 0 then 0 else n in
let l1 = max0 (l1 - 1) and c1 = max0 c1 in
let l2 = max0 (l2 - 1) and c2 = max0 c2 in
let l2, c2 = if (l2, c2) < (l1, c1) then l1, c1 else l2, c2 in
`Assoc ["start", `Assoc ["line", `Int l1; "character", `Int c1];
"end", `Assoc ["line", `Int l2; "character", `Int c2]]
let mk_range (p : Pos.pos) : J.t =
Pos.(json_of_range p.start_line p.start_col p.end_line p.end_col)
let mk_range_of_interval (r : Range.t) : J.t =
let s = Range.interval_start r and e = Range.interval_end r in
json_of_range (Range.line s) (Range.column s)
(Range.line e) (Range.column e)
let json_of_goal (hyps, concl) =
let json_of_hyp (s,t) = `Assoc ["hname", `String s; "htype", `String t] in
match concl with
| Goal.Typ(meta,typ) ->
`Assoc [
"typeofgoal", `String "Typ"
; "gid", `String meta
; "hyps", `List (List.map json_of_hyp hyps)
; "type", `String typ]
| Goal.Unif c ->
`Assoc [
"typeofgoal", `String "Unif"
; "hyps", `List (List.map json_of_hyp hyps)
; "constr", `String c]
let json_of_goals ?logs goals =
let logs = match logs with None -> "" | Some s -> s in
match goals with
| None ->
`Assoc [
"logs", `String logs
]
| Some goals ->
`Assoc [
"goals", `List List.(map json_of_goal goals);
"logs" , `String logs
]
let mk_diagnostic
((p : Pos.pos), (lvl : int), (msg : string), (goals : _ option)) : J.t =
let goals = json_of_goals goals in
let range = mk_range p in
`Assoc (mk_extra ["goal_info", goals] @
["range", range;
"severity", `Int lvl;
"message", `String msg;
])
let mk_diagnostics ~uri ~version ld : J.t =
let = mk_extra ["version", `Int version] in
mk_event "textDocument/publishDiagnostics" @@
extra @
["uri", `String uri;
"diagnostics", `List List.(map mk_diagnostic ld)]