Skip to content

Commit bf67cd2

Browse files
committed
Convert Packages to unmonad
1 parent c0c0696 commit bf67cd2

File tree

1 file changed

+135
-115
lines changed

1 file changed

+135
-115
lines changed

src/rescript-editor-support/Packages.re

Lines changed: 135 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -50,104 +50,115 @@ let makePathsForModule =
5050
(pathsForModule, nameForPath);
5151
};
5252

53-
let newBsPackage = rootPath => {
54-
let%try raw = Files.readFileResult(rootPath /+ "bsconfig.json");
55-
let config = Json.parse(raw);
56-
57-
Log.log({|📣 📣 NEW BSB PACKAGE 📣 📣|});
58-
/* failwith("Wat"); */
59-
Log.log("- location: " ++ rootPath);
60-
61-
let compiledBase = BuildSystem.getCompiledBase(rootPath);
62-
let%try (dependencyDirectories, dependencyModules) =
63-
FindFiles.findDependencyFiles(~debug=true, rootPath, config);
64-
let%try_wrap compiledBase =
65-
compiledBase
66-
|> RResult.orError(
67-
"You need to run bsb first so that reason-language-server can access the compiled artifacts.\nOnce you've run bsb, restart the language server.",
68-
);
69-
70-
let namespace = FindFiles.getNamespace(config);
71-
let localSourceDirs =
72-
FindFiles.getSourceDirectories(~includeDev=true, rootPath, config);
73-
Log.log(
74-
"Got source directories " ++ String.concat(" - ", localSourceDirs),
75-
);
76-
let localModules =
77-
FindFiles.findProjectFiles(
78-
~debug=true,
79-
namespace,
80-
rootPath,
81-
localSourceDirs,
82-
compiledBase,
83-
);
84-
/* |> List.map(((name, paths)) => (switch (namespace) {
85-
| None => name
86-
| Some(n) => name ++ "-" ++ n }, paths)); */
87-
Log.log(
88-
"-- All local modules found: "
89-
++ string_of_int(List.length(localModules)),
90-
);
91-
localModules
92-
|> List.iter(((name, paths)) => {
93-
Log.log(name);
94-
switch (paths) {
95-
| SharedTypes.Impl(cmt, _) => Log.log("impl " ++ cmt)
96-
| Intf(cmi, _) => Log.log("intf " ++ cmi)
97-
| _ => Log.log("Both")
98-
};
99-
});
100-
101-
let (pathsForModule, nameForPath) =
102-
makePathsForModule(localModules, dependencyModules);
103-
104-
let opens =
105-
switch (namespace) {
106-
| None => []
107-
| Some(namespace) =>
108-
let cmt = compiledBase /+ namespace ++ ".cmt";
109-
Log.log("############ Namespaced as " ++ namespace ++ " at " ++ cmt);
110-
Hashtbl.add(pathsForModule, namespace, Impl(cmt, None));
111-
[FindFiles.nameSpaceToName(namespace)];
112-
};
113-
Log.log("Dependency dirs " ++ String.concat(" ", dependencyDirectories));
114-
115-
let opens = {
116-
let flags =
117-
MerlinFile.getFlags(rootPath)
118-
|> RResult.withDefault([""])
119-
|> List.map(escapePreprocessingFlags);
120-
let opens =
121-
List.fold_left(
122-
(opens, item) => {
123-
let parts = Utils.split_on_char(' ', item);
124-
let rec loop = items =>
125-
switch (items) {
126-
| ["-open", name, ...rest] => [name, ...loop(rest)]
127-
| [_, ...rest] => loop(rest)
128-
| [] => []
53+
let newBsPackage = rootPath =>
54+
switch (Files.readFileResult(rootPath /+ "bsconfig.json")) {
55+
| Error(e) => Error(e)
56+
| Ok(raw) =>
57+
let config = Json.parse(raw);
58+
59+
Log.log({|📣 📣 NEW BSB PACKAGE 📣 📣|});
60+
/* failwith("Wat"); */
61+
Log.log("- location: " ++ rootPath);
62+
63+
let compiledBase = BuildSystem.getCompiledBase(rootPath);
64+
switch (FindFiles.findDependencyFiles(~debug=true, rootPath, config)) {
65+
| Error(e) => Error(e)
66+
| Ok((dependencyDirectories, dependencyModules)) =>
67+
switch (
68+
compiledBase
69+
|> RResult.orError(
70+
"You need to run bsb first so that reason-language-server can access the compiled artifacts.\nOnce you've run bsb, restart the language server.",
71+
)
72+
) {
73+
| Error(e) => Error(e)
74+
| Ok(compiledBase) =>
75+
Ok(
76+
{
77+
let namespace = FindFiles.getNamespace(config);
78+
let localSourceDirs =
79+
FindFiles.getSourceDirectories(~includeDev=true, rootPath, config);
80+
Log.log(
81+
"Got source directories " ++ String.concat(" - ", localSourceDirs),
82+
);
83+
let localModules =
84+
FindFiles.findProjectFiles(
85+
~debug=true,
86+
namespace,
87+
rootPath,
88+
localSourceDirs,
89+
compiledBase,
90+
);
91+
/* |> List.map(((name, paths)) => (switch (namespace) {
92+
| None => name
93+
| Some(n) => name ++ "-" ++ n }, paths)); */
94+
Log.log(
95+
"-- All local modules found: "
96+
++ string_of_int(List.length(localModules)),
97+
);
98+
localModules
99+
|> List.iter(((name, paths)) => {
100+
Log.log(name);
101+
switch (paths) {
102+
| SharedTypes.Impl(cmt, _) => Log.log("impl " ++ cmt)
103+
| Intf(cmi, _) => Log.log("intf " ++ cmi)
104+
| _ => Log.log("Both")
105+
};
106+
});
107+
108+
let (pathsForModule, nameForPath) =
109+
makePathsForModule(localModules, dependencyModules);
110+
111+
let opens =
112+
switch (namespace) {
113+
| None => []
114+
| Some(namespace) =>
115+
let cmt = compiledBase /+ namespace ++ ".cmt";
116+
Log.log("############ Namespaced as " ++ namespace ++ " at " ++ cmt);
117+
Hashtbl.add(pathsForModule, namespace, Impl(cmt, None));
118+
[FindFiles.nameSpaceToName(namespace)];
119+
};
120+
Log.log("Dependency dirs " ++ String.concat(" ", dependencyDirectories));
121+
122+
let opens = {
123+
let flags =
124+
MerlinFile.getFlags(rootPath)
125+
|> RResult.withDefault([""])
126+
|> List.map(escapePreprocessingFlags);
127+
let opens =
128+
List.fold_left(
129+
(opens, item) => {
130+
let parts = Utils.split_on_char(' ', item);
131+
let rec loop = items =>
132+
switch (items) {
133+
| ["-open", name, ...rest] => [name, ...loop(rest)]
134+
| [_, ...rest] => loop(rest)
135+
| [] => []
136+
};
137+
opens @ loop(parts);
138+
},
139+
opens,
140+
flags,
141+
);
142+
opens;
129143
};
130-
opens @ loop(parts);
131-
},
132-
opens,
133-
flags,
134-
);
135-
opens;
136-
};
137-
138-
let interModuleDependencies = Hashtbl.create(List.length(localModules));
139144

140-
{
141-
rootPath,
142-
localModules: localModules |> List.map(fst),
143-
dependencyModules: dependencyModules |> List.map(fst),
144-
pathsForModule,
145-
nameForPath,
146-
opens,
147-
namespace,
148-
interModuleDependencies,
145+
let interModuleDependencies = Hashtbl.create(List.length(localModules));
146+
147+
{
148+
rootPath,
149+
localModules: localModules |> List.map(fst),
150+
dependencyModules: dependencyModules |> List.map(fst),
151+
pathsForModule,
152+
nameForPath,
153+
opens,
154+
namespace,
155+
interModuleDependencies,
156+
};
157+
},
158+
)
159+
}
160+
};
149161
};
150-
};
151162

152163
let findRoot = (~uri, packagesByRoot) => {
153164
let path = Uri2.toPath(uri);
@@ -173,25 +184,34 @@ let getPackage = (~uri, state) =>
173184
),
174185
);
175186
} else {
176-
let%try root =
187+
switch (
177188
findRoot(~uri, state.packagesByRoot)
178-
|> RResult.orError("No root directory found");
179-
let%try package =
180-
switch (root) {
181-
| `Root(rootPath) =>
182-
Hashtbl.replace(state.rootForUri, uri, rootPath);
183-
Ok(
184-
Hashtbl.find(
185-
state.packagesByRoot,
186-
Hashtbl.find(state.rootForUri, uri),
187-
),
188-
);
189-
| `Bs(rootPath) =>
190-
let%try package = newBsPackage(rootPath);
191-
Hashtbl.replace(state.rootForUri, uri, package.rootPath);
192-
Hashtbl.replace(state.packagesByRoot, package.rootPath, package);
193-
Ok(package);
194-
};
195-
196-
Ok(package);
189+
|> RResult.orError("No root directory found")
190+
) {
191+
| Error(e) => Error(e)
192+
| Ok(root) =>
193+
switch (
194+
switch (root) {
195+
| `Root(rootPath) =>
196+
Hashtbl.replace(state.rootForUri, uri, rootPath);
197+
Ok(
198+
Hashtbl.find(
199+
state.packagesByRoot,
200+
Hashtbl.find(state.rootForUri, uri),
201+
),
202+
);
203+
| `Bs(rootPath) =>
204+
switch (newBsPackage(rootPath)) {
205+
| Error(e) => Error(e)
206+
| Ok(package) =>
207+
Hashtbl.replace(state.rootForUri, uri, package.rootPath);
208+
Hashtbl.replace(state.packagesByRoot, package.rootPath, package);
209+
Ok(package);
210+
}
211+
}
212+
) {
213+
| Error(e) => Error(e)
214+
| Ok(package) => Ok(package)
215+
}
216+
};
197217
};

0 commit comments

Comments
 (0)