feat: display label_detail and label_description in completion palette

This commit is contained in:
CJ van den Berg 2025-12-08 17:44:06 +01:00
parent 852f456bf0
commit d86193a9aa
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 75 additions and 18 deletions

View file

@ -19,7 +19,6 @@ pub const icon = "󱎸 ";
pub const Entry = struct {
label: []const u8,
sort_text: []const u8,
detail: []const u8,
cbor: []const u8,
};
@ -29,7 +28,7 @@ pub const ValueType = struct {
};
pub const defaultValue: ValueType = .{};
var max_detail: usize = 0;
var max_description: usize = 0;
pub fn load_entries(palette: *Type) !usize {
const editor = tui.get_active_editor() orelse return error.NotFound;
@ -38,21 +37,24 @@ pub fn load_entries(palette: *Type) !usize {
while (iter.len > 0) {
var cbor_item: []const u8 = undefined;
if (!try cbor.matchValue(&iter, cbor.extract_cbor(&cbor_item))) return error.BadCompletion;
(try palette.entries.addOne(palette.allocator)).* = .{ .cbor = cbor_item, .label = undefined, .sort_text = undefined, .detail = undefined };
(try palette.entries.addOne(palette.allocator)).* = .{ .cbor = cbor_item, .label = undefined, .sort_text = undefined };
}
max_detail = 0;
max_description = 0;
var max_label_len: usize = 0;
for (palette.entries.items) |*item| {
const label_, const sort_text, _, const maybe_replace, _, const detail, _ = get_values(item.cbor);
const label_, const sort_text, _, const maybe_replace, _, const label_detail, const label_description, _, _ = get_values(item.cbor);
if (get_replace_selection(maybe_replace)) |replace| {
if (palette.value.replace == null) palette.value.replace = replace;
}
item.label = label_;
item.sort_text = sort_text;
item.detail = detail;
var lines = std.mem.splitScalar(u8, label_description, '\n');
const label_description_len = if (lines.next()) |desc| desc.len else label_description.len;
max_label_len = @max(max_label_len, item.label.len);
max_detail = @max(max_detail, item.detail.len);
max_description = @max(max_description, label_description_len + label_detail.len);
}
const less_fn = struct {
@ -64,8 +66,8 @@ pub fn load_entries(palette: *Type) !usize {
}.less_fn;
std.mem.sort(Entry, palette.entries.items, {}, less_fn);
max_detail = @min(max_detail, tui.screen().w -| max_label_len -| 10);
return @max(max_detail, if (max_label_len > label.len + 3) 0 else label.len + 3 - max_label_len);
max_description = @min(max_description, tui.screen().w -| max_label_len -| 10);
return @max(max_description, 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 {
@ -90,6 +92,8 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
palette.items += 1;
}
//pub fn render_symbol( self: *renderer.Plane, symbol: []const u8, icon: []const u8, color: u24, detail: []const u8, description: []const u8, matches_cbor: []const u8, active: bool, selected: bool, hover: bool, theme_: *const Widget.Theme, ) bool
pub fn on_render_menu(_: *Type, button: *Type.ButtonType, theme: *const Widget.Theme, selected: bool) bool {
var item_cbor: []const u8 = undefined;
var matches_cbor: []const u8 = undefined;
@ -98,18 +102,17 @@ pub fn on_render_menu(_: *Type, button: *Type.ButtonType, theme: *const Widget.T
if (!(cbor.matchValue(&iter, cbor.extract_cbor(&item_cbor)) catch false)) return false;
if (!(cbor.matchValue(&iter, cbor.extract_cbor(&matches_cbor)) catch false)) return false;
const label_, _, const kind, _, _, const detail, _ = get_values(item_cbor);
const label_, _, const kind, _, _, const label_detail, const label_description, _, _ = get_values(item_cbor);
const icon_: []const u8 = kind_icon(@enumFromInt(kind));
const color: u24 = 0x0;
const indicator: []const u8 = detail;
return tui.render_file_item(
return tui.render_symbol(
&button.plane,
label_,
icon_,
color,
indicator[0..@min(max_detail - 3, indicator.len)],
if (max_detail < indicator.len) "" else "",
label_detail,
label_description,
matches_cbor,
button.active,
selected,
@ -118,7 +121,7 @@ pub fn on_render_menu(_: *Type, button: *Type.ButtonType, theme: *const Widget.T
);
}
fn get_values(item_cbor: []const u8) struct { []const u8, []const u8, u8, Buffer.Selection, []const u8, []const u8, []const u8 } {
fn get_values(item_cbor: []const u8) struct { []const u8, []const u8, u8, Buffer.Selection, []const u8, []const u8, []const u8, []const u8, []const u8 } {
var label_: []const u8 = "";
var label_detail: []const u8 = "";
var label_description: []const u8 = "";
@ -155,7 +158,7 @@ fn get_values(item_cbor: []const u8) struct { []const u8, []const u8, u8, Buffer
cbor.extract(&replace.end.col), // replace.end.col
cbor.extract_cbor(&additionalTextEdits),
}) catch false;
return .{ label_, sort_text, kind, replace, additionalTextEdits, if (detail.len > 0) detail else label_detail, documentation };
return .{ label_, sort_text, kind, replace, additionalTextEdits, label_detail, label_description, detail, documentation };
}
const TextEdit = struct { newText: []const u8 = &.{}, insert: ?Range = null, replace: ?Range = null };
@ -172,7 +175,7 @@ fn get_replace_selection(replace: Buffer.Selection) ?Buffer.Selection {
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
const label_, _, _, _, _, _, _ = get_values(button.opts.label);
const label_, _, _, _, _, _, _, _, _ = get_values(button.opts.label);
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", "insert_chars", .{label_} }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
const mv = tui.mainview() orelse return;
@ -181,7 +184,7 @@ fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
pub fn updated(palette: *Type, button_: ?*Type.ButtonType) !void {
const button = button_ orelse return cancel(palette);
_, _, _, const replace, _, const detail, const documentation = get_values(button.opts.label);
_, _, _, const replace, _, _, _, const detail, const documentation = get_values(button.opts.label);
const editor = tui.get_active_editor() orelse return error.NotFound;
editor.get_primary().selection = get_replace_selection(replace);