fix: improve movement behaviour of cursors on unindent
This commit is contained in:
parent
9856303ef6
commit
cc14934aa5
1 changed files with 25 additions and 17 deletions
|
@ -2602,15 +2602,10 @@ pub const Editor = struct {
|
||||||
}
|
}
|
||||||
pub const indent_meta = .{ .description = "Indent current line" };
|
pub const indent_meta = .{ .description = "Indent current line" };
|
||||||
|
|
||||||
fn unindent_cursor(self: *Self, root: Buffer.Root, cursel: *CurSel, cursor: Cursor, allocator: Allocator) error{Stop}!Buffer.Root {
|
fn unindent_cursor(self: *Self, root: Buffer.Root, cursor: *Cursor, cursor_protect: ?*Cursor, allocator: Allocator) error{Stop}!Buffer.Root {
|
||||||
const saved = cursel.*;
|
|
||||||
var newroot = root;
|
var newroot = root;
|
||||||
defer {
|
var cursel: CurSel = .{};
|
||||||
cursel.* = saved;
|
cursel.cursor = cursor.*;
|
||||||
cursel.cursor.clamp_to_buffer(newroot, self.metrics);
|
|
||||||
}
|
|
||||||
cursel.selection = null;
|
|
||||||
cursel.cursor = cursor;
|
|
||||||
const first = find_first_non_ws(root, cursel.cursor.row, self.metrics);
|
const first = find_first_non_ws(root, cursel.cursor.row, self.metrics);
|
||||||
if (first == 0) return error.Stop;
|
if (first == 0) return error.Stop;
|
||||||
const off = first % self.indent_size;
|
const off = first % self.indent_size;
|
||||||
|
@ -2618,8 +2613,16 @@ pub const Editor = struct {
|
||||||
const sel = cursel.enable_selection();
|
const sel = cursel.enable_selection();
|
||||||
sel.begin.move_begin();
|
sel.begin.move_begin();
|
||||||
try sel.end.move_to(root, sel.end.row, cols, self.metrics);
|
try sel.end.move_to(root, sel.end.row, cols, self.metrics);
|
||||||
if (cursel.cursor.col < cols) try cursel.cursor.move_to(root, cursel.cursor.row, cols, self.metrics);
|
var saved = false;
|
||||||
newroot = try self.delete_selection(root, cursel, allocator);
|
if (cursor_protect) |cp| if (cp.row == cursor.row and cp.col < cols) {
|
||||||
|
cp.col = cols + 1;
|
||||||
|
saved = true;
|
||||||
|
};
|
||||||
|
newroot = try self.delete_selection(root, &cursel, allocator);
|
||||||
|
if (cursor_protect) |cp| if (saved) {
|
||||||
|
try cp.move_to(root, cp.row, 0, self.metrics);
|
||||||
|
cp.clamp_to_buffer(newroot, self.metrics);
|
||||||
|
};
|
||||||
return newroot;
|
return newroot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2629,20 +2632,25 @@ pub const Editor = struct {
|
||||||
var sel = sel_;
|
var sel = sel_;
|
||||||
sel.normalize();
|
sel.normalize();
|
||||||
while (sel.begin.row < sel.end.row) : (sel.begin.row += 1)
|
while (sel.begin.row < sel.end.row) : (sel.begin.row += 1)
|
||||||
root = try self.unindent_cursor(root, cursel, sel.begin, allocator);
|
root = try self.unindent_cursor(root, &sel.begin, &cursel.cursor, allocator);
|
||||||
if (sel.end.col > 0)
|
if (sel.end.col > 0)
|
||||||
root = try self.unindent_cursor(root, cursel, sel.end, allocator);
|
root = try self.unindent_cursor(root, &sel.end, &cursel.cursor, allocator);
|
||||||
return root;
|
return root;
|
||||||
} else return self.unindent_cursor(root_, cursel, cursel.cursor, allocator);
|
} else return self.unindent_cursor(root_, &cursel.cursor, &cursel.cursor, allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn restore_cursels(self: *Self) void {
|
||||||
|
self.cursels.clearAndFree();
|
||||||
|
self.cursels = self.cursels_saved.clone() catch return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unindent(self: *Self, _: Context) Result {
|
pub fn unindent(self: *Self, _: Context) Result {
|
||||||
const b = try self.buf_for_update();
|
const b = try self.buf_for_update();
|
||||||
errdefer blk: {
|
errdefer self.restore_cursels();
|
||||||
self.cursels.clearAndFree();
|
const cursor_count = self.cursels.items.len;
|
||||||
self.cursels = self.cursels_saved.clone() catch break :blk;
|
|
||||||
}
|
|
||||||
const root = try self.with_cursels_mut(b.root, unindent_cursel, b.allocator);
|
const root = try self.with_cursels_mut(b.root, unindent_cursel, b.allocator);
|
||||||
|
if (self.cursels.items.len != cursor_count)
|
||||||
|
self.restore_cursels();
|
||||||
try self.update_buf(root);
|
try self.update_buf(root);
|
||||||
}
|
}
|
||||||
pub const unindent_meta = .{ .description = "Unindent current line" };
|
pub const unindent_meta = .{ .description = "Unindent current line" };
|
||||||
|
|
Loading…
Add table
Reference in a new issue