fix(coding-agent): normalize CRLF in write preview rendering (fixes #1854)

This commit is contained in:
Mario Zechner 2026-03-05 22:35:03 +01:00
parent 1f39cc776a
commit daaabbeac5

View file

@ -48,6 +48,14 @@ function replaceTabs(text: string): string {
return text.replace(/\t/g, " "); return text.replace(/\t/g, " ");
} }
/**
* Normalize control characters for terminal preview rendering.
* Keep tool arguments unchanged, sanitize only display text.
*/
function normalizeDisplayText(text: string): string {
return text.replace(/\r/g, "");
}
/** Safely coerce value to string for display. Returns null if invalid type. */ /** Safely coerce value to string for display. Returns null if invalid type. */
function str(value: unknown): string | null { function str(value: unknown): string | null {
if (typeof value === "string") return value; if (typeof value === "string") return value;
@ -174,7 +182,8 @@ export class ToolExecutionComponent extends Container {
return; return;
} }
const normalized = replaceTabs(fileContent); const displayContent = normalizeDisplayText(fileContent);
const normalized = replaceTabs(displayContent);
this.writeHighlightCache = { this.writeHighlightCache = {
rawPath, rawPath,
lang, lang,
@ -219,7 +228,8 @@ export class ToolExecutionComponent extends Container {
} }
const deltaRaw = fileContent.slice(cache.rawContent.length); const deltaRaw = fileContent.slice(cache.rawContent.length);
const deltaNormalized = replaceTabs(deltaRaw); const deltaDisplay = normalizeDisplayText(deltaRaw);
const deltaNormalized = replaceTabs(deltaDisplay);
cache.rawContent = fileContent; cache.rawContent = fileContent;
if (cache.normalizedLines.length === 0) { if (cache.normalizedLines.length === 0) {
@ -686,7 +696,8 @@ export class ToolExecutionComponent extends Container {
if (cache && cache.lang === lang && cache.rawPath === rawPath && cache.rawContent === fileContent) { if (cache && cache.lang === lang && cache.rawPath === rawPath && cache.rawContent === fileContent) {
lines = cache.highlightedLines; lines = cache.highlightedLines;
} else { } else {
const normalized = replaceTabs(fileContent); const displayContent = normalizeDisplayText(fileContent);
const normalized = replaceTabs(displayContent);
lines = highlightCode(normalized, lang); lines = highlightCode(normalized, lang);
this.writeHighlightCache = { this.writeHighlightCache = {
rawPath, rawPath,
@ -697,7 +708,7 @@ export class ToolExecutionComponent extends Container {
}; };
} }
} else { } else {
lines = fileContent.split("\n"); lines = normalizeDisplayText(fileContent).split("\n");
this.writeHighlightCache = undefined; this.writeHighlightCache = undefined;
} }