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

Commit a4c1821

Browse files
author
Rusty Key
committed
add formatters
1 parent aec1eac commit a4c1821

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
"description":
8787
"The path to the `env` command which prints the language server environment for debugging editor issues."
8888
},
89+
"reason.path.ocamlformat": {
90+
"type": "string",
91+
"default": "ocamlformat",
92+
"description": "The path to the `ocamlformat` binary."
93+
},
8994
"reason.path.ocamlmerlin": {
9095
"type": "string",
9196
"default": "ocamlmerlin",
@@ -96,11 +101,6 @@
96101
"default": null,
97102
"description": "The path to the `ocamlmerlin-lsp` binary."
98103
},
99-
"reason.path.ocpindent": {
100-
"type": "string",
101-
"default": "ocp-indent",
102-
"description": "The path to the `ocp-indent` binary."
103-
},
104104
"reason.path.opam": {
105105
"type": "string",
106106
"default": "opam",

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import * as vscode from "vscode";
44
import * as client from "./client";
5+
import { register as registerOcamlForamtter } from "./formatters/ocaml";
6+
import { register as registerReasonForamtter } from "./formatters/reason";
57

68
const reasonConfiguration = {
79
indentationRules: {
@@ -93,6 +95,8 @@ const reasonConfiguration = {
9395
export async function activate(context: vscode.ExtensionContext) {
9496
context.subscriptions.push(vscode.languages.setLanguageConfiguration("reason", reasonConfiguration));
9597
await client.launch(context);
98+
registerOcamlForamtter();
99+
registerReasonForamtter();
96100
}
97101

98102
export function deactivate() {

src/formatters/ocaml.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { execSync } from "child_process";
2+
import * as path from "path";
3+
import * as vscode from "vscode";
4+
import { getFullTextRange } from "./utils";
5+
6+
export function register() {
7+
const configuration = vscode.workspace.getConfiguration("ocaml-reason-format");
8+
const rootPath = vscode.workspace.rootPath || "";
9+
10+
vscode.languages.registerDocumentFormattingEditProvider("ocaml", {
11+
provideDocumentFormattingEdits(_document: vscode.TextDocument): vscode.TextEdit[] {
12+
const formatterPath = configuration.get<string | undefined>("ocamlformat");
13+
const formatter = formatterPath ? path.resolve(rootPath, formatterPath) : "ocamlformat";
14+
const textEditor = vscode.window.activeTextEditor;
15+
16+
if (textEditor) {
17+
const text = textEditor.document.getText();
18+
const filePath = textEditor.document.fileName;
19+
const formattedText = execSync(`cd ${rootPath} && ${formatter} --name=${filePath} /dev/stdin <<<'${text}'`, {
20+
cwd: rootPath,
21+
}).toString();
22+
const textRange = getFullTextRange(textEditor);
23+
24+
return [vscode.TextEdit.replace(textRange, formattedText)];
25+
} else {
26+
return [];
27+
}
28+
},
29+
});
30+
}

src/formatters/reason.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { execSync } from "child_process";
2+
import * as path from "path";
3+
import * as vscode from "vscode";
4+
import { getFullTextRange } from "./utils";
5+
6+
export function register() {
7+
const configuration = vscode.workspace.getConfiguration("ocaml-reason-format");
8+
const rootPath = vscode.workspace.rootPath || "";
9+
10+
vscode.languages.registerDocumentFormattingEditProvider("reason", {
11+
provideDocumentFormattingEdits(_document: vscode.TextDocument): vscode.TextEdit[] {
12+
const formatterPath = configuration.get<string | undefined>("refmt");
13+
const formatter = formatterPath ? path.resolve(rootPath, formatterPath) : "refmt";
14+
const textEditor = vscode.window.activeTextEditor;
15+
16+
if (textEditor) {
17+
const text = textEditor.document.getText();
18+
const formattedText = execSync(`${formatter} <<<'${text}'`).toString();
19+
const textRange = getFullTextRange(textEditor);
20+
21+
return [vscode.TextEdit.replace(textRange, formattedText)];
22+
} else {
23+
return [];
24+
}
25+
},
26+
});
27+
}

src/formatters/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as vscode from "vscode";
2+
3+
export function getFullTextRange(textEditor: vscode.TextEditor) {
4+
const firstLine = textEditor.document.lineAt(0);
5+
const lastLine = textEditor.document.lineAt(textEditor.document.lineCount - 1);
6+
7+
return new vscode.Range(
8+
0,
9+
firstLine.range.start.character,
10+
textEditor.document.lineCount - 1,
11+
lastLine.range.end.character,
12+
);
13+
}

0 commit comments

Comments
 (0)