Skip to content

Commit 80dec5b

Browse files
committed
Convert NotificationHandlers to unmonad
1 parent db20dc3 commit 80dec5b

File tree

1 file changed

+69
-43
lines changed

1 file changed

+69
-43
lines changed

src/rescript-editor-support/NotificationHandlers.re

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,58 @@ open RResult;
33
open TopTypes;
44
module J = JsonShort;
55

6-
let getTextDocument = doc => {
7-
let%opt uri = Json.get("uri", doc) |?> Json.string |?> Uri2.parse;
8-
let%opt text = Json.get("text", doc) |?> Json.string;
9-
Some((uri, text));
10-
};
6+
let getTextDocument = doc =>
7+
switch (Json.get("uri", doc) |?> Json.string |?> Uri2.parse) {
8+
| None => None
9+
| Some(uri) =>
10+
switch (Json.get("text", doc) |?> Json.string) {
11+
| None => None
12+
| Some(text) => Some((uri, text))
13+
}
14+
};
1115

1216
let notificationHandlers:
1317
list((string, (state, Json.t) => result(state, string))) = [
1418
(
1519
"textDocument/didOpen",
1620
(state, params) => {
17-
let%try (uri, text) =
21+
switch (
1822
Json.get("textDocument", params)
1923
|?> getTextDocument
20-
|> RResult.orError("Invalid params");
21-
Hashtbl.replace(state.documentText, uri, text);
22-
23-
let path = Uri2.toPath(uri);
24-
if (FindFiles.isSourceFile(path)) {
25-
let%try package = Packages.getPackage(uri, state);
26-
/* let name = FindFiles.getName(path); */
27-
if (!Hashtbl.mem(package.nameForPath, path)) {
28-
/* TODO: figure out what the name should be, and process it. */
29-
package.nameForPath
30-
|> Hashtbl.iter((name, _) => Log.log(" > " ++ name));
31-
Log.log("Reloading because you created a new file: " ++ path);
32-
Ok(state);
33-
/* Ok(reloadAllState(state)) */
34-
/* Hashtbl.add(package.nameForPath, path, name);
35-
Hashtbl.add(package.pathsForModule, name, Impl(path, Some(path)));
36-
Hashtbl.replace(state.packagesByRoot, package.basePath, {
37-
...package,
38-
localModules: [name, ...package.localModules]
39-
});
40-
Ok(state) */
24+
|> RResult.orError("Invalid params")
25+
) {
26+
| Error(e) => Error(e)
27+
| Ok((uri, text)) =>
28+
Hashtbl.replace(state.documentText, uri, text);
29+
let path = Uri2.toPath(uri);
30+
if (FindFiles.isSourceFile(path)) {
31+
switch (Packages.getPackage(uri, state)) {
32+
| Error(e) => Error(e)
33+
| Ok(package) =>
34+
/* let name = FindFiles.getName(path); */
35+
if (!Hashtbl.mem(package.nameForPath, path)) {
36+
/* TODO: figure out what the name should be, and process it. */
37+
package.nameForPath
38+
|> Hashtbl.iter((name, _) => Log.log(" > " ++ name));
39+
Log.log("Reloading because you created a new file: " ++ path);
40+
Ok(state);
41+
/* Ok(reloadAllState(state)) */
42+
/* Hashtbl.add(package.nameForPath, path, name);
43+
Hashtbl.add(package.pathsForModule, name, Impl(path, Some(path)));
44+
Hashtbl.replace(state.packagesByRoot, package.basePath, {
45+
...package,
46+
localModules: [name, ...package.localModules]
47+
});
48+
Ok(state) */
49+
} else {
50+
Ok(state);
51+
}
52+
};
4153
} else {
4254
Ok(state);
4355
};
44-
} else {
45-
Ok(state);
46-
};
47-
},
56+
}
57+
}
4858
),
4959
(
5060
"workspace/didChangeConfiguration",
@@ -62,18 +72,34 @@ let notificationHandlers:
6272
"textDocument/didChange",
6373
(state, params) => {
6474
open InfixResult;
65-
let%try doc = params |> RJson.get("textDocument");
66-
let%try uri = RJson.get("uri", doc) |?> RJson.string;
67-
let%try uri = Uri2.parse(uri) |> RResult.orError("Not a uri");
68-
let%try changes = RJson.get("contentChanges", params) |?> RJson.array;
69-
let%try text =
70-
List.nth(changes, List.length(changes) - 1)
71-
|> RJson.get("text")
72-
|?> RJson.string;
73-
/* Hmm how do I know if it's modified? */
74-
let state = State.updateContents(uri, text, state);
75-
Ok(state);
76-
},
75+
switch (params |> RJson.get("textDocument")) {
76+
| Error(e) => Error(e)
77+
| Ok(doc) =>
78+
switch (RJson.get("uri", doc) |?> RJson.string) {
79+
| Error(e) => Error(e)
80+
| Ok(uri) =>
81+
switch (Uri2.parse(uri) |> RResult.orError("Not a uri")) {
82+
| Error(e) => Error(e)
83+
| Ok(uri) =>
84+
switch (RJson.get("contentChanges", params) |?> RJson.array) {
85+
| Error(e) => Error(e)
86+
| Ok(changes) =>
87+
switch (
88+
List.nth(changes, List.length(changes) - 1)
89+
|> RJson.get("text")
90+
|?> RJson.string
91+
) {
92+
| Error(e) => Error(e)
93+
| Ok(text) =>
94+
/* Hmm how do I know if it's modified? */
95+
let state = State.updateContents(uri, text, state);
96+
Ok(state);
97+
}
98+
}
99+
}
100+
}
101+
}
102+
}
77103
),
78104
(
79105
"workspace/didChangeWatchedFiles",

0 commit comments

Comments
 (0)