Skip to content

Commit 87a6a3e

Browse files
committed
Convert ProcessExtra to unmonad
1 parent 01b52c6 commit 87a6a3e

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

src/rescript-editor-support/ProcessExtra.re

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,48 @@ let rec relative = (ident, path) =>
2929
| _ => None
3030
};
3131

32-
let findClosestMatchingOpen = (opens, path, ident, loc) => {
33-
let%opt openNeedle = relative(ident, path);
34-
35-
let matching =
36-
Hashtbl.fold(
37-
(_, op, res) =>
38-
if (Utils.locWithinLoc(loc, op.extent)
39-
&& Path.same(op.path, openNeedle)) {
40-
[op, ...res];
41-
} else {
42-
res;
43-
},
44-
opens,
45-
[],
46-
)
47-
|> List.sort((a: SharedTypes.openTracker, b) =>
48-
b.loc.loc_start.pos_cnum - a.loc.loc_start.pos_cnum
49-
);
32+
let findClosestMatchingOpen = (opens, path, ident, loc) =>
33+
switch (relative(ident, path)) {
34+
| None => None
35+
| Some(openNeedle) =>
36+
let matching =
37+
Hashtbl.fold(
38+
(_, op, res) =>
39+
if (Utils.locWithinLoc(loc, op.extent)
40+
&& Path.same(op.path, openNeedle)) {
41+
[op, ...res];
42+
} else {
43+
res;
44+
},
45+
opens,
46+
[],
47+
)
48+
|> List.sort((a: SharedTypes.openTracker, b) =>
49+
b.loc.loc_start.pos_cnum - a.loc.loc_start.pos_cnum
50+
);
5051

51-
switch (matching) {
52-
| [] => None
53-
| [first, ..._] => Some(first)
52+
switch (matching) {
53+
| [] => None
54+
| [first, ..._] => Some(first)
55+
};
5456
};
55-
};
5657

5758
let getTypeAtPath = (~env, path) => {
5859
switch (Query.fromCompilerPath(~env, path)) {
5960
| `GlobalMod(_) => `Not_found
6061
| `Global(moduleName, path) => `Global((moduleName, path))
6162
| `Not_found => `Not_found
6263
| `Exported(env, name) =>
63-
let res = {
64-
let%opt stamp = Hashtbl.find_opt(env.exported.types, name);
65-
let declaredType = Hashtbl.find_opt(env.file.stamps.types, stamp);
66-
switch (declaredType) {
67-
| Some(declaredType) => Some(`Local(declaredType))
64+
let res =
65+
switch (Hashtbl.find_opt(env.exported.types, name)) {
6866
| None => None
67+
| Some(stamp) =>
68+
let declaredType = Hashtbl.find_opt(env.file.stamps.types, stamp);
69+
switch (declaredType) {
70+
| Some(declaredType) => Some(`Local(declaredType))
71+
| None => None
72+
};
6973
};
70-
};
7174
res |? `Not_found;
7275
| `Stamp(stamp) =>
7376
let res = {
@@ -91,13 +94,16 @@ module F =
9194
) => {
9295
let extra = Collector.extra;
9396

94-
let maybeAddUse = (path, ident, loc, tip) => {
95-
let%opt_consume tracker =
96-
findClosestMatchingOpen(extra.opens, path, ident, loc);
97-
let%opt_consume relpath = Query.makeRelativePath(tracker.path, path);
98-
99-
tracker.used = [(relpath, tip, loc), ...tracker.used];
100-
};
97+
let maybeAddUse = (path, ident, loc, tip) =>
98+
switch (findClosestMatchingOpen(extra.opens, path, ident, loc)) {
99+
| None => ()
100+
| Some(tracker) =>
101+
switch (Query.makeRelativePath(tracker.path, path)) {
102+
| None => ()
103+
| Some(relpath) =>
104+
tracker.used = [(relpath, tip, loc), ...tracker.used]
105+
}
106+
};
101107

102108
let addLocation = (loc, ident) =>
103109
extra.locations = [(loc, ident), ...extra.locations];

0 commit comments

Comments
 (0)