diff --git a/src/extension.ts b/src/extension.ts index 5c1481e..a6994e2 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,16 +1,31 @@ import { languages, Range, TextDocument, TextEdit } from 'vscode'; -import * as cp from 'child_process'; -import * as path from 'path'; +import { spawnSync } from 'child_process'; +import { join, basename } from 'path'; +import { randomBytes } from 'crypto'; +import { tmpdir } from 'os'; +import { readFileSync, writeFileSync } from 'fs'; function provideDocumentFormattingEdits(document: TextDocument): TextEdit[] { - let tempfile = cp.spawnSync("mktemp").stdout.toString().trim(); - tempfile = `${tempfile}.cue`; + const dir = tmpdir(); + const currentFileName = basename(document.fileName); + const random = randomBytes(16).toString('hex'); + const tmpfileName = `vscode-cue-fmt-${random}-${currentFileName}`; + const tmpfile = join(dir, tmpfileName); - const copy = cp.execSync(`cat ${document.fileName} > ${tempfile}`) - const fmt = cp.spawnSync("cue", ["fmt", tempfile], {}); - const formatted = cp.spawnSync("cat", [tempfile]).stdout.toString().trim(); + // copy current contents to a tempfile + writeFileSync(tmpfile, document.getText()); + + // run `cue fmt` on temp file + // TODO: handle errror/stderr, report to user + const fmt = spawnSync("cue", ["fmt", tmpfile], {}); + + // read formatted file + const formatted = readFileSync(tmpfile).toString(); + + // create range representing the whole document to be replaced const range = document.validateRange(new Range(0, 0, Infinity, Infinity)); + // write formatted CUE back to document return [new TextEdit(range, formatted)]; }