feat: set the initial completion query based on the cursor position

This commit is contained in:
CJ van den Berg 2025-10-01 21:55:12 +02:00
parent 74b011cf7e
commit b02f096fef
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -7,6 +7,7 @@ const Buffer = @import("Buffer");
const tui = @import("../../tui.zig");
pub const Type = @import("palette.zig").Create(@This());
const ed = @import("../../editor.zig");
const module_name = @typeName(@This());
const Widget = @import("../../Widget.zig");
@ -22,13 +23,14 @@ pub const Entry = struct {
};
pub const ValueType = struct {
start: ?Buffer.Selection = null,
start: ed.CurSel = .{},
replace: ?Buffer.Selection = null,
};
pub const defaultValue: ValueType = .{};
pub fn load_entries(palette: *Type) !usize {
const editor = tui.get_active_editor() orelse return error.NotFound;
palette.value.start = editor.get_primary().selection;
palette.value.start = editor.get_primary().*;
var iter: []const u8 = editor.completions.items;
while (iter.len > 0) {
var cbor_item: []const u8 = undefined;
@ -38,7 +40,9 @@ pub fn load_entries(palette: *Type) !usize {
var max_label_len: usize = 0;
for (palette.entries.items) |*item| {
const label_, const sort_text, _, _ = get_values(item.cbor);
const label_, const sort_text, _, const replace = get_values(item.cbor);
if (palette.value.replace == null)
palette.value.replace = replace;
item.label = label_;
item.sort_text = sort_text;
max_label_len = @max(max_label_len, item.label.len);
@ -56,6 +60,14 @@ pub fn load_entries(palette: *Type) !usize {
return if (max_label_len > label.len + 3) 0 else label.len + 3 - max_label_len;
}
pub fn initial_query(palette: *Type, allocator: std.mem.Allocator) error{OutOfMemory}![]const u8 {
return if (palette.value.replace) |replace| blk: {
const editor = tui.get_active_editor() orelse break :blk allocator.dupe(u8, "");
const sel: Buffer.Selection = .{ .begin = replace.begin, .end = palette.value.start.cursor };
break :blk editor.get_selection(sel, allocator) catch break :blk allocator.dupe(u8, "");
} else allocator.dupe(u8, "");
}
pub fn clear_entries(palette: *Type) void {
palette.entries.clearRetainingCapacity();
}
@ -133,7 +145,7 @@ pub fn updated(palette: *Type, button_: ?*Type.ButtonState) !void {
pub fn cancel(palette: *Type) !void {
const editor = tui.get_active_editor() orelse return;
editor.get_primary().selection = palette.value.start;
editor.get_primary().selection = palette.value.start.selection;
}
const CompletionItemKind = enum(u8) {