|
| 1 | +class Formatter { |
| 2 | + constructor(config) { |
| 3 | + this.config = config; |
| 4 | + } |
| 5 | + |
| 6 | + async getProcess(filename = null) { |
| 7 | + const executablePath = nova.path.expanduser(this.config.get("executablePath")); |
| 8 | + const commandArguments = this.config.get("commandFormatArguments"); |
| 9 | + const defaultOptions = (filename) |
| 10 | + ? (filename !== ".") |
| 11 | + ? ["--quiet", `--stdin-filename=${filename}`, "-"] |
| 12 | + : ["--quiet", filename] |
| 13 | + : ["--quiet", "-"]; |
| 14 | + |
| 15 | + if (!nova.fs.stat(executablePath)) { |
| 16 | + console.error(`Executable ${executablePath} does not exist`); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + var options = []; |
| 21 | + |
| 22 | + if (commandArguments) { |
| 23 | + options = commandArguments |
| 24 | + .replaceAll("\n", " ") |
| 25 | + .split(" ") |
| 26 | + .map((option) => option.trim()) |
| 27 | + .filter((option) => option !== " "); |
| 28 | + } |
| 29 | + |
| 30 | + options = [...options, ...defaultOptions].filter((option) => option !== ""); |
| 31 | + |
| 32 | + return new Process( |
| 33 | + executablePath, |
| 34 | + { |
| 35 | + args: ["format", ...Array.from(new Set(options))], |
| 36 | + stdio: "pipe", |
| 37 | + cwd: nova.workspace.path, // NOTE: must be explicitly set |
| 38 | + } |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + async getPromiseToFormat(editor) { |
| 43 | + if (!this.config.get("formatOnSave")) return; |
| 44 | + |
| 45 | + return new Promise((resolve, reject) => { |
| 46 | + this.format(editor, resolve, reject); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + async format(editor, resolve=null, reject=null) { |
| 51 | + if (editor.document.isEmpty) { |
| 52 | + if (reject) reject("empty file"); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + let process = await this.getProcess( |
| 57 | + editor.document.path ? nova.path.basename(editor.document.path) : null |
| 58 | + ); |
| 59 | + |
| 60 | + if (!process) { |
| 61 | + if (reject) reject("no process"); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const textRange = new Range(0, editor.document.length); |
| 66 | + const content = editor.document.getTextInRange(textRange); |
| 67 | + const filePath = nova.workspace.relativizePath(editor.document.path); |
| 68 | + |
| 69 | + let outBuffer = []; |
| 70 | + let errBuffer = []; |
| 71 | + |
| 72 | + process.onStdout((output) => outBuffer.push(output)); |
| 73 | + process.onStderr((error) => errBuffer.push(error)); |
| 74 | + process.onDidExit((status) => { |
| 75 | + if (status === 0) { |
| 76 | + const formattedContent = outBuffer.join(""); |
| 77 | + |
| 78 | + let result = editor.edit((edit) => { |
| 79 | + if (formattedContent !== content) { |
| 80 | + console.log("Formatting " + filePath); |
| 81 | + edit.replace(textRange, formattedContent, InsertTextFormat.PlainText); |
| 82 | + } else { |
| 83 | + console.log("Nothing to format"); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + if (resolve) resolve(result); |
| 88 | + } else { |
| 89 | + console.error(errBuffer.join("")); |
| 90 | + if (reject) reject(); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + console.log("Running " + process.command + " " + process.args.join(" ")); |
| 95 | + |
| 96 | + process.start(); |
| 97 | + |
| 98 | + let writer = process.stdin.getWriter(); |
| 99 | + |
| 100 | + writer.ready.then(() => { |
| 101 | + writer.write(content); |
| 102 | + writer.close(); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + async formatWorkspace(workspace) { |
| 107 | + let process = await this.getProcess("."); |
| 108 | + |
| 109 | + if (!process) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + let errBuffer = []; |
| 114 | + |
| 115 | + process.onStderr((error) => errBuffer.push(error)); |
| 116 | + process.onDidExit((status) => { |
| 117 | + if (status === 0) { |
| 118 | + console.log("Formatting the workspace"); |
| 119 | + } else { |
| 120 | + console.error(errBuffer.join("")); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + console.log("Running " + process.command + " " + process.args.join(" ")); |
| 125 | + |
| 126 | + process.start(); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +module.exports = Formatter; |
0 commit comments