Skip to content
This repository has been archived by the owner on Oct 11, 2020. It is now read-only.

Add support for multi workspace workflow #11

Draft
wants to merge 1 commit into
base: prometheansacrifice/reason-rewrite
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
"--extensionTestsPath=${workspaceFolder}/test/suite/index.js"
],
"outFiles": ["${workspaceFolder}/out/test/**/*.js"]
}
},
{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/src/**/*.bs.js"
]
}
]
}
}
3 changes: 0 additions & 3 deletions bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"react-jsx": 3
},
"sources": [
{
"dir": "vendor"
},
{
"subdirs": true,
"dir": "src"
Expand Down
18 changes: 9 additions & 9 deletions src/Extension.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 22 additions & 18 deletions src/Extension.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@ open LSP;

let createClient = (~id, ~name, ~folder) =>
Js.Promise.(
Server.make(folder)
Server.make(folder.Folder.uri.fsPath)
|> then_(serverOptions => {
LanguageClient.make(
~id,
~name,
~serverOptions,
~clientOptions=Client.make(),
Ok(
LanguageClient.make(
~id,
~name,
~serverOptions,
~clientOptions=Client.make(),
),
)
|> resolve
})
);

let activate = _context => {
Js.Promise.(
createClient(
~id="merlin-language-server",
~name="Merlin Language Server",
~folder=Workspace.rootPath,
let activate = context => {
MultiWorkspace.start(
~context, ~commands=[||], ~createClient=(_document, folder) => {
Js.Promise.(
createClient(
~id="merlin-language-server",
~name="Merlin Language Server",
~folder,
)
|> catch(e => {
let message = Bindings.Error.ofPromiseError(e);
Js.Promise.resolve(Error(message));
})
)
|> then_((client: LanguageClient.t) => client.start(.) |> resolve)
|> catch(e => {
let message = Bindings.Error.ofPromiseError(e);
Window.showErrorMessage({j|Error: $message|j});
})
);
});
};
77 changes: 74 additions & 3 deletions src/LSP.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions src/LSP.re
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,80 @@ module LanguageClient = {
t =
"LanguageClient";
};

module MultiWorkspace = {
module FoldersMap = Belt.HashMap.String;
let start = (~context, ~commands, ~createClient): Js.Promise.t(unit) => {
let workspaceFolders = FoldersMap.make(~hintSize=1);
let startClient = (document: TextDocument.event) => {
let uri = document.uri;
if (uri.scheme === "file") {
let folder = Workspace.getWorkspaceFolder(uri);
switch (folder) {
| Some(folder)
when FoldersMap.has(workspaceFolders, folder.uri.fsPath) =>
Js.Promise.resolve()
| Some(folder) =>
createClient(document, folder)
|> Js.Promise.then_(client =>
switch (client) {
| Ok(client) =>
FoldersMap.set(
workspaceFolders,
Folder.key(folder),
client,
);
client.LanguageClient.start(.) |> Js.Promise.resolve;
| Error(message) =>
Window.showErrorMessage({j|Error: $message|j})
}
)
| None => Js.Promise.resolve()
};
} else {
Js.Promise.resolve();
};
};
Workspace.onDidOpenTextDocument(document =>
startClient(document) |> ignore
);
let openTasks =
Workspace.textDocuments
|> Js.Array.map(startClient)
|> Js.Promise.all
|> Js.Promise.then_(_ => Js.Promise.resolve());
Workspace.onDidChangeWorkspaceFolders(event => {
event.removed
|> Js.Array.forEach(folder => {
switch (FoldersMap.get(workspaceFolders, Folder.key(folder))) {
| Some(client) =>
FoldersMap.remove(workspaceFolders, Folder.key(folder));
client.LanguageClient.stop(.);
| None => ()
}
})
});

commands
|> Js.Array.forEach(((cmd, handler)) => {
Commands.register(~command=cmd, ~handler)
});

ExtensionContext.(
Js.Array.push(
{
dispose:
(.) => {
FoldersMap.forEach(workspaceFolders, (_, client) => {
client.LanguageClient.stop(.)
});
FoldersMap.clear(workspaceFolders);
},
},
context.subscriptions,
)
)
|> ignore;
openTasks;
};
};
2 changes: 1 addition & 1 deletion src/bindings/Node.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ external processEnv: Js.Dict.t(string) = "env";
module Error = {
type t;
[@bs.new] external make: string => t = "Error";
let ofPromiseError = [%raw
let ofPromiseError: Js.Promise.error => string = [%raw
error => "return error.message || 'Unknown error'"
];
};
Expand Down