feat: set the initial completion query based on the cursor position
This commit is contained in:
parent
74b011cf7e
commit
b02f096fef
1 changed files with 16 additions and 4 deletions
|
@ -7,6 +7,7 @@ const Buffer = @import("Buffer");
|
||||||
|
|
||||||
const tui = @import("../../tui.zig");
|
const tui = @import("../../tui.zig");
|
||||||
pub const Type = @import("palette.zig").Create(@This());
|
pub const Type = @import("palette.zig").Create(@This());
|
||||||
|
const ed = @import("../../editor.zig");
|
||||||
const module_name = @typeName(@This());
|
const module_name = @typeName(@This());
|
||||||
const Widget = @import("../../Widget.zig");
|
const Widget = @import("../../Widget.zig");
|
||||||
|
|
||||||
|
@ -22,13 +23,14 @@ pub const Entry = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ValueType = struct {
|
pub const ValueType = struct {
|
||||||
start: ?Buffer.Selection = null,
|
start: ed.CurSel = .{},
|
||||||
|
replace: ?Buffer.Selection = null,
|
||||||
};
|
};
|
||||||
pub const defaultValue: ValueType = .{};
|
pub const defaultValue: ValueType = .{};
|
||||||
|
|
||||||
pub fn load_entries(palette: *Type) !usize {
|
pub fn load_entries(palette: *Type) !usize {
|
||||||
const editor = tui.get_active_editor() orelse return error.NotFound;
|
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;
|
var iter: []const u8 = editor.completions.items;
|
||||||
while (iter.len > 0) {
|
while (iter.len > 0) {
|
||||||
var cbor_item: []const u8 = undefined;
|
var cbor_item: []const u8 = undefined;
|
||||||
|
@ -38,7 +40,9 @@ pub fn load_entries(palette: *Type) !usize {
|
||||||
|
|
||||||
var max_label_len: usize = 0;
|
var max_label_len: usize = 0;
|
||||||
for (palette.entries.items) |*item| {
|
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.label = label_;
|
||||||
item.sort_text = sort_text;
|
item.sort_text = sort_text;
|
||||||
max_label_len = @max(max_label_len, item.label.len);
|
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;
|
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 {
|
pub fn clear_entries(palette: *Type) void {
|
||||||
palette.entries.clearRetainingCapacity();
|
palette.entries.clearRetainingCapacity();
|
||||||
}
|
}
|
||||||
|
@ -133,7 +145,7 @@ pub fn updated(palette: *Type, button_: ?*Type.ButtonState) !void {
|
||||||
|
|
||||||
pub fn cancel(palette: *Type) !void {
|
pub fn cancel(palette: *Type) !void {
|
||||||
const editor = tui.get_active_editor() orelse return;
|
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) {
|
const CompletionItemKind = enum(u8) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue