Compare commits

...

5 commits

6 changed files with 96 additions and 42 deletions

View file

@ -668,6 +668,7 @@ pub fn build_exe(
exe.root_module.addImport("flags", flags_dep.module("flags"));
exe.root_module.addImport("cbor", cbor_mod);
exe.root_module.addImport("config", config_mod);
exe.root_module.addImport("text_manip", text_manip_mod);
exe.root_module.addImport("Buffer", Buffer_mod);
exe.root_module.addImport("tui", tui_mod);
exe.root_module.addImport("thespian", thespian_mod);
@ -717,6 +718,7 @@ pub fn build_exe(
check_exe.root_module.addImport("flags", flags_dep.module("flags"));
check_exe.root_module.addImport("cbor", cbor_mod);
check_exe.root_module.addImport("config", config_mod);
check_exe.root_module.addImport("text_manip", text_manip_mod);
check_exe.root_module.addImport("Buffer", Buffer_mod);
check_exe.root_module.addImport("tui", tui_mod);
check_exe.root_module.addImport("thespian", thespian_mod);

View file

@ -1867,15 +1867,6 @@ const DocumentSymbol = struct {
parent_name: []const u8 = &.{},
};
const SymbolInformation = struct {
name: []const u8,
kind: usize,
tags: ?[]const usize,
deprecated: ?bool,
location: Location,
containerName: ?[]const u8,
};
// Location is a subset of LocationLink
const Location = LocationLink;

View file

@ -225,6 +225,7 @@
["shift+f11", "toggle_highlight_columns"],
["ctrl+f11", "toggle_inspector_view"],
["f12", "goto_definition"],
["ctrl+shift+o", "show_symbols"],
["ctrl+space", "completion"],
["ctrl+.", "completion"],
["f34", "toggle_whitespace_mode"],

View file

@ -1,5 +1,8 @@
const std = @import("std");
const file_type_config = @import("file_type_config");
const text_manip = @import("text_manip");
const write_string = text_manip.write_string;
const write_padding = text_manip.write_padding;
const builtin = @import("builtin");
const RGB = @import("color").RGB;
@ -66,11 +69,6 @@ fn args_string_length(args_: ?[]const []const u8) usize {
return len;
}
fn write_string(writer: anytype, string: []const u8, pad: ?usize) !void {
try writer.writeAll(string);
if (pad) |pad_| try write_padding(writer, string.len, pad_);
}
fn write_checkmark(writer: anytype, success: bool, tty_config: std.io.tty.Config) !void {
try tty_config.setColor(writer, if (success) .green else .red);
if (success) try writer.writeAll(success_mark) else try writer.writeAll(fail_mark);
@ -98,10 +96,6 @@ fn write_segmented(
if (pad) |pad_| try write_padding(writer, len, pad_);
}
fn write_padding(writer: anytype, len: usize, pad_len: usize) !void {
for (0..pad_len - len) |_| try writer.writeAll(" ");
}
fn can_execute(allocator: std.mem.Allocator, binary_name: []const u8) bool {
const resolved_binary_path = bin_path.find_binary_in_path(allocator, binary_name) catch return false;
defer if (resolved_binary_path) |path| allocator.free(path);

View file

@ -89,3 +89,12 @@ pub fn toggle_prefix_in_text(prefix: []const u8, text: []const u8, allocator: st
}
return result.toOwnedSlice(allocator);
}
pub fn write_string(writer: anytype, string: []const u8, pad: ?usize) !void {
try writer.writeAll(string);
if (pad) |pad_| try write_padding(writer, string.len, pad_);
}
pub fn write_padding(writer: anytype, len: usize, pad_len: usize) !void {
for (0..pad_len - len) |_| try writer.writeAll(" ");
}

View file

@ -3,6 +3,9 @@ const fmt = std.fmt;
const cbor = @import("cbor");
const tp = @import("thespian");
const root = @import("soft_root").root;
const text_manip = @import("text_manip");
const write_string = text_manip.write_string;
const write_padding = text_manip.write_padding;
const command = @import("command");
const Buffer = @import("Buffer");
@ -17,6 +20,22 @@ pub const name = "Go to";
pub const description = "Symbols in scope";
pub const icon = "󱎸 ";
const Column = struct {
label: []const u8,
max_width: u8,
min_width: u8,
};
const ColumnName = enum(u8) {
Name = 0,
Container = 1,
Kind = 2,
};
const columns: [3]Column = .{
.{ .label = "Name", .max_width = 26, .min_width = 4 },
.{ .label = "Container", .max_width = 14, .min_width = 4 },
.{ .label = "Kind", .max_width = 12, .min_width = 4 },
};
pub const Entry = struct {
label: []const u8,
row: usize,
@ -25,38 +44,74 @@ pub const Entry = struct {
pub const ValueType = struct {
start: ed.CurSel = .{},
min_col_name: u8 = 26,
min_col_parent: u8 = 14,
min_col_kind: u8 = 12,
column_size: [3]u8 = undefined,
};
pub const defaultValue: ValueType = .{};
fn init_col_sizes(palette: *Type) void {
for (0..columns.len) |i| {
palette.value.column_size[i] = columns[i].min_width;
}
}
fn update_min_col_sizes(palette: *Type) void {
for (0..columns.len) |i| {
palette.value.column_size[i] = @min(columns[i].max_width, palette.value.column_size[i]);
}
}
fn update_max_col_sizes(palette: *Type, comp_sizes: []const usize) u8 {
var total_length: u8 = 0;
for (0..columns.len) |i| {
const truncated: u8 = @truncate(comp_sizes[i]);
palette.value.column_size[i] = @max(if (truncated > columns[i].max_width) columns[i].max_width else truncated, palette.value.column_size[i]);
total_length += palette.value.column_size[i];
}
return total_length;
}
fn write_columns(palette: *Type, writer: anytype, column_info: [][]const u8) void {
if (palette.value.column_size.len == 0)
return;
write_string(writer, column_info[0][0..@min(palette.value.column_size[0], column_info[0].len)], columns[0].max_width) catch {};
for (1..column_info.len) |i| {
write_padding(writer, 1, 2) catch {};
write_string(writer, column_info[i][0..@min(palette.value.column_size[i], column_info[i].len)], columns[i].max_width) catch {};
}
}
fn total_row_width() u8 {
var total_width: u8 = 0;
for (columns) |col| {
total_width += col.max_width;
}
return total_width;
}
pub fn load_entries(palette: *Type) !usize {
const mv = tui.mainview() orelse return 0;
const editor = tui.get_active_editor() orelse return error.NotFound;
var max_cols_len: u8 = 0;
var max_label_len: usize = 0;
palette.value.start = editor.get_primary().*;
var iter: []const u8 = mv.symbols.items;
init_col_sizes(palette);
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, .row = undefined };
}
const label_, const parent_, const kind, const row, _ = get_values(cbor_item);
(try palette.entries.addOne(palette.allocator)).* = .{ .cbor = cbor_item, .label = label_[0..@min(columns[0].max_width, label_.len)], .row = row };
var max_label_len: usize = 0;
var max_parent_len: usize = 0;
var max_kind_len: usize = 0;
for (palette.entries.items) |*item| {
const label_, const parent_, const kind, const row, _ = get_values(item.cbor);
item.label = label_;
item.row = row;
max_label_len = @max(max_label_len, item.label.len);
max_parent_len = @max(max_parent_len, parent_.len);
max_kind_len = @max(max_kind_len, kind_name(@enumFromInt(kind)).len);
const current_lengths: [3]usize = .{ label_.len, parent_.len, kind_name(@enumFromInt(kind)).len };
const label_len: u8 = @truncate(if (label_.len > columns[0].max_width) columns[0].max_width else label_.len);
max_cols_len = @max(max_cols_len, label_len, update_max_col_sizes(palette, &current_lengths));
max_label_len = @max(max_label_len, label_len);
}
update_min_col_sizes(palette);
palette.value.min_col_name = @min(palette.value.min_col_name, max_label_len);
palette.value.min_col_parent = @min(palette.value.min_col_parent, max_parent_len);
palette.value.min_col_kind = @min(palette.value.min_col_kind, max_kind_len);
const less_fn = struct {
fn less_fn(_: void, lhs: Entry, rhs: Entry) bool {
return lhs.row < rhs.row;
@ -64,7 +119,8 @@ pub fn load_entries(palette: *Type) !usize {
}.less_fn;
std.mem.sort(Entry, palette.entries.items, {}, less_fn);
return if (max_label_len > label.len + 3) 0 else label.len + 3 - max_label_len;
const total_width = total_row_width();
return 2 + if (max_cols_len > label.len + 3) total_width - max_label_len else label.len + 1 - max_cols_len;
}
pub fn clear_entries(palette: *Type) void {
@ -92,13 +148,14 @@ pub fn on_render_menu(palette: *Type, button: *Type.ButtonType, theme: *const Wi
const label_, const container, const kind, _, _ = get_values(item_cbor);
const icon_: []const u8 = kind_icon(@enumFromInt(kind));
const color: u24 = 0x0;
var buffer: [200]u8 = undefined;
const format_buffer = buffer[0..];
const this_kind_name = kind_name(@enumFromInt(kind));
const formatted = fmt.bufPrint(format_buffer, "{s:<26} {s:<14} {s:<12}", .{ label_[0..@min(palette.value.min_col_name, label_.len)], container[0..@min(palette.value.min_col_parent, container.len)], this_kind_name[0..@min(palette.value.min_col_kind, this_kind_name.len)] }) catch "";
var value: std.Io.Writer.Allocating = .init(palette.allocator);
defer value.deinit();
const writer = &value.writer;
var column_info = [_][]const u8{ label_, container, kind_name(@enumFromInt(kind)) };
write_columns(palette, writer, &column_info);
const indicator: []const u8 = &.{};
return tui.render_file_item(&button.plane, formatted, icon_, color, indicator, matches_cbor, button.active, selected, button.hover, theme);
return tui.render_file_item(&button.plane, value.written(), icon_, color, indicator, matches_cbor, button.active, selected, button.hover, theme);
}
fn get_values(item_cbor: []const u8) struct { []const u8, []const u8, u8, usize, usize } {