@imc-trading/svlangserver
Version:
A language server for systemverilog
63 lines (62 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemVerilogFormatter = void 0;
const node_1 = require("vscode-languageserver/node");
const genutils_1 = require("./genutils");
const child = require("child_process");
class SystemVerilogFormatter {
constructor(command) {
this._command = command;
}
setCommand(command) {
this._command = command;
}
format(document, range, options) {
if (!this._command) {
genutils_1.ConnectionLogger.error("Format command not provided");
return Promise.resolve([]);
}
return new Promise((resolve, reject) => {
try {
let stdout = '';
let stderr = '';
let rangeArg = !!range ? ` --lines=${range.start.line + 1}-${range.end.line + 1}` : "";
let commandArgs = (this._command + rangeArg + " -").split(/\s+/);
let command = commandArgs.shift();
let formatProc = child.spawn(command, commandArgs);
formatProc.stdout.on('data', (chunk) => {
stdout += chunk;
});
formatProc.stderr.on('data', (chunk) => {
stderr += chunk;
});
formatProc.on('error', (err) => {
if (err && err.code === 'ENOENT') {
genutils_1.ConnectionLogger.error(`The format command "${this._command}" is not available.`);
resolve([]);
}
});
formatProc.on('close', (code) => {
if (stderr.length !== 0) {
genutils_1.ConnectionLogger.error(`Formatting gave errors`);
resolve([]);
}
if (code !== 0) {
genutils_1.ConnectionLogger.error(`Format command failed`);
resolve([]);
}
resolve([{
range: node_1.Range.create(0, 0, document.lineCount - 1, 0),
newText: stdout
}]);
});
formatProc.stdin.end(document.getText());
}
catch (error) {
genutils_1.ConnectionLogger.error(error);
resolve([]);
}
});
}
}
exports.SystemVerilogFormatter = SystemVerilogFormatter;