Skip to content

Commit 51daf08

Browse files
committed
fix(webview-ui): correct new-file diff line counting by ignoring trailing newline in unified diff; add test for ChatRow
1 parent df6be8f commit 51daf08

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

webview-ui/src/components/chat/__tests__/ChatRow.diff-actions.spec.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,25 @@ describe("ChatRow - inline diff stats and actions", () => {
111111
expect(screen.getByText("+3")).toBeInTheDocument()
112112
expect(screen.getByText("-0")).toBeInTheDocument()
113113
})
114+
115+
it("counts only added lines for newFileCreated with trailing newline", () => {
116+
const content = "a\nb\nc\n"
117+
const message: any = {
118+
type: "ask",
119+
ask: "tool",
120+
ts: Date.now(),
121+
partial: false,
122+
text: JSON.stringify({
123+
tool: "newFileCreated",
124+
path: "src/new-file.ts",
125+
content,
126+
}),
127+
}
128+
129+
renderChatRow(message)
130+
131+
// Trailing newline should not increase the added count
132+
expect(screen.getByText("+3")).toBeInTheDocument()
133+
expect(screen.getByText("-0")).toBeInTheDocument()
134+
})
114135
})

webview-ui/src/utils/diffUtils.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,22 @@ export function convertSearchReplaceToUnifiedDiff(content: string, filePath?: st
125125
return hasBlocks ? unified : content
126126
}
127127

128-
/** Build a unified diff for a brand new file (all content lines are additions) */
128+
/** Build a unified diff for a brand new file (all content lines are additions).
129+
* Trailing newline is ignored for line counting and emission.
130+
*/
129131
export function convertNewFileToUnifiedDiff(content: string, filePath?: string): string {
130132
const fileName = filePath || "file"
131133
// Normalize EOLs to keep counts consistent
132134
const normalized = content.replace(/\r\n/g, "\n")
133-
const lines = normalized.split("\n")
135+
const parts = normalized.split("\n")
136+
// Drop trailing empty item produced by a final newline so we count only real content lines
137+
const contentLines = parts[parts.length - 1] === "" ? parts.slice(0, -1) : parts
134138

135139
let diff = `--- /dev/null\n`
136140
diff += `+++ ${fileName}\n`
137-
diff += `@@ -0,0 +1,${normalized === "" ? 0 : lines.length} @@\n`
141+
diff += `@@ -0,0 +1,${contentLines.length} @@\n`
138142

139-
for (const line of lines) {
140-
// Preserve final newline behavior: if content ended with newline, split will produce trailing ""
141-
// which is still okay to emit as "+", it represents a blank line.
143+
for (const line of contentLines) {
142144
diff += `+${line}\n`
143145
}
144146

0 commit comments

Comments
 (0)