Skip to content

Commit

Permalink
fix: stdout concatenation for tex-fmt #4433
Browse files Browse the repository at this point in the history
  • Loading branch information
panoanx committed Oct 13, 2024
1 parent ba1633f commit 4ea5f55
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/lint/latex-formatter/tex-fmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ async function formatDocument(document: vscode.TextDocument, range?: vscode.Rang
const args = [...(config.get('formatting.tex-fmt.args') as string[]), '--stdin']
const process = lw.external.spawn(program, args, { cwd: path.dirname(document.uri.fsPath) })

let stdout: string = ''
let stdout: Buffer = Buffer.alloc(0)
process.stdout?.on('data', (msg: Buffer | string) => {
stdout += msg
stdout = Buffer.concat([stdout, Buffer.isBuffer(msg) ? msg : Buffer.from(msg)])
})

const promise = new Promise<vscode.TextEdit | undefined>(resolve => {
Expand All @@ -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))
})
})

Expand Down

0 comments on commit 4ea5f55

Please sign in to comment.