Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit 8722279

Browse files
committed
Convert State over to unmonad
1 parent 3eb03e4 commit 8722279

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

src/rescript-editor-support/State.re

+44-27
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ let converter = src => {
1515

1616
let newDocsForCmt = (~moduleName, cmtCache, changed, cmt, src) => {
1717
let uri = Uri2.fromPath(src |? cmt);
18-
let%opt file =
18+
switch (
1919
Process_406.fileForCmt(~moduleName, ~uri, cmt, converter(src))
20-
|> RResult.toOptionAndLog;
21-
Hashtbl.replace(cmtCache, cmt, (changed, file));
22-
Some(file);
20+
|> RResult.toOptionAndLog
21+
) {
22+
| None => None
23+
| Some(file) =>
24+
Hashtbl.replace(cmtCache, cmt, (changed, file));
25+
Some(file);
26+
};
2327
};
2428

2529
let docsForCmt = (~moduleName, cmt, src, state) =>
@@ -56,21 +60,27 @@ open Infix;
5660

5761
let getFullFromCmt = (~state, ~uri) => {
5862
let path = Uri2.toPath(uri);
59-
let%try package = Packages.getPackage(uri, state);
60-
let moduleName =
61-
BuildSystem.namespacedName(package.namespace, FindFiles.getName(path));
62-
switch (Hashtbl.find_opt(package.pathsForModule, moduleName)) {
63-
| Some(paths) =>
64-
let cmt =
65-
SharedTypes.getCmt(~interface=Utils.endsWith(path, "i"), paths);
66-
let%try full = Process_406.fullForCmt(~moduleName, ~uri, cmt, x => [x]);
67-
Hashtbl.replace(
68-
package.interModuleDependencies,
69-
moduleName,
70-
SharedTypes.hashList(full.extra.externalReferences) |> List.map(fst),
71-
);
72-
Ok((package, full));
73-
| None => Error("can't find module " ++ moduleName)
63+
switch (Packages.getPackage(uri, state)) {
64+
| Error(e) => Error(e)
65+
| Ok(package) =>
66+
let moduleName =
67+
BuildSystem.namespacedName(package.namespace, FindFiles.getName(path));
68+
switch (Hashtbl.find_opt(package.pathsForModule, moduleName)) {
69+
| Some(paths) =>
70+
let cmt =
71+
SharedTypes.getCmt(~interface=Utils.endsWith(path, "i"), paths);
72+
switch (Process_406.fullForCmt(~moduleName, ~uri, cmt, x => [x])) {
73+
| Error(e) => Error(e)
74+
| Ok(full) =>
75+
Hashtbl.replace(
76+
package.interModuleDependencies,
77+
moduleName,
78+
SharedTypes.hashList(full.extra.externalReferences) |> List.map(fst),
79+
);
80+
Ok((package, full));
81+
};
82+
| None => Error("can't find module " ++ moduleName)
83+
};
7484
};
7585
};
7686

@@ -92,23 +102,30 @@ let docsForModule = (modname, state, ~package) =>
92102
};
93103

94104
let fileForUri = (state, uri) => {
95-
let%try (_package, {extra, file}) = getFullFromCmt(~state, ~uri);
96-
Ok((file, extra));
105+
switch (getFullFromCmt(~state, ~uri)) {
106+
| Error(e) => Error(e)
107+
| Ok((_package, {extra, file})) => Ok((file, extra))
108+
}
97109
};
98110

99111
let fileForModule = (state, ~package, modname) => {
100-
let%opt (file, _) = docsForModule(modname, state, ~package);
101-
Some(file);
112+
switch (docsForModule(modname, state, ~package)) {
113+
| None => None
114+
| Some((file, _)) => Some(file)
115+
}
102116
};
103117

104118
let extraForModule = (state, ~package, modname) =>
105119
if (Hashtbl.mem(package.pathsForModule, modname)) {
106120
let paths = Hashtbl.find(package.pathsForModule, modname);
107121
/* TODO do better? */
108-
let%opt src = SharedTypes.getSrc(paths);
109-
switch (getFullFromCmt(~state, ~uri=Uri2.fromPath(src))) {
110-
| Ok((_package, {extra})) => Some(extra)
111-
| Error(_) => None
122+
switch (SharedTypes.getSrc(paths)) {
123+
| None => None
124+
| Some(src) =>
125+
switch (getFullFromCmt(~state, ~uri=Uri2.fromPath(src))) {
126+
| Ok((_package, {extra})) => Some(extra)
127+
| Error(_) => None
128+
}
112129
};
113130
} else {
114131
None;

0 commit comments

Comments
 (0)