Compare commits
11 commits
310dadb9be
...
f9bbb55814
| Author | SHA1 | Date | |
|---|---|---|---|
| f9bbb55814 | |||
| ae6df9dba8 | |||
| fc244eabb6 | |||
| 70c2673cfe | |||
| 1d06b71102 | |||
| 0e994e9f25 | |||
| 99e32520ad | |||
| 53391aa7b5 | |||
| 9597dd7b6d | |||
| f26d0534de | |||
| c8ed34701c |
4 changed files with 71 additions and 20 deletions
|
|
@ -5711,6 +5711,37 @@ pub const Editor = struct {
|
||||||
}
|
}
|
||||||
pub const goto_line_and_column_meta: Meta = .{ .arguments = &.{ .integer, .integer } };
|
pub const goto_line_and_column_meta: Meta = .{ .arguments = &.{ .integer, .integer } };
|
||||||
|
|
||||||
|
pub fn focus_on_range(self: *Self, ctx: Context) Result {
|
||||||
|
var sel: Selection = .{};
|
||||||
|
var pos_type: PosType = .column;
|
||||||
|
if (!try ctx.args.match(.{
|
||||||
|
tp.extract(&sel.begin.row),
|
||||||
|
tp.extract(&sel.begin.col),
|
||||||
|
tp.extract(&sel.end.row),
|
||||||
|
tp.extract(&sel.end.col),
|
||||||
|
tp.extract(&pos_type),
|
||||||
|
})) return error.InvalidFocusOnRangeArgument;
|
||||||
|
|
||||||
|
self.cancel_all_selections();
|
||||||
|
const root = self.buf_root() catch return;
|
||||||
|
if (pos_type == .byte)
|
||||||
|
sel = sel.from_pos(root, self.metrics) catch return;
|
||||||
|
const primary = self.get_primary();
|
||||||
|
try primary.cursor.move_to(
|
||||||
|
root,
|
||||||
|
sel.begin.row,
|
||||||
|
sel.begin.col,
|
||||||
|
self.metrics,
|
||||||
|
);
|
||||||
|
primary.selection = sel;
|
||||||
|
if (self.view.is_visible(&primary.cursor))
|
||||||
|
self.clamp()
|
||||||
|
else
|
||||||
|
try self.scroll_view_center(.{});
|
||||||
|
self.need_render();
|
||||||
|
}
|
||||||
|
pub const focus_on_range_meta: Meta = .{ .arguments = &.{ .integer, .integer, .integer, .integer } };
|
||||||
|
|
||||||
pub fn goto_byte_offset(self: *Self, ctx: Context) Result {
|
pub fn goto_byte_offset(self: *Self, ctx: Context) Result {
|
||||||
try self.send_editor_jump_source();
|
try self.send_editor_jump_source();
|
||||||
var offset: usize = 0;
|
var offset: usize = 0;
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,7 @@ pub fn Create(options: type) type {
|
||||||
longest_hint: usize = 0,
|
longest_hint: usize = 0,
|
||||||
initial_selected: ?usize = null,
|
initial_selected: ?usize = null,
|
||||||
placement: Placement,
|
placement: Placement,
|
||||||
|
quick_activate_enabled: bool = true,
|
||||||
|
|
||||||
items: usize = 0,
|
items: usize = 0,
|
||||||
view_rows: usize,
|
view_rows: usize,
|
||||||
|
|
@ -77,7 +78,7 @@ pub fn Create(options: type) type {
|
||||||
else
|
else
|
||||||
ModalBackground.Options(*Self).on_render_default
|
ModalBackground.Options(*Self).on_render_default
|
||||||
else
|
else
|
||||||
ModalBackground.Options(*Self).on_render_default,
|
ModalBackground.Options(*Self).on_render_dim,
|
||||||
}),
|
}),
|
||||||
.menu = try Menu.create(*Self, allocator, tui.plane(), .{
|
.menu = try Menu.create(*Self, allocator, tui.plane(), .{
|
||||||
.ctx = self,
|
.ctx = self,
|
||||||
|
|
@ -111,8 +112,6 @@ pub fn Create(options: type) type {
|
||||||
try options.load_entries_with_args(self, ctx)
|
try options.load_entries_with_args(self, ctx)
|
||||||
else
|
else
|
||||||
try options.load_entries(self);
|
try options.load_entries(self);
|
||||||
if (self.entries.items.len > 0)
|
|
||||||
self.initial_selected = self.menu.selected;
|
|
||||||
if (@hasDecl(options, "restore_state"))
|
if (@hasDecl(options, "restore_state"))
|
||||||
options.restore_state(self) catch {};
|
options.restore_state(self) catch {};
|
||||||
if (@hasDecl(options, "initial_query")) blk: {
|
if (@hasDecl(options, "initial_query")) blk: {
|
||||||
|
|
@ -555,7 +554,9 @@ pub fn Create(options: type) type {
|
||||||
pub const palette_menu_activate_meta: Meta = .{};
|
pub const palette_menu_activate_meta: Meta = .{};
|
||||||
|
|
||||||
pub fn palette_menu_activate_quick(self: *Self, _: Ctx) Result {
|
pub fn palette_menu_activate_quick(self: *Self, _: Ctx) Result {
|
||||||
|
if (!self.quick_activate_enabled) return;
|
||||||
if (self.menu.selected orelse 0 > 0) self.menu.activate_selected();
|
if (self.menu.selected orelse 0 > 0) self.menu.activate_selected();
|
||||||
|
self.quick_activate_enabled = false;
|
||||||
}
|
}
|
||||||
pub const palette_menu_activate_quick_meta: Meta = .{};
|
pub const palette_menu_activate_quick_meta: Meta = .{};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ const columns: [3]Column = .{
|
||||||
|
|
||||||
pub const Entry = struct {
|
pub const Entry = struct {
|
||||||
label: []const u8,
|
label: []const u8,
|
||||||
row: usize,
|
range: ed.Selection,
|
||||||
cbor: []const u8,
|
cbor: []const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -104,8 +104,8 @@ 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;
|
||||||
const label_, const parent_, const kind, const row, _ = get_values(cbor_item);
|
const label_, const parent_, const kind, const sel = 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 };
|
(try palette.entries.addOne(palette.allocator)).* = .{ .cbor = cbor_item, .label = label_[0..@min(columns[0].max_width, label_.len)], .range = sel };
|
||||||
|
|
||||||
const current_lengths: [3]usize = .{ label_.len, parent_.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);
|
const label_len: u8 = @truncate(if (label_.len > columns[0].max_width) columns[0].max_width else label_.len);
|
||||||
|
|
@ -116,11 +116,14 @@ pub fn load_entries(palette: *Type) !usize {
|
||||||
|
|
||||||
const less_fn = struct {
|
const less_fn = struct {
|
||||||
fn less_fn(_: void, lhs: Entry, rhs: Entry) bool {
|
fn less_fn(_: void, lhs: Entry, rhs: Entry) bool {
|
||||||
return lhs.row < rhs.row;
|
return lhs.range.begin.row < rhs.range.begin.row;
|
||||||
}
|
}
|
||||||
}.less_fn;
|
}.less_fn;
|
||||||
std.mem.sort(Entry, palette.entries.items, {}, less_fn);
|
std.mem.sort(Entry, palette.entries.items, {}, less_fn);
|
||||||
|
|
||||||
|
palette.initial_selected = find_closest(palette);
|
||||||
|
palette.quick_activate_enabled = false;
|
||||||
|
|
||||||
const total_width = total_row_width();
|
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;
|
return 2 + if (max_cols_len > label.len + 3) total_width - max_label_len else label.len + 1 - max_cols_len;
|
||||||
}
|
}
|
||||||
|
|
@ -147,7 +150,7 @@ pub fn on_render_menu(palette: *Type, button: *Type.ButtonType, theme: *const Wi
|
||||||
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 container, const kind, _, _ = get_values(item_cbor);
|
const label_, const container, const kind, _ = 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;
|
||||||
var value: std.Io.Writer.Allocating = .init(palette.allocator);
|
var value: std.Io.Writer.Allocating = .init(palette.allocator);
|
||||||
|
|
@ -160,21 +163,20 @@ pub fn on_render_menu(palette: *Type, button: *Type.ButtonType, theme: *const Wi
|
||||||
return tui.render_file_item(&button.plane, value.written(), 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 } {
|
fn get_values(item_cbor: []const u8) struct { []const u8, []const u8, u8, ed.Selection } {
|
||||||
var label_: []const u8 = "";
|
var label_: []const u8 = "";
|
||||||
var container: []const u8 = "";
|
var container: []const u8 = "";
|
||||||
var kind: u8 = 0;
|
var kind: u8 = 0;
|
||||||
var row: usize = 0;
|
var range: ed.Selection = .{};
|
||||||
var col: usize = 0;
|
|
||||||
_ = cbor.match(item_cbor, .{
|
_ = cbor.match(item_cbor, .{
|
||||||
cbor.any, // file_path
|
cbor.any, // file_path
|
||||||
cbor.extract(&label_), // name
|
cbor.extract(&label_), // name
|
||||||
cbor.extract(&container), // parent_name
|
cbor.extract(&container), // parent_name
|
||||||
cbor.extract(&kind), // kind
|
cbor.extract(&kind), // kind
|
||||||
cbor.extract(&row), // range.begin.row
|
cbor.extract(&range.begin.row), // range.begin.row
|
||||||
cbor.extract(&col), // range.begin.col
|
cbor.extract(&range.begin.col), // range.begin.col
|
||||||
cbor.any, // range.end.row
|
cbor.extract(&range.end.row), // range.end.row
|
||||||
cbor.any, // range.end.col
|
cbor.extract(&range.end.col), // range.end.col
|
||||||
cbor.any, // tags
|
cbor.any, // tags
|
||||||
cbor.any, // selectionRange.begin.row
|
cbor.any, // selectionRange.begin.row
|
||||||
cbor.any, // selectionRange.begin.col
|
cbor.any, // selectionRange.begin.col
|
||||||
|
|
@ -183,19 +185,33 @@ fn get_values(item_cbor: []const u8) struct { []const u8, []const u8, u8, usize,
|
||||||
cbor.any, // deprecated
|
cbor.any, // deprecated
|
||||||
cbor.any, // detail
|
cbor.any, // detail
|
||||||
}) catch false;
|
}) catch false;
|
||||||
return .{ label_, container, kind, row, col };
|
return .{ label_, container, kind, range };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_closest(palette: *Type) ?usize {
|
||||||
|
const editor = tui.get_active_editor() orelse return null;
|
||||||
|
const cursor = editor.get_primary().cursor;
|
||||||
|
var previous: usize = 0;
|
||||||
|
for (palette.entries.items, 0..) |entry, idx| {
|
||||||
|
_, _, _, const sel = get_values(entry.cbor);
|
||||||
|
if (cursor.within(sel))
|
||||||
|
return idx + 1;
|
||||||
|
if (cursor.row < sel.begin.row) return previous + 1;
|
||||||
|
previous = idx;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
|
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
|
||||||
_, _, _, const row, const col = get_values(button.opts.label);
|
_, _, _, const sel = 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", "goto_line_and_column", .{ row + 1, col + 1 } }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
|
tp.self_pid().send(.{ "cmd", "goto_line_and_column", .{ sel.begin.row + 1, sel.begin.col + 1 } }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 row, const col = get_values(button.opts.label);
|
_, _, _, const sel = get_values(button.opts.label);
|
||||||
tp.self_pid().send(.{ "cmd", "goto_line_and_column", .{ row + 1, col + 1 } }) catch {};
|
tp.self_pid().send(.{ "cmd", "focus_on_range", .{ sel.begin.row, sel.begin.col, sel.end.row, sel.end.col, ed.PosType.byte } }) catch {};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cancel(palette: *Type) !void {
|
pub fn cancel(palette: *Type) !void {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ pub const Type = @import("palette.zig").Create(@This());
|
||||||
pub const label = "Search themes";
|
pub const label = "Search themes";
|
||||||
pub const name = " theme";
|
pub const name = " theme";
|
||||||
pub const description = "theme";
|
pub const description = "theme";
|
||||||
|
pub const modal_dim = false;
|
||||||
|
pub const placement = .top_right;
|
||||||
|
|
||||||
pub const Entry = struct {
|
pub const Entry = struct {
|
||||||
label: []const u8,
|
label: []const u8,
|
||||||
|
|
@ -39,6 +41,7 @@ pub fn load_entries(palette: *Type) !usize {
|
||||||
};
|
};
|
||||||
longest_hint = @max(longest_hint, theme.name.len);
|
longest_hint = @max(longest_hint, theme.name.len);
|
||||||
}
|
}
|
||||||
|
palette.quick_activate_enabled = false;
|
||||||
return longest_hint;
|
return longest_hint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue