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

Commit feaad4b

Browse files
committed
Remove uses of let%opt_wrap.
1 parent 3100963 commit feaad4b

File tree

8 files changed

+92
-120
lines changed

8 files changed

+92
-120
lines changed

src/ppx/Ppx_Monads.re

-23
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,6 @@ Alternatively, if you are just performing a side effect, and want
9191
the result of the whole thing to be unit, use `let%consume`.
9292
|};
9393

94-
let opt_wrap_explanation = {|
95-
Optional declaration sugar:
96-
```
97-
let%opt_wrap name = value;
98-
otherStuff
99-
```
100-
is transformed into
101-
```
102-
switch (value) {
103-
| None => None
104-
| Some(name) => Some({
105-
otherStuff
106-
})
107-
}
108-
```
109-
The `wrap` suffix means that the `otherStuff` will be automatically
110-
wrapped in a `Some`.
111-
112-
If you don't want this wrapping, then use `let%opt`.
113-
Alternatively, if you are just performing a side effect, and want
114-
the result of the whole thing to be unit, use `let%consume`.
115-
|};
11694

11795
let opt_consume_explanation = {|
11896
Optional declaration sugar:
@@ -143,7 +121,6 @@ let mapper =
143121
) as txt, loc}, PStr([{pstr_desc: Pstr_eval({pexp_desc: Pexp_let(Nonrecursive, bindings, continuation)}, _attributes)}]))) => {
144122
let (front, explanation) = switch (txt) {
145123
| "opt" => ([%expr Monads.Option.bind], opt_explanation)
146-
| "opt_wrap" => ([%expr Monads.Option.map], opt_wrap_explanation)
147124
| "opt_consume" => ([%expr Monads.Option.consume], opt_consume_explanation)
148125
| "try" => ([%expr Monads.Result.bind], "Sugar for the Result type")
149126
| "try_wrap" => ([%expr Monads.Result.map], "Sugar for the Result type - auto-wraps in `Ok()`")

src/rescript-editor-support/MessageHandlers.re

+22-23
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,28 @@ let handlers:
7777
let%try (uri, pos) = Protocol.rPositionParams(params);
7878
let%try package = getPackage(uri, state);
7979

80-
let res =
81-
{
82-
let pos = Utils.cmtLocFromVscode(pos);
83-
let%opt (file, extra) =
84-
State.fileForUri(state, ~package, uri) |> toOptionAndLog;
85-
86-
let%opt_wrap refs = References.refsForPos(~file, ~extra, pos);
87-
(
88-
state,
89-
J.l(
90-
refs
91-
|> List.map(loc =>
92-
J.o([
93-
("range", Protocol.rangeOfLoc(loc)),
94-
("kind", J.i(2)),
95-
])
96-
),
97-
),
98-
);
99-
}
100-
|? (state, Json.Null);
101-
102-
Ok(res);
80+
let pos = Utils.cmtLocFromVscode(pos);
81+
let refs =
82+
switch (State.fileForUri(state, ~package, uri) |> toOptionAndLog) {
83+
| None => None
84+
| Some((file, extra)) => References.refsForPos(~file, ~extra, pos)
85+
};
86+
Ok((
87+
state,
88+
switch (refs) {
89+
| None => J.null
90+
| Some(refs) =>
91+
J.l(
92+
refs
93+
|> List.map(loc =>
94+
J.o([
95+
("range", Protocol.rangeOfLoc(loc)),
96+
("kind", J.i(2)),
97+
])
98+
),
99+
)
100+
},
101+
));
103102
},
104103
),
105104
(

src/rescript-editor-support/Monads.re

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
2-
module type MonadThing = {
3-
type t('a);
4-
let map: (t('a), ~f: 'a => 'b) => t('b);
5-
let bind: (t('a), ~f: 'a => t('b)) => t('b);
6-
let consume: (t('a), ~f: 'a => unit) => unit;
7-
};
8-
91
module Option = {
102
type t('a) = option('a);
11-
let map = (value, ~f as use) =>
12-
switch (value) {
13-
| Some(x) => Some(use(x))
14-
| None => None
15-
};
163
let bind = (value, ~f as use) =>
174
switch (value) {
185
| Some(x) => use(x)
@@ -25,7 +12,7 @@ module Option = {
2512
};
2613
};
2714

28-
module O: MonadThing = Option;
15+
module O = Option;
2916

3017
module Result = {
3118
let map /*: t 'a 'b => f::('a => 'c) => t 'c 'b*/ = (value, ~f as use) =>

src/rescript-editor-support/NewCompletions.re

+9-12
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,13 @@ let getItems =
512512

513513
switch (determineCompletion(multiple)) {
514514
| `Normal(path) =>
515-
{
516-
Log.log("normal " ++ pathToString(path));
517-
let%opt_wrap (env, suffix) =
518-
getEnvWithOpens(~pos, ~env, ~getModule, ~opens, path);
515+
Log.log("normal " ++ pathToString(path));
516+
switch (getEnvWithOpens(~pos, ~env, ~getModule, ~opens, path)) {
517+
| Some((env, suffix)) =>
519518
Log.log("Got the env");
520519
valueCompletions(~env, suffix);
521-
}
522-
|? []
520+
| None => []
521+
};
523522
| `Attribute(target, suffix) =>
524523
{
525524
Log.log("suffix :" ++ suffix);
@@ -573,16 +572,14 @@ let getItems =
573572
}
574573
|? []
575574
| `AbsAttribute(path) =>
576-
{
577-
let%opt_wrap (env, suffix) =
578-
getEnvWithOpens(~pos, ~env, ~getModule, ~opens, path);
579-
575+
switch (getEnvWithOpens(~pos, ~env, ~getModule, ~opens, path)) {
576+
| None => []
577+
| Some((env, suffix)) =>
580578
attributeCompletions(~env, ~suffix)
581579
@ List.concat(
582580
opens |> List.map(env => attributeCompletions(~env, ~suffix)),
583-
);
581+
)
584582
}
585-
|? []
586583
};
587584
};
588585
};

src/rescript-editor-support/ProcessExtra.re

+27-26
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,20 @@ let getTypeAtPath = (~env, path) => {
6262
| `Exported(env, name) =>
6363
let res = {
6464
let%opt stamp = Hashtbl.find_opt(env.exported.types, name);
65-
let%opt_wrap declaredType =
66-
Hashtbl.find_opt(env.file.stamps.types, stamp);
67-
`Local(declaredType);
65+
let declaredType = Hashtbl.find_opt(env.file.stamps.types, stamp);
66+
switch (declaredType) {
67+
| Some(declaredType) => Some(`Local(declaredType))
68+
| None => None
69+
};
6870
};
6971
res |? `Not_found;
7072
| `Stamp(stamp) =>
7173
let res = {
72-
let%opt_wrap declaredType =
73-
Hashtbl.find_opt(env.file.stamps.types, stamp);
74-
`Local(declaredType);
74+
let declaredType = Hashtbl.find_opt(env.file.stamps.types, stamp);
75+
switch (declaredType) {
76+
| Some(declaredType) => Some(`Local(declaredType))
77+
| None => None
78+
};
7579
};
7680
res |? `Not_found;
7781
};
@@ -141,12 +145,12 @@ module F =
141145
addExternalReference(moduleName, path, tip, identLoc);
142146
GlobalReference(moduleName, path, tip);
143147
| `Exported(env, name) =>
144-
let res = {
145-
let%opt_wrap stamp = Hashtbl.find_opt(env.exported.values, name);
148+
switch (Hashtbl.find_opt(env.exported.values, name)) {
149+
| Some(stamp) =>
146150
addReference(stamp, identLoc);
147151
LocalReference(stamp, tip);
148-
};
149-
res |? NotFound;
152+
| None => NotFound
153+
}
150154
| `GlobalMod(_) => NotFound
151155
};
152156
addLocation(loc, Typed(typ, locType));
@@ -166,12 +170,12 @@ module F =
166170
addExternalReference(moduleName, path, Module, loc);
167171
LModule(GlobalReference(moduleName, path, Module));
168172
| `Exported(env, name) =>
169-
let res = {
170-
let%opt_wrap stamp = Hashtbl.find_opt(env.exported.modules, name);
173+
switch (Hashtbl.find_opt(env.exported.modules, name)) {
174+
| Some(stamp) =>
171175
addReference(stamp, loc);
172176
LModule(LocalReference(stamp, Module));
173-
};
174-
res |? LModule(NotFound);
177+
| None => LModule(NotFound)
178+
}
175179
};
176180
addLocation(loc, locType);
177181
};
@@ -189,13 +193,12 @@ module F =
189193
let locType =
190194
switch (t) {
191195
| `Local({stamp, item: {kind: Record(attributes)}}) =>
192-
{
193-
let%opt_wrap {stamp: astamp} =
194-
attributes |> List.find_opt(a => a.aname.txt == name);
196+
switch (attributes |> List.find_opt(a => a.aname.txt == name)) {
197+
| Some({stamp: astamp}) =>
195198
addReference(astamp, nameLoc);
196199
LocalReference(stamp, Attribute(name));
200+
| None => NotFound
197201
}
198-
|? NotFound
199202
| `Global(moduleName, path) =>
200203
addExternalReference(moduleName, path, Attribute(name), nameLoc);
201204
GlobalReference(moduleName, path, Attribute(name));
@@ -221,13 +224,12 @@ module F =
221224
let locType =
222225
switch (t) {
223226
| `Local({stamp, item: {kind: Record(attributes)}}) =>
224-
{
225-
let%opt_wrap {stamp: astamp} =
226-
attributes |> List.find_opt(a => a.aname.txt == name);
227+
switch (attributes |> List.find_opt(a => a.aname.txt == name)) {
228+
| Some({stamp: astamp}) =>
227229
addReference(astamp, nameLoc);
228230
LocalReference(stamp, Attribute(name));
231+
| None => NotFound
229232
}
230-
|? NotFound
231233
| `Global(moduleName, path) =>
232234
addExternalReference(
233235
moduleName,
@@ -258,13 +260,12 @@ module F =
258260
let locType =
259261
switch (t) {
260262
| `Local({stamp, item: {kind: Variant(constructors)}}) =>
261-
{
262-
let%opt_wrap {stamp: cstamp} =
263-
constructors |> List.find_opt(c => c.cname.txt == cstr_name);
263+
switch (constructors |> List.find_opt(c => c.cname.txt == cstr_name)) {
264+
| Some({stamp: cstamp}) =>
264265
addReference(cstamp, nameLoc);
265266
LocalReference(stamp, Constructor(name));
267+
| None => NotFound
266268
}
267-
|? NotFound
268269
| `Global(moduleName, path) =>
269270
addExternalReference(moduleName, path, Constructor(name), nameLoc);
270271
GlobalReference(moduleName, path, Constructor(name));

src/rescript-editor-support/Query.re

+19-14
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,16 @@ let fromCompilerPath = (~env, path) => {
148148
| `Path(0, moduleName, path) => `Global((moduleName, path))
149149
| `GlobalMod(name) => `GlobalMod(name)
150150
| `Path(stamp, _moduleName, path) =>
151-
let res = {
152-
let%opt {item: kind} =
153-
Hashtbl.find_opt(env.file.stamps.modules, stamp);
154-
let%opt_wrap res = findInModule(~env, kind, path);
155-
switch (res) {
156-
| `Local(env, name) => `Exported((env, name))
157-
| `Global(moduleName, fullPath) => `Global((moduleName, fullPath))
151+
let res =
152+
switch (Hashtbl.find_opt(env.file.stamps.modules, stamp)) {
153+
| None => None
154+
| Some({item: kind}) => findInModule(~env, kind, path)
158155
};
156+
switch (res) {
157+
| None => `Not_found
158+
| Some(`Local(env, name)) => `Exported((env, name))
159+
| Some(`Global(moduleName, fullPath)) => `Global((moduleName, fullPath))
159160
};
160-
res |? `Not_found;
161161
};
162162
};
163163

@@ -188,13 +188,18 @@ let resolveModuleFromCompilerPath = (~env, ~getModule, path) => {
188188
let resolveFromCompilerPath = (~env, ~getModule, path) => {
189189
switch (fromCompilerPath(~env, path)) {
190190
| `Global(moduleName, path) =>
191-
let res = {
192-
let%opt file = getModule(moduleName);
193-
let env = {file, exported: file.contents.exported};
194-
let%opt_wrap (env, name) = resolvePath(~env, ~getModule, ~path);
195-
`Exported((env, name));
191+
let res =
192+
switch (getModule(moduleName)) {
193+
| None => None
194+
| Some(file) =>
195+
let env = {file, exported: file.contents.exported};
196+
resolvePath(~env, ~getModule, ~path);
197+
};
198+
switch (res) {
199+
| None => `Not_found
200+
| Some((env, name)) => `Exported((env, name))
196201
};
197-
res |? `Not_found;
202+
198203
| `Stamp(stamp) => `Stamp(stamp)
199204
| `GlobalMod(_) => `Not_found
200205
| `Not_found => `Not_found

src/rescript-editor-support/References.re

+10-6
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ let localReferencesForLoc = (~file, ~extra, loc) =>
7070
Hashtbl.find_opt(extra.internalReferences, localStamp);
7171
| LModule(GlobalReference(moduleName, path, tip))
7272
| Typed(_, GlobalReference(moduleName, path, tip)) =>
73-
let%opt_wrap refs =
74-
Hashtbl.find_opt(extra.externalReferences, moduleName);
75-
refs
76-
|> Utils.filterMap(((p, t, l)) =>
77-
p == path && t == tip ? Some(l) : None
78-
);
73+
switch (Hashtbl.find_opt(extra.externalReferences, moduleName)) {
74+
| None => None
75+
| Some(refs) =>
76+
Some(
77+
refs
78+
|> Utils.filterMap(((p, t, l)) =>
79+
p == path && t == tip ? Some(l) : None
80+
),
81+
)
82+
}
7983
};
8084

8185
let definedForLoc = (~file, ~getModule, loc) => {

src/rescript-editor-support/State.re

+4-2
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,10 @@ let docsForModule = (modname, state, ~package) =>
299299
let src = SharedTypes.getSrc(paths);
300300
Log.log("FINDING docs for module " ++ SharedTypes.showPaths(paths));
301301
Log.log("FINDING " ++ cmt ++ " src " ++ (src |? ""));
302-
let%opt_wrap docs = docsForCmt(~moduleName=modname, cmt, src, state);
303-
(docs, src);
302+
switch (docsForCmt(~moduleName=modname, cmt, src, state)) {
303+
| None => None
304+
| Some(docs) => Some((docs, src))
305+
};
304306
} else {
305307
Log.log("No path for module " ++ modname);
306308
None;

0 commit comments

Comments
 (0)