diff --git a/src/lint/latex-formatter/latexindent.ts b/src/lint/latex-formatter/latexindent.ts index 1469fda94..da53266ec 100644 --- a/src/lint/latex-formatter/latexindent.ts +++ b/src/lint/latex-formatter/latexindent.ts @@ -156,15 +156,19 @@ function format(document: vscode.TextDocument, range?: vscode.Range): Thenable stdoutBuffer.push(chunk.toString())) - worker.stderr.on('data', (chunk: Buffer | string) => stderrBuffer.push(chunk.toString())) + const stdoutBuffer: Buffer[] = [] + const stderrBuffer: Buffer[] = [] + worker.stdout.on('data', (chunk: Buffer | string) => { + stdoutBuffer.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)) + }) + worker.stderr.on('data', (chunk: Buffer | string) => { + stderrBuffer.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)) + }) worker.on('error', err => { removeTemporaryFiles() void logger.showErrorMessage('Formatting failed. Please refer to LaTeX Workshop Output for details.') logger.log(`Formatting failed: ${err.message}`) - logger.log(`stderr: ${stderrBuffer.join('')}`) + logger.log(`stderr: ${Buffer.concat(stderrBuffer).toString()}`) resolve(undefined) }) worker.on('close', code => { @@ -172,10 +176,10 @@ function format(document: vscode.TextDocument, range?: vscode.Range): Thenable { - stdout += msg + stdout = Buffer.concat([stdout, Buffer.isBuffer(msg) ? msg : Buffer.from(msg)]) }) const promise = new Promise(resolve => { @@ -33,12 +33,13 @@ async function formatDocument(document: vscode.TextDocument, range?: vscode.Rang logger.showErrorMessage(`${program} returned ${code} . Be cautious on the edits.`) resolve(undefined) } + let stdoutStr = stdout.toString() // tex-fmt adds an extra newline at the end - if (stdout.endsWith('\n\n')) { - stdout = stdout.slice(0, -1) + if (stdoutStr.endsWith('\n\n')) { + stdoutStr = stdoutStr.slice(0, -1) } logger.log(`Formatted using ${program} .`) - resolve(vscode.TextEdit.replace(range ?? document.validateRange(new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE)), stdout)) + resolve(vscode.TextEdit.replace(range ?? document.validateRange(new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE)), stdoutStr)) }) })