@@ -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+ */
129131export 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