From ba1633ffc78f82b0d37bc2e2fdd30fa7c3a20607 Mon Sep 17 00:00:00 2001 From: panoanx Date: Sun, 13 Oct 2024 14:30:14 +0800 Subject: [PATCH 1/2] fix: stringify chunk before merging chunks breaks character at the end --- src/lint/latex-formatter/latexindent.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) 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 Date: Sun, 13 Oct 2024 21:16:04 +0800 Subject: [PATCH 2/2] fix: stdout concatenation for tex-fmt #4433 --- src/lint/latex-formatter/tex-fmt.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/lint/latex-formatter/tex-fmt.ts b/src/lint/latex-formatter/tex-fmt.ts index ffa2fd4ca..e146b5d22 100644 --- a/src/lint/latex-formatter/tex-fmt.ts +++ b/src/lint/latex-formatter/tex-fmt.ts @@ -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(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)) }) })