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

Commit 32fe6d3

Browse files
author
Rusty Key
committed
handle possible errors
1 parent c705687 commit 32fe6d3

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

src/formatters/ocaml.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { execSync } from "child_process";
22
import * as fs from "fs";
3-
import * as path from "path";
43
import { v4 as uuidv4 } from "uuid";
54
import * as vscode from "vscode";
6-
import { getFullTextRange } from "./utils";
5+
import { getFormatter, getFullTextRange } from "./utils";
76

87
export function register() {
98
const configuration = vscode.workspace.getConfiguration("reason");
@@ -13,21 +12,28 @@ export function register() {
1312
{ scheme: "file", language: "ocaml" },
1413
{
1514
provideDocumentFormattingEdits(_document: vscode.TextDocument): vscode.TextEdit[] {
16-
const formatterPath = configuration.get<string | undefined>("path.ocamlformat");
17-
const formatter = formatterPath ? path.resolve(rootPath, formatterPath) : "ocamlformat";
1815
const textEditor = vscode.window.activeTextEditor;
16+
const formatter = getFormatter(configuration, "ocamlformat");
17+
18+
console.log(formatter);
19+
20+
if (!formatter) return [];
1921

2022
if (textEditor) {
2123
const tempFileName = `/tmp/vscode-reasonml-${uuidv4()}.ml`;
2224
fs.writeFileSync(tempFileName, textEditor.document.getText(), "utf8");
23-
const filePath = textEditor.document.fileName;
24-
const formattedText = execSync(
25-
`cd ${rootPath} && ${formatter} --name=${filePath} ${tempFileName}`,
26-
).toString();
27-
fs.unlinkSync(tempFileName);
28-
29-
const textRange = getFullTextRange(textEditor);
30-
return [vscode.TextEdit.replace(textRange, formattedText)];
25+
try {
26+
const filePath = textEditor.document.fileName;
27+
const formattedText = execSync(
28+
`cd ${rootPath} && ${formatter} --name=${filePath} ${tempFileName}`,
29+
).toString();
30+
const textRange = getFullTextRange(textEditor);
31+
fs.unlinkSync(tempFileName);
32+
return [vscode.TextEdit.replace(textRange, formattedText)];
33+
} catch (e) {
34+
fs.unlinkSync(tempFileName);
35+
return [];
36+
}
3137
} else {
3238
return [];
3339
}

src/formatters/reason.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import { execSync } from "child_process";
22
import * as fs from "fs";
3-
import * as path from "path";
43
import { v4 as uuidv4 } from "uuid";
54
import * as vscode from "vscode";
6-
import { getFullTextRange } from "./utils";
5+
import { getFormatter, getFullTextRange } from "./utils";
76

87
export function register() {
98
const configuration = vscode.workspace.getConfiguration("reason");
10-
const rootPath = vscode.workspace.rootPath || "";
119

1210
vscode.languages.registerDocumentFormattingEditProvider(
1311
{ scheme: "file", language: "reason" },
1412
{
1513
provideDocumentFormattingEdits(_document: vscode.TextDocument): vscode.TextEdit[] {
16-
const formatterPath = configuration.get<string | undefined>("path.refmt");
17-
const formatter = formatterPath ? path.resolve(rootPath, formatterPath) : "/usr/local/bin/refmt";
1814
const textEditor = vscode.window.activeTextEditor;
15+
const formatter = getFormatter(configuration, "refmt");
16+
17+
if (!formatter) return [];
18+
1919
if (textEditor) {
2020
const tempFileName = `/tmp/vscode-refmt-${uuidv4()}.re`;
2121
fs.writeFileSync(tempFileName, textEditor.document.getText(), "utf8");
22-
const formattedText = execSync(`${formatter} ${tempFileName}`).toString();
23-
const textRange = getFullTextRange(textEditor);
24-
fs.unlinkSync(tempFileName);
25-
26-
return [vscode.TextEdit.replace(textRange, formattedText)];
22+
try {
23+
const formattedText = execSync(`${formatter} ${tempFileName}`).toString();
24+
const textRange = getFullTextRange(textEditor);
25+
fs.unlinkSync(tempFileName);
26+
return [vscode.TextEdit.replace(textRange, formattedText)];
27+
} catch (e) {
28+
fs.unlinkSync(tempFileName);
29+
return [];
30+
}
2731
} else {
2832
return [];
2933
}

src/formatters/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { execSync } from "child_process";
2+
import * as path from "path";
13
import * as vscode from "vscode";
24

35
export function getFullTextRange(textEditor: vscode.TextEditor) {
@@ -11,3 +13,27 @@ export function getFullTextRange(textEditor: vscode.TextEditor) {
1113
lastLine.range.end.character,
1214
);
1315
}
16+
17+
function getExecutablePath(executable: string) {
18+
try {
19+
return execSync(`which ${executable}`).toString();
20+
} catch (_e) {
21+
return null;
22+
}
23+
}
24+
25+
export function getFormatter(configuration: vscode.WorkspaceConfiguration, formatterName: string) {
26+
const rootPath = vscode.workspace.rootPath || "";
27+
const formatterPath = configuration.get<string | undefined>(`path.${formatterName}`) || formatterName;
28+
29+
const formatter =
30+
formatterPath === formatterName ? getExecutablePath(formatterName) : path.resolve(rootPath, formatterPath);
31+
32+
if (!formatter) {
33+
vscode.window.showInformationMessage(
34+
`${formatterPath} is not available. Please specify "vscode-reasonml.path.${formatterName}"`,
35+
);
36+
}
37+
38+
return formatter;
39+
}

0 commit comments

Comments
 (0)