refactor: simplify completion selection functions

This commit is contained in:
CJ van den Berg 2026-01-30 14:20:06 +01:00
parent afe39f118b
commit 8c8388c0c2
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 21 additions and 24 deletions

View file

@ -4975,15 +4975,10 @@ pub const Editor = struct {
}; };
} }
pub fn insert_completion(self: *Self, sel_: ?Selection, text: []const u8, insertTextFormat: usize) Result { 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);
if (self.has_secondary_cursors()) if (self.has_secondary_cursors())
self.replicate_selection(sel); self.replicate_selection(sel);
primary.selection = sel; self.get_primary().selection = sel;
switch (insertTextFormat) { switch (insertTextFormat) {
2 => try self.insert_snippet(text), 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 { pub fn update_completion_cursels(self: *Self, sel: Selection, text: []const u8) Result {
const b = self.buf_for_update() catch return; const b = self.buf_for_update() catch return;
if (self.has_secondary_cursors()) if (self.has_secondary_cursors())

View file

@ -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); 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) { const pos = switch (tui.config().completion_insert_mode) {
.replace => replace_ orelse insert_ orelse return null, .replace => replace_ orelse insert_ orelse return ed.Selection.from_cursor(&editor.get_primary().cursor),
.insert => insert_ orelse replace_ orelse return null, .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(); sel.normalize();
const cursor = editor.get_primary().cursor; const cursor = editor.get_primary().cursor;
return switch (tui.config().completion_insert_mode) { return switch (tui.config().completion_insert_mode) {
.insert => if (editor.get_primary().cursor.within(sel)) .insert => .{ .begin = sel.begin, .end = cursor },
.{ .begin = sel.begin, .end = cursor }
else
sel,
.replace => sel, .replace => sel,
}; };
} }
fn get_insert_selection(self: *Type, values: Values, cursor: ed.Cursor) ed.Selection { fn get_insert_selection(editor: *ed.Editor, values: Values) Buffer.Selection {
return if (values.replace) |sel| return get_replacement_selection(editor, values.insert, values.replace);
sel }
else if (self.value.start.selection) |sel|
sel pub fn complete(self: *Type, _: ?*Type.ButtonType) !void {
else self.menu.activate_selected();
.{ .begin = self.value.start.cursor, .end = cursor };
} }
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void { fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
const self = menu.*.opts.ctx; const self = menu.*.opts.ctx;
const values = get_values(button.opts.label); 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) const text = if (values.insertText.len > 0)
values.insertText values.insertText
else if (values.textEdit_newText.len > 0) else if (values.textEdit_newText.len > 0)

View file

@ -152,7 +152,7 @@ fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
values.textEdit_newText values.textEdit_newText
else else
values.label; 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; const mv = tui.mainview() orelse return;
mv.cancel_info_content() catch {}; mv.cancel_info_content() catch {};
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e); tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);