From b02f096fef576f12c4be59d56a7fb7a0a7d88016 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 1 Oct 2025 21:55:12 +0200 Subject: [PATCH] feat: set the initial completion query based on the cursor position --- src/tui/mode/overlay/completion_palette.zig | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/tui/mode/overlay/completion_palette.zig b/src/tui/mode/overlay/completion_palette.zig index c335a69..095b5be 100644 --- a/src/tui/mode/overlay/completion_palette.zig +++ b/src/tui/mode/overlay/completion_palette.zig @@ -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) {