Compare commits

..

No commits in common. "9c4f30c4ee297d6799bf00cca22e24b1431fceed" and "01cbdf8545bb9ef9b01b119f90a3cd0ee069b897" have entirely different histories.

2 changed files with 23 additions and 38 deletions

View file

@ -8,35 +8,25 @@ pub fn reflow(allocator: std.mem.Allocator, text: []const u8, width: usize) erro
var first = true; var first = true;
var line_len: usize = 0; var line_len: usize = 0;
for (words) |word| { for (words) |word| {
const state: enum { if (line_len == 0) {
begin, if (first) {
words, try writer.writeAll(prefix.first);
} = if (line_len == 0) .begin else .words; first = false;
blk: switch (state) { } else {
.begin => { try writer.writeAll(prefix.continuation);
if (first) { var pad = prefix.first.len - prefix.continuation.len;
try writer.writeAll(prefix.first); while (pad > 0) : (pad -= 1)
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)
try writer.writeByte(' '); 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;
} }
} }

View file

@ -6829,9 +6829,7 @@ pub const Editor = struct {
self.filter_ = null; self.filter_ = null;
} }
fn reflow_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator, ctx: Context) error{Stop}!Buffer.Root { fn reflow_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root {
var reflow_width: usize = tui.config().reflow_width;
_ = ctx.args.match(.{tp.extract(&reflow_width)}) catch {};
var root = root_; var root = root_;
var sel = cursel.selection orelse return root; var sel = cursel.selection orelse return root;
sel.normalize(); sel.normalize();
@ -6839,7 +6837,7 @@ pub const Editor = struct {
const sfa_allocator = sfa.get(); const sfa_allocator = sfa.get();
const cut_text = copy_selection(root, sel, sfa_allocator, self.metrics) catch return error.Stop; const cut_text = copy_selection(root, sel, sfa_allocator, self.metrics) catch return error.Stop;
defer sfa_allocator.free(cut_text); 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); defer sfa_allocator.free(reflowed);
root = try self.delete_selection(root, cursel, allocator); root = try self.delete_selection(root, cursel, allocator);
root = self.insert(root, cursel, reflowed, allocator) catch return error.Stop; root = self.insert(root, cursel, reflowed, allocator) catch return error.Stop;
@ -6847,16 +6845,13 @@ pub const Editor = struct {
return root; 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 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); try self.update_buf(root);
self.clamp(); self.clamp();
} }
pub const reflow_meta: Meta = .{ pub const reflow_meta: Meta = .{ .description = "Reflow selection" };
.description = "Reflow selection",
.arguments = &.{.integer},
};
fn to_upper_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { fn to_upper_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root {
var root = root_; var root = root_;