diff --git a/src/buffer/Buffer.zig b/src/buffer/Buffer.zig index 27612f2..1f19d75 100644 --- a/src/buffer/Buffer.zig +++ b/src/buffer/Buffer.zig @@ -1122,9 +1122,9 @@ pub fn load_from_file_and_update(self: *Self, file_path: []const u8) !void { self.last_save_eol_mode = eol_mode; } -pub fn store_to_string(self: *const Self, allocator: Allocator) ![]u8 { +pub fn store_to_string(self: *const Self, allocator: Allocator, eol_mode: EolMode) ![]u8 { var s = try ArrayList(u8).initCapacity(allocator, self.root.weights_sum().len); - try self.root.store(s.writer()); + try self.root.store(s.writer(), eol_mode); return s.toOwnedSlice(); } diff --git a/test/tests_buffer.zig b/test/tests_buffer.zig index e92d237..fa757d9 100644 --- a/test/tests_buffer.zig +++ b/test/tests_buffer.zig @@ -8,20 +8,21 @@ fn metrics() Buffer.Metrics { return .{ .ctx = undefined, .egc_length = struct { - fn f(_: *const anyopaque, _: []const u8, colcount: *c_int, _: usize) usize { + fn f(_: Buffer.Metrics, _: []const u8, colcount: *c_int, _: usize) usize { colcount.* = 1; return 1; } }.f, .egc_chunk_width = struct { - fn f(_: *const anyopaque, chunk_: []const u8, _: usize) usize { + fn f(_: Buffer.Metrics, chunk_: []const u8, _: usize) usize { return chunk_.len; } }.f, + .tab_width = 1, }; } -fn get_big_doc() !*Buffer { +fn get_big_doc(eol_mode: *Buffer.EolMode) !*Buffer { const BigDocGen = struct { line_num: usize = 0, lines: usize = 10000, @@ -65,7 +66,7 @@ fn get_big_doc() !*Buffer { try gen.reader().readAllArrayList(&doc, std.math.maxInt(usize)); var buf = try Buffer.create(a); var fis = std.io.fixedBufferStream(doc.items); - buf.update(try buf.load(fis.reader(), doc.items.len)); + buf.update(try buf.load(fis.reader(), doc.items.len, eol_mode)); return buf; } @@ -84,14 +85,15 @@ test "buffer" { \\are belong to \\us! ; + var eol_mode: Buffer.EolMode = .lf; const buffer = try Buffer.create(a); defer buffer.deinit(); - const root = try buffer.load_from_string(doc); + const root = try buffer.load_from_string(doc, &eol_mode); try std.testing.expect(root.is_balanced()); buffer.update(root); - const result: []const u8 = try buffer.store_to_string(a); + const result: []const u8 = try buffer.store_to_string(a, eol_mode); defer a.free(result); try std.testing.expectEqualDeep(result, doc); try std.testing.expectEqual(doc.len, result.len); @@ -105,7 +107,8 @@ fn get_line(buf: *const Buffer, line: usize) ![]const u8 { } test "walk_from_line" { - const buffer = try get_big_doc(); + var eol_mode: Buffer.EolMode = .lf; + const buffer = try get_big_doc(&eol_mode); defer buffer.deinit(); const lines = buffer.root.lines(); @@ -143,9 +146,10 @@ test "line_len" { \\are belong to \\us! ; + var eol_mode: Buffer.EolMode = .lf; const buffer = try Buffer.create(a); defer buffer.deinit(); - buffer.update(try buffer.load_from_string(doc)); + buffer.update(try buffer.load_from_string(doc, &eol_mode)); try std.testing.expectEqual(try buffer.root.line_width(0, metrics()), 8); try std.testing.expectEqual(try buffer.root.line_width(1, metrics()), 5); @@ -166,16 +170,17 @@ test "get_byte_pos" { \\are belong to \\us! ; + var eol_mode: Buffer.EolMode = .lf; const buffer = try Buffer.create(a); defer buffer.deinit(); - buffer.update(try buffer.load_from_string(doc)); + buffer.update(try buffer.load_from_string(doc, &eol_mode)); - try std.testing.expectEqual(0, try buffer.root.get_byte_pos(.{ .row = 0, .col = 0 }, metrics())); - try std.testing.expectEqual(9, try buffer.root.get_byte_pos(.{ .row = 1, .col = 0 }, metrics())); - try std.testing.expectEqual(11, try buffer.root.get_byte_pos(.{ .row = 1, .col = 2 }, metrics())); - try std.testing.expectEqual(33, try buffer.root.get_byte_pos(.{ .row = 4, .col = 0 }, metrics())); - try std.testing.expectEqual(66, try buffer.root.get_byte_pos(.{ .row = 8, .col = 0 }, metrics())); - try std.testing.expectEqual(97, try buffer.root.get_byte_pos(.{ .row = 11, .col = 2 }, metrics())); + try std.testing.expectEqual(0, try buffer.root.get_byte_pos(.{ .row = 0, .col = 0 }, metrics(), eol_mode)); + try std.testing.expectEqual(9, try buffer.root.get_byte_pos(.{ .row = 1, .col = 0 }, metrics(), eol_mode)); + try std.testing.expectEqual(11, try buffer.root.get_byte_pos(.{ .row = 1, .col = 2 }, metrics(), eol_mode)); + try std.testing.expectEqual(33, try buffer.root.get_byte_pos(.{ .row = 4, .col = 0 }, metrics(), eol_mode)); + try std.testing.expectEqual(66, try buffer.root.get_byte_pos(.{ .row = 8, .col = 0 }, metrics(), eol_mode)); + try std.testing.expectEqual(97, try buffer.root.get_byte_pos(.{ .row = 11, .col = 2 }, metrics(), eol_mode)); } test "del_chars" { @@ -193,25 +198,26 @@ test "del_chars" { \\are belong to \\us! ; + var eol_mode: Buffer.EolMode = .lf; const buffer = try Buffer.create(a); defer buffer.deinit(); - buffer.update(try buffer.load_from_string(doc)); + buffer.update(try buffer.load_from_string(doc, &eol_mode)); - buffer.update(try buffer.root.del_chars(3, try buffer.root.line_width(3, metrics()) - 1, 1, buffer.a, metrics())); + buffer.update(try buffer.root.del_chars(3, try buffer.root.line_width(3, metrics()) - 1, 1, buffer.allocator, metrics())); const line3 = try get_line(buffer, 3); defer a.free(line3); try std.testing.expect(std.mem.eql(u8, line3, "us")); - buffer.update(try buffer.root.del_chars(3, 0, 7, buffer.a, metrics())); + buffer.update(try buffer.root.del_chars(3, 0, 7, buffer.allocator, metrics())); const line3_1 = try get_line(buffer, 3); defer a.free(line3_1); try std.testing.expect(std.mem.eql(u8, line3_1, "your")); try std.testing.expect(buffer.root.is_balanced()); - buffer.update(try buffer.root.rebalance(buffer.a, buffer.a)); + buffer.update(try buffer.root.rebalance(buffer.allocator, buffer.allocator)); try std.testing.expect(buffer.root.is_balanced()); - buffer.update(try buffer.root.del_chars(0, try buffer.root.line_width(0, metrics()) - 1, 2, buffer.a, metrics())); + buffer.update(try buffer.root.del_chars(0, try buffer.root.line_width(0, metrics()) - 1, 2, buffer.allocator, metrics())); const line0 = try get_line(buffer, 0); defer a.free(line0); try std.testing.expect(std.mem.eql(u8, line0, "All youropes")); @@ -238,11 +244,12 @@ test "del_chars2" { \\are belong to \\us! ; + var eol_mode: Buffer.EolMode = .lf; const buffer = try Buffer.create(a); defer buffer.deinit(); - buffer.update(try buffer.load_from_string(doc)); + buffer.update(try buffer.load_from_string(doc, &eol_mode)); - buffer.update(try buffer.root.del_chars(2, try buffer.root.line_width(2, metrics()) - 3, 6, buffer.a, metrics())); + buffer.update(try buffer.root.del_chars(2, try buffer.root.line_width(2, metrics()) - 3, 6, buffer.allocator, metrics())); try check_line(buffer, 2, "are belong!"); try check_line(buffer, 3, "All your"); @@ -253,64 +260,65 @@ test "insert_chars" { const doc: []const u8 = \\B ; + var eol_mode: Buffer.EolMode = .lf; const buffer = try Buffer.create(a); defer buffer.deinit(); - buffer.update(try buffer.load_from_string(doc)); + buffer.update(try buffer.load_from_string(doc, &eol_mode)); const line0 = try get_line(buffer, 0); defer a.free(line0); try std.testing.expect(std.mem.eql(u8, line0, "B")); - _, _, var root = try buffer.root.insert_chars(0, 0, "1", buffer.a, metrics()); + _, _, var root = try buffer.root.insert_chars(0, 0, "1", buffer.allocator, metrics()); buffer.update(root); const line1 = try get_line(buffer, 0); defer a.free(line1); try std.testing.expect(std.mem.eql(u8, line1, "1B")); - _, _, root = try root.insert_chars(0, 1, "2", buffer.a, metrics()); + _, _, root = try root.insert_chars(0, 1, "2", buffer.allocator, metrics()); buffer.update(root); const line2 = try get_line(buffer, 0); defer a.free(line2); try std.testing.expect(std.mem.eql(u8, line2, "12B")); - _, _, root = try root.insert_chars(0, 2, "3", buffer.a, metrics()); + _, _, root = try root.insert_chars(0, 2, "3", buffer.allocator, metrics()); buffer.update(root); const line3 = try get_line(buffer, 0); defer a.free(line3); try std.testing.expect(std.mem.eql(u8, line3, "123B")); - _, _, root = try root.insert_chars(0, 3, "4", buffer.a, metrics()); + _, _, root = try root.insert_chars(0, 3, "4", buffer.allocator, metrics()); buffer.update(root); const line4 = try get_line(buffer, 0); defer a.free(line4); try std.testing.expect(std.mem.eql(u8, line4, "1234B")); - _, _, root = try root.insert_chars(0, 4, "5", buffer.a, metrics()); + _, _, root = try root.insert_chars(0, 4, "5", buffer.allocator, metrics()); buffer.update(root); const line5 = try get_line(buffer, 0); defer a.free(line5); try std.testing.expect(std.mem.eql(u8, line5, "12345B")); - _, _, root = try root.insert_chars(0, 5, "6", buffer.a, metrics()); + _, _, root = try root.insert_chars(0, 5, "6", buffer.allocator, metrics()); buffer.update(root); const line6 = try get_line(buffer, 0); defer a.free(line6); try std.testing.expect(std.mem.eql(u8, line6, "123456B")); - _, _, root = try root.insert_chars(0, 6, "7", buffer.a, metrics()); + _, _, root = try root.insert_chars(0, 6, "7", buffer.allocator, metrics()); buffer.update(root); const line7 = try get_line(buffer, 0); defer a.free(line7); try std.testing.expect(std.mem.eql(u8, line7, "1234567B")); - const line, const col, root = try buffer.root.insert_chars(0, 7, "8\n9", buffer.a, metrics()); + const line, const col, root = try buffer.root.insert_chars(0, 7, "8\n9", buffer.allocator, metrics()); buffer.update(root); const line8 = try get_line(buffer, 0);