diff --git a/src/buffer/reflow.zig b/src/buffer/reflow.zig index b191a06..fa3214a 100644 --- a/src/buffer/reflow.zig +++ b/src/buffer/reflow.zig @@ -8,35 +8,25 @@ pub fn reflow(allocator: std.mem.Allocator, text: []const u8, width: usize) erro var first = true; var line_len: usize = 0; for (words) |word| { - const state: enum { - begin, - words, - } = if (line_len == 0) .begin else .words; - blk: switch (state) { - .begin => { - if (first) { - try writer.writeAll(prefix.first); - first = false; - } else { - try writer.writeAll(prefix.continuation); - var pad = prefix.first.len - prefix.continuation.len; - while (pad > 0) : (pad -= 1) - try writer.writeByte(' '); - } - line_len += prefix.len; - continue :blk .words; - }, - .words => { - if (line_len + word.len + 1 >= width - 1) { - try writer.writeByte('\n'); - line_len = 0; - continue :blk .begin; - } - if (line_len > prefix.len) + if (line_len == 0) { + if (first) { + try writer.writeAll(prefix.first); + first = false; + } else { + try writer.writeAll(prefix.continuation); + var pad = prefix.first.len - prefix.continuation.len; + while (pad > 0) : (pad -= 1) try writer.writeByte(' '); - try writer.writeAll(word); - line_len += word.len; - }, + } + line_len += prefix.len; + } + if (line_len > prefix.len) + try writer.writeByte(' '); + try writer.writeAll(word); + line_len += word.len; + if (line_len >= width) { + try writer.writeByte('\n'); + line_len = 0; } } diff --git a/src/tui/editor.zig b/src/tui/editor.zig index df8754d..4af99ac 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -6829,9 +6829,7 @@ pub const Editor = struct { self.filter_ = null; } - fn reflow_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator, ctx: Context) error{Stop}!Buffer.Root { - var reflow_width: usize = tui.config().reflow_width; - _ = ctx.args.match(.{tp.extract(&reflow_width)}) catch {}; + fn reflow_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { var root = root_; var sel = cursel.selection orelse return root; sel.normalize(); @@ -6839,7 +6837,7 @@ pub const Editor = struct { const sfa_allocator = sfa.get(); const cut_text = copy_selection(root, sel, sfa_allocator, self.metrics) catch return error.Stop; defer sfa_allocator.free(cut_text); - const reflowed = Buffer.reflow(sfa_allocator, cut_text, reflow_width) catch return error.Stop; + const reflowed = Buffer.reflow(sfa_allocator, cut_text, tui.config().reflow_width) catch return error.Stop; defer sfa_allocator.free(reflowed); root = try self.delete_selection(root, cursel, allocator); root = self.insert(root, cursel, reflowed, allocator) catch return error.Stop; @@ -6847,16 +6845,13 @@ pub const Editor = struct { return root; } - pub fn reflow(self: *Self, ctx: Context) Result { + pub fn reflow(self: *Self, _: Context) Result { const b = try self.buf_for_update(); - const root = try self.with_cursels_mut_once_arg(b.root, reflow_cursel, b.allocator, ctx); + const root = try self.with_cursels_mut_once(b.root, reflow_cursel, b.allocator); try self.update_buf(root); self.clamp(); } - pub const reflow_meta: Meta = .{ - .description = "Reflow selection", - .arguments = &.{.integer}, - }; + pub const reflow_meta: Meta = .{ .description = "Reflow selection" }; fn to_upper_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { var root = root_;