Skip to content

Commit

Permalink
Merge pull request #4433 from panoanx/master
Browse files Browse the repository at this point in the history
fix:  stringify chunk before merging chunks breaks character at the end
  • Loading branch information
James-Yu authored Oct 13, 2024
2 parents 1a46978 + 4ea5f55 commit 7f4f1ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
18 changes: 11 additions & 7 deletions src/lint/latex-formatter/latexindent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,26 +156,30 @@ function format(document: vscode.TextDocument, range?: vscode.Range): Thenable<v
logger.logCommand('Formatting LaTeX.', formatter, args)
const worker = cs.spawn(formatter, args, { stdio: 'pipe', cwd: documentDirectory })
// handle stdout/stderr
const stdoutBuffer: string[] = []
const stderrBuffer: string[] = []
worker.stdout.on('data', (chunk: Buffer | string) => 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 => {
removeTemporaryFiles()
if (code !== 0) {
void logger.showErrorMessage('Formatting failed. Please refer to LaTeX Workshop Output for details.')
logger.log(`Formatting failed with exit code ${code}`)
logger.log(`stderr: ${stderrBuffer.join('')}`)
logger.log(`stderr: ${Buffer.concat(stderrBuffer).toString()}`)
return resolve(undefined)
}
const stdout = stdoutBuffer.join('')
const stdout = Buffer.concat(stdoutBuffer).toString()
if (stdout !== '') {
const edit = vscode.TextEdit.replace(range ?? document.validateRange(new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE)), stdout)
logger.log('Formatted ' + document.fileName)
Expand Down
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 7f4f1ff

Please sign in to comment.