refactor: major re-work of completion to edit via buffer instead of query

This means completion no longer changes the buffer in anyway until a
completion menu entry is actually selected. This simplifies (or eliminates)
many edge cases and greatly improves multi-cursor support.
This commit is contained in:
CJ van den Berg 2026-01-30 11:20:48 +01:00
parent 211648b2c9
commit 518af3ab45
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
6 changed files with 107 additions and 244 deletions

View file

@ -30,6 +30,7 @@ pub const Entry = struct {
};
pub const ValueType = struct {
editor: *ed.Editor = undefined,
start: ed.CurSel = .{},
cursor: ed.Cursor = .{},
view: ed.View = .{},
@ -40,6 +41,13 @@ pub const defaultValue: ValueType = .{};
var max_description: usize = 0;
pub fn init(self: *Type) error{ Stop, OutOfMemory }!void {
try self.value.commands.init(self);
self.value.editor = tui.get_active_editor() orelse return error.Stop;
self.value.cursor = self.value.editor.get_primary().cursor;
self.value.view = self.value.editor.view;
}
pub fn load_entries(self: *Type) !usize {
max_description = 0;
var max_label_len: usize = 0;
@ -47,9 +55,9 @@ pub fn load_entries(self: *Type) !usize {
var existing: std.StringHashMapUnmanaged(void) = .empty;
defer existing.deinit(self.allocator);
const editor = tui.get_active_editor() orelse return error.NotFound;
self.value.start = editor.get_primary().*;
var iter: []const u8 = editor.completions.data.items;
self.value.start = self.value.editor.get_primary().*;
self.value.replace = null;
var iter: []const u8 = self.value.editor.completions.data.items;
while (iter.len > 0) {
var cbor_item: []const u8 = undefined;
if (!try cbor.matchValue(&iter, cbor.extract_cbor(&cbor_item))) return error.BadCompletion;
@ -59,7 +67,7 @@ pub fn load_entries(self: *Type) !usize {
if (existing.contains(dup_text)) continue;
try existing.put(self.allocator, dup_text, {});
if (self.value.replace == null) if (get_replace_selection(values.replace)) |replace| {
if (self.value.replace == null) if (get_replace_selection(self.value.editor, values)) |replace| {
self.value.replace = replace;
};
const item = try self.entries.addOne(self.allocator);
@ -100,56 +108,39 @@ pub fn handle_event(self: *Type, _: tp.pid_ref, m: tp.message) tp.result {
try m.match(.{ "E", "pos", tp.more }) or
try m.match(.{ "E", "close" }))
{
const editor = tui.get_active_editor() orelse return;
if (!self.value.cursor.eql(editor.get_primary().cursor) or !self.value.view.eql(editor.view)) {
const cursor = self.value.editor.get_primary().cursor;
if (self.value.cursor.row != cursor.row or
self.value.cursor.col > cursor.col or
!self.value.view.eql(self.value.editor.view))
{
tp.self_pid().send(.{ "cmd", "palette_menu_cancel" }) catch |e| self.logger.err(module_name, e);
} else {
update_query(self, cursor) catch |e| self.logger.err(module_name, e);
}
}
}
pub fn initial_query(self: *Type, allocator: std.mem.Allocator) error{OutOfMemory}![]const u8 {
try self.value.commands.init(self);
const editor = tui.get_active_editor() orelse return allocator.dupe(u8, "");
self.value.cursor = editor.get_primary().cursor;
self.value.view = editor.view;
pub fn initial_query(self: *Type, allocator: std.mem.Allocator) error{ Stop, OutOfMemory }![]const u8 {
return get_query_text(self, self.value.cursor, allocator);
}
fn get_query_text(self: *Type, cursor: ed.Cursor, allocator: std.mem.Allocator) error{ Stop, OutOfMemory }![]const u8 {
return if (self.value.replace) |replace| blk: {
const sel: Buffer.Selection = .{ .begin = replace.begin, .end = self.value.start.cursor };
break :blk editor.get_selection(sel, allocator) catch break :blk allocator.dupe(u8, "");
const sel: Buffer.Selection = .{ .begin = replace.begin, .end = cursor };
break :blk try self.value.editor.get_selection(sel, allocator);
} else allocator.dupe(u8, "");
}
pub fn update_query(self: *Type, query: []const u8) void {
const editor = tui.get_active_editor() orelse return;
const sel = get_insert_selection(self, editor.get_primary().cursor);
editor.update_completion_cursels(sel, query) catch |e| self.logger.err(module_name, e);
const primary = editor.get_primary();
self.value.cursor = primary.cursor;
self.value.view = editor.view;
if (self.value.replace) |*s| s.* = .{ .begin = s.begin, .end = self.value.cursor };
fn update_query(self: *Type, cursor: ed.Cursor) error{OutOfMemory}!void {
const query = get_query_text(self, cursor, self.allocator) catch |e| switch (e) {
error.Stop => return,
else => |e_| return e_,
};
defer self.allocator.free(query);
tp.self_pid().send(.{ "cmd", "completion" }) catch |e| self.logger.err(module_name, e);
return;
}
pub fn delete_word_empty(self: *Type) void {
cancel(self) catch return;
tp.self_pid().send(.{ "cmd", "delete_word_left" }) catch |e| self.logger.err(module_name, e);
}
pub fn delete_empty(self: *Type) void {
cancel(self) catch return;
tp.self_pid().send(.{ "cmd", "smart_delete_backward" }) catch |e| self.logger.err(module_name, e);
}
fn get_insert_selection(self: *Type, cursor: ed.Cursor) ed.Selection {
return if (self.value.replace) |sel|
sel
else if (self.value.start.selection) |sel|
sel
else
.{ .begin = self.value.start.cursor, .end = cursor };
}
pub fn clear_entries(self: *Type) void {
self.entries.clearRetainingCapacity();
}
@ -164,7 +155,7 @@ pub fn add_menu_entry(self: *Type, entry: *Entry, matches: ?[]const usize) !void
self.items += 1;
}
pub fn on_render_menu(_: *Type, button: *Type.ButtonType, theme: *const Widget.Theme, selected: bool) bool {
pub fn on_render_menu(self: *Type, button: *Type.ButtonType, theme: *const Widget.Theme, selected: bool) bool {
var item_cbor: []const u8 = undefined;
var matches_cbor: []const u8 = undefined;
@ -177,7 +168,7 @@ pub fn on_render_menu(_: *Type, button: *Type.ButtonType, theme: *const Widget.T
const color: u24 = 0x0;
if (tui.config().enable_terminal_cursor) blk: {
const cursor = (tui.get_active_editor() orelse break :blk).get_primary_abs() orelse break :blk;
const cursor = self.value.editor.get_primary_abs() orelse break :blk;
tui.rdr().cursor_enable(@intCast(cursor.row), @intCast(cursor.col), tui.get_cursor_shape()) catch {};
}
@ -199,11 +190,12 @@ pub fn on_render_menu(_: *Type, button: *Type.ButtonType, theme: *const Widget.T
);
}
const Values = struct {
pub const Values = struct {
label: []const u8,
sort_text: []const u8,
kind: CompletionItemKind,
replace: Buffer.Selection,
insert: ?Buffer.Selection,
replace: ?Buffer.Selection,
additionalTextEdits: []const u8,
label_detail: []const u8,
label_description: []const u8,
@ -214,7 +206,7 @@ const Values = struct {
textEdit_newText: []const u8,
};
fn get_values(item_cbor: []const u8) Values {
pub fn get_values(item_cbor: []const u8) Values {
var label_: []const u8 = "";
var label_detail: []const u8 = "";
var label_description: []const u8 = "";
@ -225,7 +217,8 @@ fn get_values(item_cbor: []const u8) Values {
var insertText: []const u8 = "";
var insertTextFormat: usize = 0;
var textEdit_newText: []const u8 = "";
var replace: Buffer.Selection = .{};
var insert_cbor: []const u8 = &.{};
var replace_cbor: []const u8 = &.{};
var additionalTextEdits: []const u8 = &.{};
_ = cbor.match(item_cbor, .{
cbor.any, // file_path
@ -243,21 +236,16 @@ fn get_values(item_cbor: []const u8) Values {
cbor.extract(&insertText), // insertText
cbor.extract(&insertTextFormat), // insertTextFormat
cbor.extract(&textEdit_newText), // textEdit_newText
cbor.any, // insert.begin.row
cbor.any, // insert.begin.col
cbor.any, // insert.end.row
cbor.any, // insert.end.col
cbor.extract(&replace.begin.row), // replace.begin.row
cbor.extract(&replace.begin.col), // replace.begin.col
cbor.extract(&replace.end.row), // replace.end.row
cbor.extract(&replace.end.col), // replace.end.col
cbor.extract_cbor(&insert_cbor),
cbor.extract_cbor(&replace_cbor),
cbor.extract_cbor(&additionalTextEdits),
}) catch false;
return .{
.label = label_,
.sort_text = sort_text,
.kind = @enumFromInt(kind),
.replace = replace,
.insert = get_range(insert_cbor),
.replace = get_range(replace_cbor),
.additionalTextEdits = additionalTextEdits,
.label_detail = label_detail,
.label_description = label_description,
@ -269,37 +257,53 @@ fn get_values(item_cbor: []const u8) Values {
};
}
fn get_range(range_cbor: []const u8) ?Buffer.Selection {
var range: Buffer.Selection = .{};
return if (cbor.match(range_cbor, tp.null_) catch false)
null
else if (cbor.match(range_cbor, .{
cbor.extract(&range.begin.row),
cbor.extract(&range.begin.col),
cbor.extract(&range.end.row),
cbor.extract(&range.end.col),
}) catch false)
range
else
null;
}
const TextEdit = struct { newText: []const u8 = &.{}, insert: ?Range = null, replace: ?Range = null };
const Range = struct { start: Position, end: Position };
const Position = struct { line: usize, character: usize };
fn get_replace_selection(replace: Buffer.Selection) ?Buffer.Selection {
return if (replace.empty())
null
else if (tui.get_active_editor()) |edt|
edt.get_completion_replacement_selection(replace)
else
replace;
pub fn get_replace_selection(editor: *ed.Editor, values: Values) ?Buffer.Selection {
return editor.get_completion_replacement_selection(values.insert, values.replace);
}
pub fn complete(self: *Type, _: ?*Type.ButtonType) !void {
self.menu.activate_selected();
}
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 select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
const self = menu.*.opts.ctx;
const values = get_values(button.opts.label);
const editor = tui.get_active_editor() orelse return;
const sel = get_insert_selection(self, editor.get_primary().cursor);
const sel = get_insert_selection(self, values, self.value.editor.get_primary().cursor);
const text = if (values.insertText.len > 0)
values.insertText
else if (values.textEdit_newText.len > 0)
values.textEdit_newText
else
values.label;
editor.insert_completion(sel, text, values.insertTextFormat) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
self.value.cursor = editor.get_primary().cursor;
self.value.view = editor.view;
self.value.editor.insert_completion(sel, 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| self.logger.err(module_name, e);