feat: display label_detail and label_description in completion palette
This commit is contained in:
parent
852f456bf0
commit
d86193a9aa
2 changed files with 75 additions and 18 deletions
|
|
@ -19,7 +19,6 @@ pub const icon = " ";
|
||||||
pub const Entry = struct {
|
pub const Entry = struct {
|
||||||
label: []const u8,
|
label: []const u8,
|
||||||
sort_text: []const u8,
|
sort_text: []const u8,
|
||||||
detail: []const u8,
|
|
||||||
cbor: []const u8,
|
cbor: []const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -29,7 +28,7 @@ pub const ValueType = struct {
|
||||||
};
|
};
|
||||||
pub const defaultValue: ValueType = .{};
|
pub const defaultValue: ValueType = .{};
|
||||||
|
|
||||||
var max_detail: usize = 0;
|
var max_description: usize = 0;
|
||||||
|
|
||||||
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;
|
||||||
|
|
@ -38,21 +37,24 @@ pub fn load_entries(palette: *Type) !usize {
|
||||||
while (iter.len > 0) {
|
while (iter.len > 0) {
|
||||||
var cbor_item: []const u8 = undefined;
|
var cbor_item: []const u8 = undefined;
|
||||||
if (!try cbor.matchValue(&iter, cbor.extract_cbor(&cbor_item))) return error.BadCompletion;
|
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;
|
var max_label_len: usize = 0;
|
||||||
for (palette.entries.items) |*item| {
|
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 (get_replace_selection(maybe_replace)) |replace| {
|
||||||
if (palette.value.replace == null) palette.value.replace = replace;
|
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;
|
||||||
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_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 {
|
const less_fn = struct {
|
||||||
|
|
@ -64,8 +66,8 @@ pub fn load_entries(palette: *Type) !usize {
|
||||||
}.less_fn;
|
}.less_fn;
|
||||||
std.mem.sort(Entry, palette.entries.items, {}, less_fn);
|
std.mem.sort(Entry, palette.entries.items, {}, less_fn);
|
||||||
|
|
||||||
max_detail = @min(max_detail, tui.screen().w -| max_label_len -| 10);
|
max_description = @min(max_description, 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);
|
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 {
|
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;
|
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 {
|
pub fn on_render_menu(_: *Type, button: *Type.ButtonType, theme: *const Widget.Theme, selected: bool) bool {
|
||||||
var item_cbor: []const u8 = undefined;
|
var item_cbor: []const u8 = undefined;
|
||||||
var matches_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(&item_cbor)) catch false)) return false;
|
||||||
if (!(cbor.matchValue(&iter, cbor.extract_cbor(&matches_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 icon_: []const u8 = kind_icon(@enumFromInt(kind));
|
||||||
const color: u24 = 0x0;
|
const color: u24 = 0x0;
|
||||||
const indicator: []const u8 = detail;
|
|
||||||
|
|
||||||
return tui.render_file_item(
|
return tui.render_symbol(
|
||||||
&button.plane,
|
&button.plane,
|
||||||
label_,
|
label_,
|
||||||
icon_,
|
icon_,
|
||||||
color,
|
color,
|
||||||
indicator[0..@min(max_detail - 3, indicator.len)],
|
label_detail,
|
||||||
if (max_detail < indicator.len) "…" else "",
|
label_description,
|
||||||
matches_cbor,
|
matches_cbor,
|
||||||
button.active,
|
button.active,
|
||||||
selected,
|
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_: []const u8 = "";
|
||||||
var label_detail: []const u8 = "";
|
var label_detail: []const u8 = "";
|
||||||
var label_description: []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(&replace.end.col), // replace.end.col
|
||||||
cbor.extract_cbor(&additionalTextEdits),
|
cbor.extract_cbor(&additionalTextEdits),
|
||||||
}) catch false;
|
}) 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 };
|
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 {
|
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", "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);
|
tp.self_pid().send(.{ "cmd", "insert_chars", .{label_} }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
|
||||||
const mv = tui.mainview() orelse return;
|
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 {
|
pub fn updated(palette: *Type, button_: ?*Type.ButtonType) !void {
|
||||||
const button = button_ orelse return cancel(palette);
|
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;
|
const editor = tui.get_active_editor() orelse return error.NotFound;
|
||||||
editor.get_primary().selection = get_replace_selection(replace);
|
editor.get_primary().selection = get_replace_selection(replace);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2100,6 +2100,60 @@ pub fn render_file_vcs_item_cbor(self: *renderer.Plane, file_item_cbor: []const
|
||||||
return render_file_vcs_item(self, file_path_, icon, color, indicator, vcs_status, matches_cbor, active, selected, hover, theme_);
|
return render_file_vcs_item(self, file_path_, icon, color, indicator, vcs_status, matches_cbor, active, selected, hover, theme_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
const style_base = theme_.editor_widget;
|
||||||
|
const style_symbol = if (active) theme_.editor_cursor else if (hover or selected) theme_.editor_selection else theme_.editor_widget;
|
||||||
|
const style_detail: Widget.Theme.Style = .{ .bg = style_symbol.bg, .fg = theme_.input_placeholder.fg, .fs = theme_.input_placeholder.fs };
|
||||||
|
const style_description = if (find_scope_style(theme_, "entity.name")) |sty| sty.style else style_detail;
|
||||||
|
self.set_base_style(style_base);
|
||||||
|
self.erase();
|
||||||
|
self.home();
|
||||||
|
self.set_style(style_symbol);
|
||||||
|
if (active or hover or selected) {
|
||||||
|
self.fill(" ");
|
||||||
|
self.home();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.set_style(style_description);
|
||||||
|
render_pointer(self, selected);
|
||||||
|
|
||||||
|
const icon_width = render_file_icon(self, icon, color);
|
||||||
|
|
||||||
|
self.set_style(style_symbol);
|
||||||
|
_ = self.print("{s}", .{symbol}) catch {};
|
||||||
|
|
||||||
|
self.set_style(style_detail);
|
||||||
|
_ = self.print("{s}", .{detail}) catch {};
|
||||||
|
|
||||||
|
var lines = std.mem.splitScalar(u8, description, '\n');
|
||||||
|
if (lines.next()) |desc| {
|
||||||
|
self.set_style(style_description);
|
||||||
|
_ = self.print_aligned_right(0, "{s} ", .{desc}) catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
var iter = matches_cbor;
|
||||||
|
var index: usize = 0;
|
||||||
|
var len = cbor.decodeArrayHeader(&iter) catch return false;
|
||||||
|
while (len > 0) : (len -= 1) {
|
||||||
|
if (cbor.matchValue(&iter, cbor.extract(&index)) catch break) {
|
||||||
|
render_match_cell(self, 0, index + 2 + icon_width, theme_) catch break;
|
||||||
|
} else break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
fn get_or_create_theme_file(self: *Self, allocator: std.mem.Allocator) ![]const u8 {
|
fn get_or_create_theme_file(self: *Self, allocator: std.mem.Allocator) ![]const u8 {
|
||||||
const theme_name = self.current_theme().name;
|
const theme_name = self.current_theme().name;
|
||||||
if (root.read_theme(allocator, theme_name)) |content| {
|
if (root.read_theme(allocator, theme_name)) |content| {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue