Skip to content

Commit 05042fc

Browse files
Peter MarreckPeter Marreck
authored andcommitted
fix: replace_lines ensures body ends with newline
Without a trailing newline, the last line of the replacement body would merge with the following line in the file. All other splice callers (insert-at, insert-before, insert-after) already handled this; replace-lines was the only one missing it.
1 parent 342156e commit 05042fc

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/main.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3294,7 +3294,16 @@ pub fn runReplaceLines(allocator: std.mem.Allocator, file_path: []const u8, from
32943294
const start_byte = offsets[from_idx];
32953295
const end_byte = if (to_idx + 1 < offsets.len) offsets[to_idx + 1] else source.len;
32963296

3297-
try spliceFile(allocator, file_path, start_byte, end_byte, input_text);
3297+
// Ensure body ends with newline so replacement doesn't merge with next line
3298+
const body = if (input_text.len > 0 and input_text[input_text.len - 1] != '\n') blk: {
3299+
const with_nl = try allocator.alloc(u8, input_text.len + 1);
3300+
@memcpy(with_nl[0..input_text.len], input_text);
3301+
with_nl[input_text.len] = '\n';
3302+
break :blk with_nl;
3303+
} else input_text;
3304+
defer if (body.ptr != input_text.ptr) allocator.free(body);
3305+
3306+
try spliceFile(allocator, file_path, start_byte, end_byte, body);
32983307
try writer.print("Replaced lines {d}-{d}\n", .{ from.line, to.line });
32993308
try emitNewVersion(allocator, file_path, writer);
33003309
}

0 commit comments

Comments
 (0)