refactor: simplify completion selection functions
This commit is contained in:
parent
afe39f118b
commit
8c8388c0c2
3 changed files with 21 additions and 24 deletions
|
|
@ -4975,15 +4975,10 @@ pub const Editor = struct {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn insert_completion(self: *Self, sel_: ?Selection, text: []const u8, insertTextFormat: usize) Result {
|
||||
const primary = self.get_primary();
|
||||
const sel = if (sel_) |s| blk: {
|
||||
primary.selection = s;
|
||||
break :blk s;
|
||||
} else primary.selection orelse Selection.from_cursor(&primary.cursor);
|
||||
pub fn insert_completion(self: *Self, sel: Selection, text: []const u8, insertTextFormat: usize) Result {
|
||||
if (self.has_secondary_cursors())
|
||||
self.replicate_selection(sel);
|
||||
primary.selection = sel;
|
||||
self.get_primary().selection = sel;
|
||||
|
||||
switch (insertTextFormat) {
|
||||
2 => try self.insert_snippet(text),
|
||||
|
|
@ -4991,6 +4986,12 @@ pub const Editor = struct {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn insert_completion_at_cursor(self: *Self, text: []const u8, insertTextFormat: usize) Result {
|
||||
const primary = self.get_primary();
|
||||
const sel = primary.selection orelse Selection.from_cursor(&primary.cursor);
|
||||
return self.insert_completion(sel, text, insertTextFormat);
|
||||
}
|
||||
|
||||
pub fn update_completion_cursels(self: *Self, sel: Selection, text: []const u8) Result {
|
||||
const b = self.buf_for_update() catch return;
|
||||
if (self.has_secondary_cursors())
|
||||
|
|
|
|||
|
|
@ -299,36 +299,32 @@ pub fn get_query_selection(editor: *ed.Editor, values: Values) ?Buffer.Selection
|
|||
return get_replacement_selection(editor, values.insert, values.replace);
|
||||
}
|
||||
|
||||
fn get_replacement_selection(editor: *ed.Editor, insert_: ?Buffer.Selection, replace_: ?Buffer.Selection) ?Buffer.Selection {
|
||||
fn get_replacement_selection(editor: *ed.Editor, insert_: ?Buffer.Selection, replace_: ?Buffer.Selection) Buffer.Selection {
|
||||
const pos = switch (tui.config().completion_insert_mode) {
|
||||
.replace => replace_ orelse insert_ orelse return null,
|
||||
.insert => insert_ orelse replace_ orelse return null,
|
||||
.replace => replace_ orelse insert_ orelse return ed.Selection.from_cursor(&editor.get_primary().cursor),
|
||||
.insert => insert_ orelse replace_ orelse return ed.Selection.from_cursor(&editor.get_primary().cursor),
|
||||
};
|
||||
var sel = pos.from_pos(editor.buf_root() catch return null, editor.metrics);
|
||||
var sel = pos.from_pos(editor.buf_root() catch return ed.Selection.from_cursor(&editor.get_primary().cursor), editor.metrics);
|
||||
sel.normalize();
|
||||
const cursor = editor.get_primary().cursor;
|
||||
return switch (tui.config().completion_insert_mode) {
|
||||
.insert => if (editor.get_primary().cursor.within(sel))
|
||||
.{ .begin = sel.begin, .end = cursor }
|
||||
else
|
||||
sel,
|
||||
.insert => .{ .begin = sel.begin, .end = cursor },
|
||||
.replace => sel,
|
||||
};
|
||||
}
|
||||
|
||||
fn get_insert_selection(self: *Type, values: Values, cursor: ed.Cursor) ed.Selection {
|
||||
return if (values.replace) |sel|
|
||||
sel
|
||||
else if (self.value.start.selection) |sel|
|
||||
sel
|
||||
else
|
||||
.{ .begin = self.value.start.cursor, .end = cursor };
|
||||
fn get_insert_selection(editor: *ed.Editor, values: Values) Buffer.Selection {
|
||||
return get_replacement_selection(editor, values.insert, values.replace);
|
||||
}
|
||||
|
||||
pub fn complete(self: *Type, _: ?*Type.ButtonType) !void {
|
||||
self.menu.activate_selected();
|
||||
}
|
||||
|
||||
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
|
||||
const self = menu.*.opts.ctx;
|
||||
const values = get_values(button.opts.label);
|
||||
const sel = get_insert_selection(self, values, self.value.editor.get_primary().cursor);
|
||||
const sel = get_insert_selection(self.value.editor, values);
|
||||
const text = if (values.insertText.len > 0)
|
||||
values.insertText
|
||||
else if (values.textEdit_newText.len > 0)
|
||||
|
|
|
|||
|
|
@ -152,7 +152,7 @@ fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
|
|||
values.textEdit_newText
|
||||
else
|
||||
values.label;
|
||||
editor.insert_completion(null, text, values.insertTextFormat) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
|
||||
editor.insert_completion_at_cursor(text, values.insertTextFormat) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
|
||||
const mv = tui.mainview() orelse return;
|
||||
mv.cancel_info_content() catch {};
|
||||
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue