Skip to content

Commit b60dcd0

Browse files
cristianocchenglou
authored andcommitted
Refactor JsonShort.
1 parent 3a95188 commit b60dcd0

8 files changed

+314
-338
lines changed

src/rescript-editor-support/BasicServer.re

+25-31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
module J = JsonShort;
2+
13
type messageSeverity =
24
| Error;
35

@@ -10,7 +12,7 @@ let showMessage = (typ, message) =>
1012
Rpc.sendNotification(
1113
stdout,
1214
"window/showMessage",
13-
JsonShort.(o([("type", i(severity(typ))), ("message", s(message))])),
15+
J.o([("type", J.i(severity(typ))), ("message", J.s(message))]),
1416
);
1517

1618
let handleMessage = (messageHandlers, id, method, params, state) => {
@@ -21,12 +23,10 @@ let handleMessage = (messageHandlers, id, method, params, state) => {
2123
Rpc.sendError(
2224
stdout,
2325
id,
24-
JsonShort.(
25-
o([
26-
("code", i(-32601)), /* MethodNotFoundError */
27-
("message", s("Unexpected method: " ++ method)),
28-
])
29-
),
26+
J.o([
27+
("code", J.i(-32601)), /* MethodNotFoundError */
28+
("message", J.s("Unexpected method: " ++ method)),
29+
]),
3030
);
3131
state;
3232
| handler =>
@@ -44,27 +44,23 @@ let handleMessage = (messageHandlers, id, method, params, state) => {
4444
Rpc.sendError(
4545
stdout,
4646
id,
47-
JsonShort.(
48-
o([
49-
("code", i(-32603)), /* InternalError */
50-
("message", s(string)),
51-
])
52-
),
47+
J.o([
48+
("code", J.i(-32603)), /* InternalError */
49+
("message", J.s(string)),
50+
]),
5351
);
5452
state;
5553
| exception e =>
5654
Rpc.sendError(
5755
stdout,
5856
id,
59-
JsonShort.(
60-
o([
61-
("code", i(-32603)), /* InternalError */
62-
(
63-
"message",
64-
s(Printexc.to_string(e) ++ Printexc.get_backtrace()),
65-
),
66-
])
67-
),
57+
J.o([
58+
("code", J.i(-32603)), /* InternalError */
59+
(
60+
"message",
61+
J.s(Printexc.to_string(e) ++ Printexc.get_backtrace()),
62+
),
63+
]),
6864
);
6965
/* }; */
7066
state;
@@ -151,15 +147,13 @@ let run =
151147
Rpc.sendError(
152148
stdout,
153149
id,
154-
JsonShort.(
155-
o([
156-
("code", i(-32603)), /* InternalError */
157-
(
158-
"message",
159-
s(Printexc.to_string(e) ++ Printexc.get_backtrace()),
160-
),
161-
])
162-
),
150+
J.o([
151+
("code", J.i(-32603)), /* InternalError */
152+
(
153+
"message",
154+
J.s(Printexc.to_string(e) ++ Printexc.get_backtrace()),
155+
),
156+
]),
163157
);
164158
}
165159
| _ => failwith("Client must send 'initialize' as first event")

src/rescript-editor-support/Diagnostics.re

+56-60
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
open TopTypes;
22
open Infix;
3+
module J = JsonShort;
34

45
let makeDiagnostic = (documentText, ((line, c1, c2), message)) => {
5-
open JsonShort;
66
let text = String.concat("\n", message);
77
let (l2, c22) =
88
{
@@ -14,10 +14,10 @@ let makeDiagnostic = (documentText, ((line, c1, c2), message)) => {
1414
switch (ErrorParser.parseDependencyError(text)) {
1515
| None =>
1616
Some(
17-
o([
17+
J.o([
1818
("range", Protocol.rangeOfInts(line, c1, l2, c22)),
19-
("message", s(text)),
20-
("severity", i(Utils.startsWith(text, "Warning") ? 2 : 1)),
19+
("message", J.s(text)),
20+
("severity", J.i(Utils.startsWith(text, "Warning") ? 2 : 1)),
2121
]),
2222
)
2323
| Some(_) =>
@@ -42,66 +42,62 @@ let runDiagnostics = (uri, state, ~package) => {
4242
Log.log("Running diagnostics for " ++ uri);
4343
let%try_consume documentText = getText(state, uri);
4444
let%try_consume result = State.getCompilationResult(uri, state, ~package);
45-
JsonShort.(
46-
Rpc.sendNotification(
47-
stdout,
48-
"textDocument/publishDiagnostics",
49-
o([
50-
("uri", s(uri)),
51-
(
52-
"diagnostics",
53-
switch (result) {
54-
| AsYouType.SyntaxError(text, otherText, _) =>
55-
let errors =
56-
ErrorParser.parseErrors(
57-
Utils.splitLines(Utils.stripAnsii(otherText)),
58-
);
59-
let errors =
60-
errors
61-
|> List.filter(((_loc, message)) =>
62-
message
63-
!= [
64-
"Error: Uninterpreted extension 'merlin.syntax-error'.",
65-
]
66-
);
67-
let errors =
68-
ErrorParser.parseErrors(
69-
Utils.splitLines(Utils.stripAnsii(text)),
70-
)
71-
@ errors;
72-
l(errors |> Utils.filterMap(makeDiagnostic(documentText)));
73-
| Success(text, _) =>
74-
if (String.trim(text) == "") {
75-
l([]);
76-
} else {
77-
let errors =
78-
ErrorParser.parseErrors(
79-
Utils.splitLines(Utils.stripAnsii(text)),
80-
);
81-
l(errors |> Utils.filterMap(makeDiagnostic(documentText)));
82-
}
83-
| TypeError(text, _) =>
84-
Log.log("type error here " ++ text);
45+
Rpc.sendNotification(
46+
stdout,
47+
"textDocument/publishDiagnostics",
48+
J.o([
49+
("uri", J.s(uri)),
50+
(
51+
"diagnostics",
52+
switch (result) {
53+
| AsYouType.SyntaxError(text, otherText, _) =>
54+
let errors =
55+
ErrorParser.parseErrors(
56+
Utils.splitLines(Utils.stripAnsii(otherText)),
57+
);
58+
let errors =
59+
errors
60+
|> List.filter(((_loc, message)) =>
61+
message
62+
!= ["Error: Uninterpreted extension 'merlin.syntax-error'."]
63+
);
64+
let errors =
65+
ErrorParser.parseErrors(
66+
Utils.splitLines(Utils.stripAnsii(text)),
67+
)
68+
@ errors;
69+
J.l(errors |> Utils.filterMap(makeDiagnostic(documentText)));
70+
| Success(text, _) =>
71+
if (String.trim(text) == "") {
72+
J.l([]);
73+
} else {
8574
let errors =
8675
ErrorParser.parseErrors(
8776
Utils.splitLines(Utils.stripAnsii(text)),
88-
)
89-
|> List.filter(((_loc, message)) => {
90-
!
91-
Str.string_match(
92-
Str.regexp(
93-
{|.*Missing dependency [a-zA-Z]+ in search path|},
94-
),
95-
String.concat(" ", message),
96-
0,
97-
)
98-
});
77+
);
78+
J.l(errors |> Utils.filterMap(makeDiagnostic(documentText)));
79+
}
80+
| TypeError(text, _) =>
81+
Log.log("type error here " ++ text);
82+
let errors =
83+
ErrorParser.parseErrors(
84+
Utils.splitLines(Utils.stripAnsii(text)),
85+
)
86+
|> List.filter(((_loc, message)) => {
87+
!
88+
Str.string_match(
89+
Str.regexp(
90+
{|.*Missing dependency [a-zA-Z]+ in search path|},
91+
),
92+
String.concat(" ", message),
93+
0,
94+
)
95+
});
9996

100-
l(errors |> Utils.filterMap(makeDiagnostic(documentText)));
101-
},
102-
),
103-
]),
104-
)
97+
J.l(errors |> Utils.filterMap(makeDiagnostic(documentText)));
98+
},
99+
),
100+
]),
105101
);
106102
};
107103

src/rescript-editor-support/EditorSupportCommands.re

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
open Infix;
2+
module J = JsonShort;
23

34
let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
45
let locations =
@@ -15,19 +16,18 @@ let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
1516
| None => locations
1617
};
1718
};
18-
open JsonShort;
1919
let dedupTable = Hashtbl.create(1);
2020
let dedupHover = (hover, i) => {
2121
let isCandidate = String.length(hover) > 10;
2222
if (isCandidate) {
2323
switch (Hashtbl.find_opt(dedupTable, hover)) {
24-
| Some(n) => s("#" ++ string_of_int(n))
24+
| Some(n) => J.s("#" ++ string_of_int(n))
2525
| None =>
2626
Hashtbl.replace(dedupTable, hover, i);
27-
s(hover);
27+
J.s(hover);
2828
};
2929
} else {
30-
s(hover);
30+
J.s(hover);
3131
};
3232
};
3333
let locationsInfo =
@@ -83,7 +83,7 @@ let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
8383
[
8484
(
8585
"definition",
86-
o(
86+
J.o(
8787
uriIsCurrentFile
8888
? [range] : [("uri", Json.String(uri2)), range],
8989
),
@@ -96,10 +96,12 @@ let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
9696
skip
9797
? None
9898
: Some(
99-
o([("range", Protocol.rangeOfLoc(location))] @ hover @ def),
99+
J.o(
100+
[("range", Protocol.rangeOfLoc(location))] @ hover @ def,
101+
),
100102
);
101103
})
102-
|> l;
104+
|> J.l;
103105

104106
Json.stringify(locationsInfo);
105107
};

0 commit comments

Comments
 (0)