From 5789cae5a9cca7f9b41009077c3bb5688d9ecf9e Mon Sep 17 00:00:00 2001 From: Cheng Lou Date: Wed, 7 Apr 2021 03:51:59 -0700 Subject: [PATCH 1/2] Remove unnecessary monadic infixes --- src/MessageHandlers.re | 264 ++++++++++++++++++----------------------- src/NewCompletions.re | 86 +++++++------- src/ProcessExtra.re | 28 ++--- src/References.re | 169 +++++++++++++------------- 4 files changed, 250 insertions(+), 297 deletions(-) diff --git a/src/MessageHandlers.re b/src/MessageHandlers.re index dd4029a8..4930312c 100644 --- a/src/MessageHandlers.re +++ b/src/MessageHandlers.re @@ -1,6 +1,5 @@ open RResult; open TopTypes; -open Infix; module J = JsonShort; let handlers: @@ -15,31 +14,26 @@ let handlers: | Error(e) => Error(e) | Ok((package, {file, extra})) => let position = Utils.cmtLocFromVscode(pos); - ( - switch ( - References.definitionForPos( - ~pathsForModule=package.pathsForModule, - ~file, - ~extra, - ~getUri=State.fileForUri(state), - ~getModule=State.fileForModule(state, ~package), - position, - ) - ) { - | None => None - | Some((uri, loc)) => - Some( - Ok(( - state, - Json.Object([ - ("uri", Json.String(Uri2.toString(uri))), - ("range", Protocol.rangeOfLoc(loc)), - ]), - )), - ); - } - ) - |? Ok((state, Json.Null)); + switch ( + References.definitionForPos( + ~pathsForModule=package.pathsForModule, + ~file, + ~extra, + ~getUri=State.fileForUri(state), + ~getModule=State.fileForModule(state, ~package), + position, + ) + ) { + | None => Ok((state, Json.Null)) + | Some((uri, loc)) => + Ok(( + state, + Json.Object([ + ("uri", Json.String(Uri2.toString(uri))), + ("range", Protocol.rangeOfLoc(loc)), + ]), + )) + } } } } @@ -114,53 +108,44 @@ let handlers: switch (State.fileForUri(state, uri)) { | Error(e) => Error(e) | Ok((file, extra)) => - Ok( - Infix.( - ( - switch ( - References.locForPos(~extra, Utils.cmtLocFromVscode(pos)) - ) { - | None => None - | Some((_, loc)) => - switch ( - References.allReferencesForLoc( - ~pathsForModule=package.pathsForModule, - ~file, - ~extra, - ~allModules=package.localModules, - ~getUri=State.fileForUri(state), - ~getModule=State.fileForModule(state, ~package), - ~getExtra=State.extraForModule(state, ~package), - loc, - ) - |> toOptionAndLog - ) { - | None => None - | Some(allReferences) => - Some(( - state, - J.l( - allReferences - |> List.map(((fname, references)) => { - let locs = - fname == uri - ? List.filter( - loc => !Protocol.locationContains(loc, pos), - references, - ) - : references; - locs - |> List.map(loc => Protocol.locationOfLoc(~fname, loc)); - }) - |> List.concat, - ), - )); - } - } + switch (References.locForPos(~extra, Utils.cmtLocFromVscode(pos))) { + | None => Ok((state, J.null)) + | Some((_, loc)) => + switch ( + References.allReferencesForLoc( + ~pathsForModule=package.pathsForModule, + ~file, + ~extra, + ~allModules=package.localModules, + ~getUri=State.fileForUri(state), + ~getModule=State.fileForModule(state, ~package), + ~getExtra=State.extraForModule(state, ~package), + loc, ) - |? (state, J.null) - ), - ) + |> toOptionAndLog + ) { + | None => Ok((state, J.null)) + | Some(allReferences) => + Ok(( + state, + J.l( + allReferences + |> List.map(((fname, references)) => { + let locs = + fname == uri + ? List.filter( + loc => !Protocol.locationContains(loc, pos), + references, + ) + : references; + locs + |> List.map(loc => Protocol.locationOfLoc(~fname, loc)); + }) + |> List.concat, + ), + )); + } + }; } } } @@ -181,59 +166,51 @@ let handlers: switch (RJson.get("newName", params)) { | Error(e) => Error(e) | Ok(newName) => - open Infix; - ( + switch (References.locForPos(~extra, Utils.cmtLocFromVscode(pos))) { + | None => Ok((state, J.null)) + | Some((_, loc)) => switch ( - References.locForPos(~extra, Utils.cmtLocFromVscode(pos)) + References.allReferencesForLoc( + ~file, + ~extra, + ~pathsForModule=package.pathsForModule, + ~allModules=package.localModules, + ~getModule=State.fileForModule(state, ~package), + ~getUri=State.fileForUri(state), + ~getExtra=State.extraForModule(state, ~package), + loc, + ) + |> toOptionAndLog ) { - | None => None - | Some((_, loc)) => - switch ( - References.allReferencesForLoc( - ~file, - ~extra, - ~pathsForModule=package.pathsForModule, - ~allModules=package.localModules, - ~getModule=State.fileForModule(state, ~package), - ~getUri=State.fileForUri(state), - ~getExtra=State.extraForModule(state, ~package), - loc, - ) - |> toOptionAndLog - ) { - | None => None - | Some(allReferences) => - Some( - Ok(( - state, - J.o([ - ( - "changes", - J.o( - allReferences - |> List.map(((fname, references)) => - ( - fname |> Uri2.toString, - J.l( - references - |> List.map(loc => - J.o([ - ( "range", Protocol.rangeOfLoc(loc)), - ("newText", newName), - ]) - ), + | None => Ok((state, J.null)) + | Some(allReferences) => + Ok(( + state, + J.o([ + ( + "changes", + J.o( + allReferences + |> List.map(((fname, references)) => + ( + fname |> Uri2.toString, + J.l( + references + |> List.map(loc => + J.o([ + ( "range", Protocol.rangeOfLoc(loc)), + ("newText", newName), + ]) ), - ) ), - ), - ), - ]), + ) + ), + ), ), - )) - } + ]), + )) } - ) - |? Ok((state, J.null)) + } } } } @@ -336,33 +313,28 @@ let handlers: switch (State.fileForUri(state, uri)) { | Error(e) => Error(e) | Ok((file, extra)) => - { - let pos = Utils.cmtLocFromVscode(pos); - switch (References.locForPos(~extra, pos)) { - | None => None - | Some((location, loc)) => - switch ( - Hover.newHover( - ~file, - ~getModule=State.fileForModule(state, ~package), - loc, - ) - ) { - | None => None - | Some(text) => - Some( - Ok(( - state, - J.o([ - ("range", Protocol.rangeOfLoc(location)), - ("contents", text |> Protocol.contentKind), - ]), - ), - )) - } - }; - } - |? Ok((state, J.null)) + let pos = Utils.cmtLocFromVscode(pos); + switch (References.locForPos(~extra, pos)) { + | None => Ok((state, J.null)) + | Some((location, loc)) => + switch ( + Hover.newHover( + ~file, + ~getModule=State.fileForModule(state, ~package), + loc, + ) + ) { + | None => Ok((state, J.null)) + | Some(text) => + Ok(( + state, + J.o([ + ("range", Protocol.rangeOfLoc(location)), + ("contents", text |> Protocol.contentKind), + ]), + )) + } + }; } } } diff --git a/src/NewCompletions.re b/src/NewCompletions.re index 4f5dc6ce..46932c54 100644 --- a/src/NewCompletions.re +++ b/src/NewCompletions.re @@ -1,7 +1,7 @@ open SharedTypes; -open Infix; let showConstructor = ({cname: {txt}, args, res}) => { + open Infix; txt ++ ( args == [] @@ -500,7 +500,6 @@ let getItems = ); locallyDefinedValues @ valuesFromOpens @ localModuleNames; | multiple => - open Infix; Log.log("Completing for " ++ String.concat("<.>", multiple)); switch (determineCompletion(multiple)) { @@ -516,65 +515,61 @@ let getItems = { Log.log("suffix :" ++ suffix); switch (target) { - | [] => None + | [] => [] | [first, ...rest] => Log.log("-------------- Looking for " ++ first); switch (Query.findInScope(pos, first, env.file.stamps.values)) { - | None => None + | None => [] | Some(declared) => Log.log("Found it! " ++ declared.name.txt); switch (declared.item |> Shared.digConstructor) { - | None => None + | None => [] | Some(path) => switch (Hover.digConstructor(~env, ~getModule, path)) { - | None => None + | None => [] | Some((env, typ)) => switch ( rest - |> List.fold_left( - (current, name) => - switch (current) { - | None => None - | Some((env, typ)) => - switch (typ.item.SharedTypes.Type.kind) { - | Record(fields) => - switch ( - fields - |> List.find_opt(f => f.fname.txt == name) - ) { - | None => None - | Some(attr) => - Log.log("Found attr " ++ name); - switch (attr.typ |> Shared.digConstructor) { - | None => None - | Some(path) => - Hover.digConstructor(~env, ~getModule, path) - }; - } - | _ => None - } - }, - Some((env, typ)), - ) + |> List.fold_left( (current, name) => + switch (current) { + | None => None + | Some((env, typ)) => + switch (typ.item.SharedTypes.Type.kind) { + | Record(fields) => + switch ( + fields + |> List.find_opt(f => f.fname.txt == name) + ) { + | None => None + | Some(attr) => + Log.log("Found attr " ++ name); + switch (attr.typ |> Shared.digConstructor) { + | None => None + | Some(path) => + Hover.digConstructor(~env, ~getModule, path) + }; + } + | _ => None + } + }, + Some((env, typ)), + ) ) { - | None => None + | None => [] | Some((env, typ)) => switch (typ.item.kind) { | Record(fields) => - Some( - fields - |> Utils.filterMap(f => - if (Utils.startsWith(f.fname.txt, suffix)) { - Some(( - env.file.uri, - {...emptyDeclared(f.fname.txt), item: Field(f, typ)}, - )); - } else { - None; - } - ), + fields |> Utils.filterMap(f => + if (Utils.startsWith(f.fname.txt, suffix)) { + Some(( + env.file.uri, + {...emptyDeclared(f.fname.txt), item: Field(f, typ)}, + )); + } else { + None; + } ) - | _ => None + | _ => [] } } } @@ -582,7 +577,6 @@ let getItems = }; }; } - |? [] | `AbsAttribute(path) => switch (getEnvWithOpens(~pos, ~env, ~getModule, ~opens, path)) { | None => [] diff --git a/src/ProcessExtra.re b/src/ProcessExtra.re index 6d033dc8..2f3c7f65 100644 --- a/src/ProcessExtra.re +++ b/src/ProcessExtra.re @@ -1,6 +1,5 @@ open Typedtree; open SharedTypes; -open Infix; let handleConstructor = (path, txt) => { let typeName = @@ -61,26 +60,21 @@ let getTypeAtPath = (~env, path) => { | `Global(moduleName, path) => `Global((moduleName, path)) | `Not_found => `Not_found | `Exported(env, name) => - let res = - switch (Hashtbl.find_opt(env.exported.types, name)) { - | None => None - | Some(stamp) => - let declaredType = Hashtbl.find_opt(env.file.stamps.types, stamp); - switch (declaredType) { - | Some(declaredType) => Some(`Local(declaredType)) - | None => None - }; - }; - res |? `Not_found; - | `Stamp(stamp) => - let res = { + switch (Hashtbl.find_opt(env.exported.types, name)) { + | None => `Not_found + | Some(stamp) => let declaredType = Hashtbl.find_opt(env.file.stamps.types, stamp); switch (declaredType) { - | Some(declaredType) => Some(`Local(declaredType)) - | None => None + | Some(declaredType) => `Local(declaredType) + | None => `Not_found }; + } + | `Stamp(stamp) => + let declaredType = Hashtbl.find_opt(env.file.stamps.types, stamp); + switch (declaredType) { + | Some(declaredType) => `Local(declaredType) + | None => `Not_found }; - res |? `Not_found; }; }; diff --git a/src/References.re b/src/References.re index 4d520b7a..90234596 100644 --- a/src/References.re +++ b/src/References.re @@ -311,109 +311,102 @@ let forLocalStamp = switch (Hashtbl.find_opt(extra.internalReferences, localStamp)) { | None => None | Some(local) => - open Infix; + maybeLog("Checking externals: " ++ string_of_int(stamp)); let externals = - { - maybeLog("Checking externals: " ++ string_of_int(stamp)); - switch (Query.declaredForTip(~stamps=env.file.stamps, stamp, tip)) { - | None => None - | Some(declared) => - if (isVisible(declared)) { - /** - if this file has a corresponding interface or implementation file - also find the references in that file. - */ - let alternativeReferences = - ( - switch ( - alternateDeclared( - ~pathsForModule, - ~file, - ~getUri, - declared, - tip, - ) - ) { - | None => None - | Some((file, extra, {stamp})) => - switch ( - switch (tip) { - | Constructor(name) => - Query.getConstructor(file, stamp, name) |?>> (x => x.stamp) - | Field(name) => - Query.getField(file, stamp, name) |?>> (x => x.stamp) - | _ => Some(stamp) - } - ) { - | None => None - | Some(localStamp) => - switch (Hashtbl.find_opt(extra.internalReferences, localStamp)) { - | None => None - | Some(local) => Some([(file.uri, local)]) - } - } - } - ) - |? []; + switch (Query.declaredForTip(~stamps=env.file.stamps, stamp, tip)) { + | None => [] + | Some(declared) => + if (isVisible(declared)) { + /** + if this file has a corresponding interface or implementation file + also find the references in that file. + */ + let alternativeReferences = switch ( - pathFromVisibility(declared.modulePath, declared.name.txt) + alternateDeclared( + ~pathsForModule, + ~file, + ~getUri, + declared, + tip, + ) ) { - | None => None - | Some(path) => - maybeLog("Now checking path " ++ pathToString(path)); - let thisModuleName = file.moduleName; - let externals = - allModules - |> List.filter(name => name != file.moduleName) - |> Utils.filterMap(name => - { + | None => [] + | Some((file, extra, {stamp})) => + switch ( + switch (tip) { + | Constructor(name) => + Query.getConstructor(file, stamp, name) |?>> (x => x.stamp) + | Field(name) => + Query.getField(file, stamp, name) |?>> (x => x.stamp) + | _ => Some(stamp) + } + ) { + | None => [] + | Some(localStamp) => + switch (Hashtbl.find_opt(extra.internalReferences, localStamp)) { + | None => [] + | Some(local) => [(file.uri, local)] + } + } + }; + switch ( + pathFromVisibility(declared.modulePath, declared.name.txt) + ) { + | None => [] + | Some(path) => + maybeLog("Now checking path " ++ pathToString(path)); + let thisModuleName = file.moduleName; + let externals = + allModules + |> List.filter(name => name != file.moduleName) + |> Utils.filterMap(name => + { + switch ( + getModule(name) + |> RResult.orError( + "Could not get file for module " ++ name, + ) + ) { + | Error(e) => Error(e) + | Ok(file) => switch ( - getModule(name) + getExtra(name) |> RResult.orError( - "Could not get file for module " ++ name, + "Could not get extra for module " ++ name, ) ) { | Error(e) => Error(e) - | Ok(file) => + | Ok(extra) => switch ( - getExtra(name) + Hashtbl.find_opt(extra.externalReferences, thisModuleName) |> RResult.orError( - "Could not get extra for module " ++ name, + "No references in " + ++ name + ++ " for " + ++ thisModuleName, ) ) { | Error(e) => Error(e) - | Ok(extra) => - switch ( - Hashtbl.find_opt(extra.externalReferences, thisModuleName) - |> RResult.orError( - "No references in " - ++ name - ++ " for " - ++ thisModuleName, - ) - ) { - | Error(e) => Error(e) - | Ok(refs) => - let refs = - refs - |> Utils.filterMap(((p, t, l)) => - p == path && t == tip ? Some(l) : None - ); - Ok((file.uri, refs)); - } + | Ok(refs) => + let refs = + refs + |> Utils.filterMap(((p, t, l)) => + p == path && t == tip ? Some(l) : None + ); + Ok((file.uri, refs)); } } - } |> RResult.toOptionAndLog - ); - Some(alternativeReferences @ externals); - }; - } else { - maybeLog("Not visible"); - Some([]); - } - }; - } - |? []; + } + } |> RResult.toOptionAndLog + ); + alternativeReferences @ externals; + }; + } else { + maybeLog("Not visible"); + []; + } + }; Some([(file.uri, local), ...externals]); } }; From c1a1988a6ee0c47a9f0dff8b49d19cdf444929a3 Mon Sep 17 00:00:00 2001 From: Cheng Lou Date: Wed, 7 Apr 2021 04:00:58 -0700 Subject: [PATCH 2/2] Remove more unnecessary monadic infixes --- src/FindFiles.re | 1 - src/Infix.re | 6 ---- src/ProcessCmt.re | 53 ++++++++++++++++++------------------ src/RescriptEditorSupport.re | 5 +++- 4 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/FindFiles.re b/src/FindFiles.re index 8a63e71b..0a327a03 100644 --- a/src/FindFiles.re +++ b/src/FindFiles.re @@ -299,7 +299,6 @@ let findDependencyFiles = (~debug, base, config) => { let namespace = getNamespace(inner); let directories = getSourceDirectories(~includeDev=false, loc, inner); - /* |! "No compiled base found"; */ switch (BuildSystem.getCompiledBase(loc)) { | None => None | Some(compiledBase) => diff --git a/src/Infix.re b/src/Infix.re index f71e1a30..eec5f46b 100644 --- a/src/Infix.re +++ b/src/Infix.re @@ -46,12 +46,6 @@ let fold = (o, d, f) => | Some(v) => f(v) }; -let (|?<) = (o, fn) => - switch (o) { - | None => () - | Some(v) => fn(v) - }; - let logIfAbsent = (message, x) => switch (x) { | None => diff --git a/src/ProcessCmt.re b/src/ProcessCmt.re index 7a079e4a..d2bcc0ac 100644 --- a/src/ProcessCmt.re +++ b/src/ProcessCmt.re @@ -512,33 +512,32 @@ and forModule = (env, mod_desc, moduleName) => let contents = forStructure(~env, structure.str_items); Structure(contents); | Tmod_functor(ident, argName, maybeType, resultExpr) => - maybeType - |?< ( - t => - forTreeModuleType(~env, t) - |?< ( - kind => { - let stamp = Ident.binding_time(ident); - let declared = - ProcessAttributes.newDeclared( - ~item=kind, - ~name=argName, - ~scope={ - Location.loc_start: t.mty_loc.loc_end, - loc_end: env.scope.loc_end, - loc_ghost: false, - }, - ~extent=t.Typedtree.mty_loc, - ~stamp, - ~modulePath=NotVisible, - ~processDoc=env.processDoc, - false, - [], - ); - Hashtbl.add(env.stamps.modules, stamp, declared); - } - ) - ); + switch (maybeType) { + | None => () + | Some(t) => + switch (forTreeModuleType(~env, t)) { + | None => () + | Some(kind) => + let stamp = Ident.binding_time(ident); + let declared = + ProcessAttributes.newDeclared( + ~item=kind, + ~name=argName, + ~scope={ + Location.loc_start: t.mty_loc.loc_end, + loc_end: env.scope.loc_end, + loc_ghost: false, + }, + ~extent=t.Typedtree.mty_loc, + ~stamp, + ~modulePath=NotVisible, + ~processDoc=env.processDoc, + false, + [], + ); + Hashtbl.add(env.stamps.modules, stamp, declared); + } + }; forModule(env, resultExpr.mod_desc, moduleName); | Tmod_apply(functor_, _arg, _coercion) => forModule(env, functor_.mod_desc, moduleName) diff --git a/src/RescriptEditorSupport.re b/src/RescriptEditorSupport.re index 60c0b2c6..60f44e10 100644 --- a/src/RescriptEditorSupport.re +++ b/src/RescriptEditorSupport.re @@ -130,7 +130,10 @@ let main = () => { ~getInitialState, ); Log.log("Finished"); - Log.out^ |?< close_out; + switch (Log.out^) { + | None => () + | Some(out) => close_out(out) + } | (_opts, ["dump", ...files]) => EditorSupportCommands.dump(files) | (_opts, ["complete", pathWithPos, currentFile]) => EditorSupportCommands.complete(~pathWithPos, ~currentFile)