fix: memory leaks in operations that call cut_selection or cut_to
This commit is contained in:
parent
06788c4243
commit
b7343219c1
2 changed files with 29 additions and 21 deletions
|
@ -2612,11 +2612,11 @@ pub const Editor = struct {
|
||||||
return try copy_selection(root, sel.*, text_allocator, self.metrics);
|
return try copy_selection(root, sel.*, text_allocator, self.metrics);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cut_selection(self: *Self, root: Buffer.Root, cursel: *CurSel) !struct { []const u8, Buffer.Root } {
|
pub fn cut_selection(self: *Self, root: Buffer.Root, cursel: *CurSel, text_allocator: Allocator) !struct { []const u8, Buffer.Root } {
|
||||||
return if (cursel.selection) |sel| ret: {
|
return if (cursel.selection) |sel| ret: {
|
||||||
var old_selection: Selection = sel;
|
var old_selection: Selection = sel;
|
||||||
old_selection.normalize();
|
old_selection.normalize();
|
||||||
const cut_text = try copy_selection(root, sel, self.allocator, self.metrics);
|
const cut_text = try copy_selection(root, sel, text_allocator, self.metrics);
|
||||||
if (cut_text.len > 100) {
|
if (cut_text.len > 100) {
|
||||||
self.logger.print("cut:{f}...", .{std.ascii.hexEscape(cut_text[0..100], .lower)});
|
self.logger.print("cut:{f}...", .{std.ascii.hexEscape(cut_text[0..100], .lower)});
|
||||||
} else {
|
} else {
|
||||||
|
@ -2652,41 +2652,43 @@ pub const Editor = struct {
|
||||||
return root_;
|
return root_;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cut_to(self: *Self, move: cursor_operator_const, root_: Buffer.Root) !struct { []const u8, Buffer.Root } {
|
pub fn cut_to(self: *Self, move: cursor_operator_const, root_: Buffer.Root, text_allocator: Allocator) !struct { []const u8, Buffer.Root } {
|
||||||
var all_stop = true;
|
var all_stop = true;
|
||||||
var root = root_;
|
var root = root_;
|
||||||
|
|
||||||
var text = std.ArrayListUnmanaged(u8).empty;
|
var text = std.ArrayListUnmanaged(u8).empty;
|
||||||
defer text.deinit(self.allocator);
|
defer text.deinit(text_allocator);
|
||||||
var first = true;
|
var first = true;
|
||||||
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
||||||
if (cursel.selection) |_| {
|
if (cursel.selection) |_| {
|
||||||
const cut_text, root = self.cut_selection(root, cursel) catch continue;
|
const cut_text, root = self.cut_selection(root, cursel, text_allocator) catch continue;
|
||||||
|
defer text_allocator.free(cut_text);
|
||||||
all_stop = false;
|
all_stop = false;
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
try text.appendSlice(self.allocator, "\n");
|
try text.appendSlice(text_allocator, "\n");
|
||||||
}
|
}
|
||||||
try text.appendSlice(self.allocator, cut_text);
|
try text.appendSlice(text_allocator, cut_text);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
with_selection_const(root, move, cursel, self.metrics) catch continue;
|
with_selection_const(root, move, cursel, self.metrics) catch continue;
|
||||||
const cut_text, root = self.cut_selection(root, cursel) catch continue;
|
const cut_text, root = self.cut_selection(root, cursel, text_allocator) catch continue;
|
||||||
|
defer text_allocator.free(cut_text);
|
||||||
|
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
try text.appendSlice(self.allocator, "\n");
|
try text.appendSlice(text_allocator, "\n");
|
||||||
}
|
}
|
||||||
try text.appendSlice(self.allocator, cut_text);
|
try text.appendSlice(text_allocator, cut_text);
|
||||||
all_stop = false;
|
all_stop = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (all_stop)
|
if (all_stop)
|
||||||
return error.Stop;
|
return error.Stop;
|
||||||
return .{ try text.toOwnedSlice(self.allocator), root };
|
return .{ try text.toOwnedSlice(text_allocator), root };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cut_internal_vim(self: *Self, _: Context) Result {
|
pub fn cut_internal_vim(self: *Self, _: Context) Result {
|
||||||
|
@ -2705,7 +2707,8 @@ pub const Editor = struct {
|
||||||
};
|
};
|
||||||
var first = true;
|
var first = true;
|
||||||
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
||||||
const cut_text, root = try self.cut_selection(root, cursel);
|
const cut_text, root = try self.cut_selection(root, cursel, self.allocator);
|
||||||
|
defer self.allocator.free(cut_text);
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -2740,7 +2743,8 @@ pub const Editor = struct {
|
||||||
var text = std.ArrayListUnmanaged(u8).empty;
|
var text = std.ArrayListUnmanaged(u8).empty;
|
||||||
defer text.deinit(self.allocator);
|
defer text.deinit(self.allocator);
|
||||||
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
||||||
const cut_text, root = try self.cut_selection(root, cursel);
|
const cut_text, root = try self.cut_selection(root, cursel, self.allocator);
|
||||||
|
defer self.allocator.free(cut_text);
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -2770,6 +2774,8 @@ pub const Editor = struct {
|
||||||
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
||||||
if (cursel.selection) |sel| {
|
if (cursel.selection) |sel| {
|
||||||
const copy_text = try copy_selection(root, sel, self.allocator, self.metrics);
|
const copy_text = try copy_selection(root, sel, self.allocator, self.metrics);
|
||||||
|
defer self.allocator.free(copy_text);
|
||||||
|
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -2866,6 +2872,7 @@ pub const Editor = struct {
|
||||||
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
||||||
if (cursel.selection) |sel| {
|
if (cursel.selection) |sel| {
|
||||||
const copy_text = try copy_selection(root, sel, self.allocator, self.metrics);
|
const copy_text = try copy_selection(root, sel, self.allocator, self.metrics);
|
||||||
|
defer self.allocator.free(copy_text);
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -2901,6 +2908,7 @@ pub const Editor = struct {
|
||||||
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
|
||||||
if (cursel.selection) |sel| {
|
if (cursel.selection) |sel| {
|
||||||
const copy_text = try copy_selection(root, sel, self.allocator, self.metrics);
|
const copy_text = try copy_selection(root, sel, self.allocator, self.metrics);
|
||||||
|
defer self.allocator.free(copy_text);
|
||||||
if (first) {
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -3010,7 +3018,7 @@ pub const Editor = struct {
|
||||||
|
|
||||||
pub fn cut_forward_internal(self: *Self, _: Context) Result {
|
pub fn cut_forward_internal(self: *Self, _: Context) Result {
|
||||||
const b = try self.buf_for_update();
|
const b = try self.buf_for_update();
|
||||||
const text, const root = try self.cut_to(move_cursor_right, b.root);
|
const text, const root = try self.cut_to(move_cursor_right, b.root, self.allocator);
|
||||||
self.set_clipboard_internal(text);
|
self.set_clipboard_internal(text);
|
||||||
try self.update_buf(root);
|
try self.update_buf(root);
|
||||||
self.clamp();
|
self.clamp();
|
||||||
|
@ -3094,7 +3102,7 @@ pub const Editor = struct {
|
||||||
|
|
||||||
pub fn cut_buffer_end(self: *Self, _: Context) Result {
|
pub fn cut_buffer_end(self: *Self, _: Context) Result {
|
||||||
const b = try self.buf_for_update();
|
const b = try self.buf_for_update();
|
||||||
const text, const root = try self.cut_to(move_cursor_buffer_end, b.root);
|
const text, const root = try self.cut_to(move_cursor_buffer_end, b.root, self.allocator);
|
||||||
self.set_clipboard_internal(text);
|
self.set_clipboard_internal(text);
|
||||||
try self.update_buf(root);
|
try self.update_buf(root);
|
||||||
self.clamp();
|
self.clamp();
|
||||||
|
@ -3103,7 +3111,7 @@ pub const Editor = struct {
|
||||||
|
|
||||||
pub fn cut_buffer_begin(self: *Self, _: Context) Result {
|
pub fn cut_buffer_begin(self: *Self, _: Context) Result {
|
||||||
const b = try self.buf_for_update();
|
const b = try self.buf_for_update();
|
||||||
const text, const root = try self.cut_to(move_cursor_buffer_begin, b.root);
|
const text, const root = try self.cut_to(move_cursor_buffer_begin, b.root, self.allocator);
|
||||||
self.set_clipboard_internal(text);
|
self.set_clipboard_internal(text);
|
||||||
try self.update_buf(root);
|
try self.update_buf(root);
|
||||||
self.clamp();
|
self.clamp();
|
||||||
|
@ -3112,7 +3120,7 @@ pub const Editor = struct {
|
||||||
|
|
||||||
pub fn cut_word_left_vim(self: *Self, _: Context) Result {
|
pub fn cut_word_left_vim(self: *Self, _: Context) Result {
|
||||||
const b = try self.buf_for_update();
|
const b = try self.buf_for_update();
|
||||||
const text, const root = try self.cut_to(move_cursor_word_left_vim, b.root);
|
const text, const root = try self.cut_to(move_cursor_word_left_vim, b.root, self.allocator);
|
||||||
self.set_clipboard_internal(text);
|
self.set_clipboard_internal(text);
|
||||||
try self.update_buf(root);
|
try self.update_buf(root);
|
||||||
self.clamp();
|
self.clamp();
|
||||||
|
@ -3129,7 +3137,7 @@ pub const Editor = struct {
|
||||||
|
|
||||||
pub fn cut_word_right_vim(self: *Self, _: Context) Result {
|
pub fn cut_word_right_vim(self: *Self, _: Context) Result {
|
||||||
const b = try self.buf_for_update();
|
const b = try self.buf_for_update();
|
||||||
const text, const root = try self.cut_to(move_cursor_word_right_vim, b.root);
|
const text, const root = try self.cut_to(move_cursor_word_right_vim, b.root, self.allocator);
|
||||||
self.set_clipboard_internal(text);
|
self.set_clipboard_internal(text);
|
||||||
try self.update_buf(root);
|
try self.update_buf(root);
|
||||||
self.clamp();
|
self.clamp();
|
||||||
|
@ -3154,7 +3162,7 @@ pub const Editor = struct {
|
||||||
|
|
||||||
pub fn cut_to_end_vim(self: *Self, _: Context) Result {
|
pub fn cut_to_end_vim(self: *Self, _: Context) Result {
|
||||||
const b = try self.buf_for_update();
|
const b = try self.buf_for_update();
|
||||||
const text, const root = try self.cut_to(move_cursor_end_vim, b.root);
|
const text, const root = try self.cut_to(move_cursor_end_vim, b.root, self.allocator);
|
||||||
self.set_clipboard_internal(text);
|
self.set_clipboard_internal(text);
|
||||||
try self.update_buf(root);
|
try self.update_buf(root);
|
||||||
self.clamp();
|
self.clamp();
|
||||||
|
@ -5038,8 +5046,8 @@ pub const Editor = struct {
|
||||||
pub fn find_word_at_cursor(self: *Self, ctx: Context) Result {
|
pub fn find_word_at_cursor(self: *Self, ctx: Context) Result {
|
||||||
_ = ctx;
|
_ = ctx;
|
||||||
const query: []const u8 = try self.copy_word_at_cursor(self.allocator);
|
const query: []const u8 = try self.copy_word_at_cursor(self.allocator);
|
||||||
|
defer self.allocator.free(query);
|
||||||
try self.find_in_buffer(query);
|
try self.find_in_buffer(query);
|
||||||
self.allocator.free(query);
|
|
||||||
}
|
}
|
||||||
pub const find_word_at_cursor_meta: Meta = .{ .description = "Search for the word under the cursor" };
|
pub const find_word_at_cursor_meta: Meta = .{ .description = "Search for the word under the cursor" };
|
||||||
|
|
||||||
|
|
|
@ -306,7 +306,7 @@ const cmds_ = struct {
|
||||||
const mv = tui.mainview() orelse return;
|
const mv = tui.mainview() orelse return;
|
||||||
const ed = mv.get_active_editor() orelse return;
|
const ed = mv.get_active_editor() orelse return;
|
||||||
const b = try ed.buf_for_update();
|
const b = try ed.buf_for_update();
|
||||||
const text, const root = try ed.cut_to(move_noop, b.root);
|
const text, const root = try ed.cut_to(move_noop, b.root, ed.allocator);
|
||||||
ed.set_clipboard_internal(text);
|
ed.set_clipboard_internal(text);
|
||||||
try ed.update_buf(root);
|
try ed.update_buf(root);
|
||||||
ed.clamp();
|
ed.clamp();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue