Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit 50023a6

Browse files
committed
add local opam switch support
1 parent 9ae3159 commit 50023a6

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/client/index.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ function isEsyConfiguredProperly(esyConfig: any) {
4949
});
5050
}
5151

52-
function isConfuguredProperly(esyConfig: any) {
52+
function isConfiguredProperly(esyConfig: any, opamConfig: any) {
5353
if (esyConfig) {
5454
return isEsyConfiguredProperly(esyConfig);
5555
}
5656

5757
if (isBucklescriptProject()) return true;
5858

59+
if (!!opamConfig) return true;
60+
5961
vscode.window.showInformationMessage(
6062
"LSP is unable to start. Extension couldn't detect type of the project. Provide esy or bucklescript configuration. More in README.",
6163
);
@@ -64,38 +66,46 @@ function isConfuguredProperly(esyConfig: any) {
6466

6567
export async function launch(context: vscode.ExtensionContext): Promise<void> {
6668
const esyConfig = await getEsyConfig();
69+
const opamConfig = await getOpamConfig();
6770

68-
if (!isConfuguredProperly(esyConfig)) return;
71+
if (!isConfiguredProperly(esyConfig, opamConfig)) return;
6972

7073
return launchMerlinLsp(context, {
7174
useEsy: !!esyConfig,
75+
useOpam: !!opamConfig,
7276
});
7377
}
7478

7579
function getPrebuiltExecutablesPath() {
7680
return path.join(__dirname, `../../../executables/${process.platform}`);
7781
}
7882

79-
function getMerlinLspPath(useEsy: boolean) {
83+
function getMerlinLspPath({ useEsy: boolean, useOpam: boolean }) {
8084
let merlinLspPath = isWin ? "ocamlmerlin-lsp.exe" : "ocamlmerlin-lsp";
8185

82-
if (!useEsy) {
86+
if (!useEsy && !useOpam) {
8387
merlinLspPath = path.join(getPrebuiltExecutablesPath(), merlinLspPath);
8488
}
8589

8690
return merlinLspPath;
8791
}
8892

89-
function getMerlinLspOptions(options: { useEsy: boolean }) {
90-
const merlinLsp = getMerlinLspPath(options.useEsy);
91-
const pth = options.useEsy ? process.env.PATH : `${getPrebuiltExecutablesPath()}:${process.env.PATH}`;
93+
function getMerlinLspOptions(options: { useEsy: boolean, useOpam: boolean }) {
94+
const merlinLsp = getMerlinLspPath(options);
95+
const usePkgManager = options.useEsy || options.useOpam;
96+
const pth = usePkgManager ? process.env.PATH : `${getPrebuiltExecutablesPath()}:${process.env.PATH}`;
9297

9398
let run;
9499
if (options.useEsy) {
95100
run = {
96101
args: ["exec-command", "--include-current-env", merlinLsp],
97102
command: process.platform === "win32" ? "esy.cmd" : "esy",
98103
};
104+
} else {
105+
run = {
106+
args: ["exec-command", "exec", "--", merlinLsp],
107+
command: "opam",
108+
};
99109
} else {
100110
run = {
101111
args: [],
@@ -132,7 +142,7 @@ function getMerlinLspOptions(options: { useEsy: boolean }) {
132142
return serverOptions;
133143
}
134144

135-
export async function launchMerlinLsp(context: vscode.ExtensionContext, options: { useEsy: boolean }): Promise<void> {
145+
export async function launchMerlinLsp(context: vscode.ExtensionContext, options: { useEsy: boolean, useOpam: boolean }): Promise<void> {
136146
const serverOptions = getMerlinLspOptions(options);
137147
const reasonConfig = vscode.workspace.getConfiguration("reason");
138148

src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ export async function getEsyConfig() {
4545
}
4646
}
4747

48+
export async function getOpamConfig() {
49+
const root = vscode.workspace.rootPath;
50+
if (root == null) {
51+
return false;
52+
}
53+
54+
let configFile = path.join(root, "_opam");
55+
const isConfigFileExists = await exists(configFile);
56+
if (!isConfigFileExists) {
57+
return null;
58+
}
59+
return configFile;
60+
}
61+
4862
export function getFullTextRange(textEditor: vscode.TextEditor) {
4963
const firstLine = textEditor.document.lineAt(0);
5064
const lastLine = textEditor.document.lineAt(textEditor.document.lineCount - 1);

0 commit comments

Comments
 (0)