Skip to content

Commit 5133092

Browse files
author
tanton
committed
Merge branch 'main' of github.com:c3lang/vscode-c3
2 parents 82a7fc7 + 103efed commit 5133092

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@
8787
"type": "integer",
8888
"default": 2000,
8989
"markdownDescription": "Delay calculation of code diagnostics after modifications in source. In milliseconds, default 2000 ms."
90+
},
91+
"c3.format.enable": {
92+
"type": "boolean",
93+
"default": false,
94+
"markdownDescription": "Enables formatting (via `clang-format`, see [clang.llvm.org](https://clang.llvm.org/docs/ClangFormat.html))"
95+
},
96+
"c3.format.path": {
97+
"type": "string",
98+
"default": null,
99+
"markdownDescription": "The path to `clang-format` binary (default: `clang-format` in `PATH`)"
90100
}
91101
}
92102
}

src/extension.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { activate as activateLS, deactivate as deactivateLS } from './lsp';
22
import { setupC3 } from './setupC3';
3+
import { setupFormat } from './format';
34

45
export async function activate(context) {
56
await setupC3(context);
7+
await setupFormat(context);
68
activateLS(context);
79
}
810

src/format.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import vscode from "vscode";
2+
import { spawn } from "child_process";
3+
4+
function runClangFormat(text, fileName, formatterPath = "clang-format") {
5+
return new Promise((resolve, reject) => {
6+
const args = [];
7+
8+
if (fileName) {
9+
args.push("-assume-filename", fileName);
10+
}
11+
12+
const proc = spawn(formatterPath, args);
13+
14+
let stdout = "";
15+
let stderr = "";
16+
17+
proc.stdout.on("data", (data) => (stdout += data));
18+
proc.stderr.on("data", (data) => (stderr += data));
19+
20+
proc.on("error", reject);
21+
22+
proc.on("close", (code) => {
23+
if (code !== 0) {
24+
return reject(new Error(`clang-format exited with code ${code}: ${stderr}`));
25+
}
26+
27+
resolve(stdout);
28+
});
29+
30+
proc.stdin.end(text);
31+
});
32+
}
33+
34+
export async function setupFormat(context) {
35+
const fmtConfig = vscode.workspace.getConfiguration("c3.format");
36+
const fmtPath = fmtConfig.get("path") || "clang-format";
37+
38+
context.subscriptions.push(
39+
// Format full document
40+
vscode.languages.registerDocumentFormattingEditProvider(
41+
[
42+
{ language: "c3", scheme: "file" }, // files on disk
43+
{ language: "c3", scheme: "untitled" }, // unsaved files
44+
], {
45+
async provideDocumentFormattingEdits(document) {
46+
try {
47+
const input = document.getText();
48+
const result = await runClangFormat(input, document.fileName, fmtPath);
49+
50+
const fullRange = new vscode.Range(
51+
document.positionAt(0),
52+
document.positionAt(input.length)
53+
);
54+
55+
return [
56+
vscode.TextEdit.replace(fullRange, result)
57+
];
58+
} catch (err) {
59+
vscode.window.showErrorMessage(
60+
`Error formatting c3 document: ${err.message}`
61+
);
62+
return [];
63+
}
64+
}
65+
})
66+
);
67+
}

0 commit comments

Comments
 (0)