feat: add open_recent_project and change_project commands

This commit is contained in:
CJ van den Berg 2024-08-19 20:35:05 +02:00
parent 2d9e66b534
commit 231e4ccb88
8 changed files with 177 additions and 10 deletions

View file

@ -39,7 +39,7 @@ pub fn create(a: std.mem.Allocator, parent: Widget) !Widget {
try self.menu.add_item_with_handler("Help ······················· :h", menu_action_help);
try self.menu.add_item_with_handler("Open file ·················· :o", menu_action_open_file);
try self.menu.add_item_with_handler("Open recent file ··········· :e", menu_action_open_recent_file);
try self.menu.add_item_with_handler("Open recent project ·(wip)·· :r", menu_action_open_recent_project);
try self.menu.add_item_with_handler("Open recent project ········ :r", menu_action_open_recent_project);
try self.menu.add_item_with_handler("Show/Run commands ·········· :p", menu_action_show_commands);
try self.menu.add_item_with_handler("Open config file ··········· :c", menu_action_open_config);
try self.menu.add_item_with_handler("Change theme ··············· :t", menu_action_change_theme);
@ -116,7 +116,7 @@ fn menu_action_open_recent_file(_: **Menu.State(*Self), _: *Button.State(*Menu.S
}
fn menu_action_open_recent_project(_: **Menu.State(*Self), _: *Button.State(*Menu.State(*Self))) void {
tp.self_pid().send(.{ "log", "home", "open recent project not implemented" }) catch {};
command.executeName("open_recent_project", .{}) catch {};
}
fn menu_action_show_commands(_: **Menu.State(*Self), _: *Button.State(*Menu.State(*Self))) void {

View file

@ -87,7 +87,7 @@ pub fn create(a: std.mem.Allocator) !Widget {
pub fn deinit(self: *Self, a: std.mem.Allocator) void {
self.close_all_panel_views();
for (self.file_stack.items) |file_path| self.a.free(file_path);
self.clear_file_stack();
self.file_stack.deinit();
self.commands.deinit();
self.widgets.deinit(a);
@ -224,7 +224,7 @@ const cmds = struct {
}
pub fn open_project_cwd(self: *Self, _: Ctx) Result {
try project_manager.open_cwd();
try project_manager.open(".");
_ = try self.statusbar.msg(.{ "PRJ", "open" });
}
@ -236,6 +236,23 @@ const cmds = struct {
_ = try self.statusbar.msg(.{ "PRJ", "open" });
}
pub fn change_project(self: *Self, ctx: Ctx) Result {
var project_dir: []const u8 = undefined;
if (!try ctx.args.match(.{tp.extract(&project_dir)}))
return;
if (self.editor) |editor| {
if (editor.is_dirty())
return tp.exit("unsaved changes");
self.clear_file_stack();
try editor.close_file(.{});
} else {
self.clear_file_stack();
}
try project_manager.open(project_dir);
_ = try self.statusbar.msg(.{ "PRJ", "open" });
log.logger("project").print("switched to project {s}", .{project_dir});
}
pub fn navigate(self: *Self, ctx: Ctx) Result {
tui.reset_drag_context();
const frame = tracy.initZone(@src(), .{ .name = "navigate" });
@ -653,6 +670,11 @@ fn pop_file_stack(self: *Self, closed: ?[]const u8) ?[]const u8 {
return self.file_stack.popOrNull();
}
fn clear_file_stack(self: *Self) void {
for (self.file_stack.items) |file_path| self.a.free(file_path);
self.file_stack.clearRetainingCapacity();
}
fn add_find_in_files_result(
self: *Self,
file_list_type: FileListType,

View file

@ -81,6 +81,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
return switch (modifiers) {
mod.CTRL => switch (keynormal) {
'E' => self.cmd("open_recent", .{}),
'R' => self.cmd("open_recent_project", .{}),
'J' => self.cmd("toggle_panel", .{}),
'Z' => self.cmd("undo", .{}),
'Y' => self.cmd("redo", .{}),
@ -369,6 +370,7 @@ const hints = tui.KeybindHints.initComptime(.{
.{ "move_word_right", "C-right, A-f" },
.{ "open_command_palette", "C-S-p, S-A-p" },
.{ "open_recent", "C-e" },
.{ "open_recent_project", "C-r" },
.{ "paste", "A-v" },
.{ "pop_cursor", "C-u" },
.{ "pull_down", "A-down" },

View file

@ -63,6 +63,7 @@ fn mapPress(self: *Self, keypress: u32, modifiers: u32) tp.result {
'W' => self.cmd("quit", .{}),
'O' => self.cmd("open_file", .{}),
'E' => self.cmd("open_recent", .{}),
'R' => self.cmd("open_recent_project", .{}),
'P' => self.cmd("open_command_palette", .{}),
'/' => self.cmd("open_help", .{}),
'K' => self.leader = .{ .keypress = keynormal, .modifiers = modifiers },
@ -94,7 +95,7 @@ fn mapPress(self: *Self, keypress: u32, modifiers: u32) tp.result {
'h' => self.cmd("open_help", .{}),
'o' => self.cmd("open_file", .{}),
'e' => self.cmd("open_recent", .{}),
'r' => self.msg("open recent project not implemented"),
'r' => self.cmd("open_recent_project", .{}),
'p' => self.cmd("open_command_palette", .{}),
'c' => self.cmd("open_config", .{}),
't' => self.cmd("change_theme", .{}),
@ -157,6 +158,7 @@ const hints = tui.KeybindHints.initComptime(.{
.{ "find_in_files", "C-S-f" },
.{ "open_file", "o, C-o" },
.{ "open_recent", "e, C-e" },
.{ "open_recent_project", "r, C-r" },
.{ "open_command_palette", "p, C-S-p, S-A-p" },
.{ "home_menu_activate", "enter" },
.{ "home_menu_down", "down" },

View file

@ -0,0 +1,55 @@
const std = @import("std");
const cbor = @import("cbor");
const tp = @import("thespian");
const project_manager = @import("project_manager");
const Widget = @import("../../Widget.zig");
const tui = @import("../../tui.zig");
pub const Type = @import("palette.zig").Create(@This());
pub const label = "Search projects";
pub const Entry = struct {
name: []const u8,
};
pub const Match = struct {
name: []const u8,
score: i32,
matches: []const usize,
};
pub fn load_entries(palette: *Type) !void {
const rsp = try project_manager.request_recent_projects(palette.a);
var iter: []const u8 = rsp.buf;
var len = try cbor.decodeArrayHeader(&iter);
while (len > 0) : (len -= 1) {
var name: []const u8 = undefined;
if (try cbor.matchValue(&iter, cbor.extract(&name))) {
(palette.entries.addOne() catch @panic("oom")).* = .{
.name = name,
};
} else return error.InvalidMessageField;
}
}
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {
var value = std.ArrayList(u8).init(palette.a);
defer value.deinit();
const writer = value.writer();
try cbor.writeValue(writer, entry.name);
try cbor.writeValue(writer, if (palette.hints) |hints| hints.get(entry.name) orelse "" else "");
if (matches) |matches_|
try cbor.writeValue(writer, matches_);
try palette.menu.add_item_with_handler(value.items, select);
palette.items += 1;
}
fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
var name: []const u8 = undefined;
var iter = button.opts.label;
if (!(cbor.matchString(&iter, &name) catch false)) return;
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err("open_recent_project", e);
tp.self_pid().send(.{ "cmd", "change_project", .{name} }) catch |e| menu.*.opts.ctx.logger.err("open_recent_project", e);
}

View file

@ -641,6 +641,10 @@ const cmds = struct {
return self.enter_overlay_mode(@import("mode/overlay/open_recent.zig"));
}
pub fn open_recent_project(self: *Self, _: Ctx) Result {
return self.enter_overlay_mode(@import("mode/overlay/open_recent_project.zig").Type);
}
pub fn change_theme(self: *Self, _: Ctx) Result {
return self.enter_overlay_mode(@import("mode/overlay/theme_palette.zig").Type);
}