feat(open_file): complete files per directory with simple prefix matching
This commit is contained in:
		
							parent
							
								
									16b28c0254
								
							
						
					
					
						commit
						8beddc5ea2
					
				
					 1 changed files with 108 additions and 25 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
const std = @import("std");
 | 
			
		||||
const tp = @import("thespian");
 | 
			
		||||
const log = @import("log");
 | 
			
		||||
const root = @import("root");
 | 
			
		||||
 | 
			
		||||
const key = @import("renderer").input.key;
 | 
			
		||||
const mod = @import("renderer").input.modifier;
 | 
			
		||||
| 
						 | 
				
			
			@ -15,12 +16,20 @@ const EventHandler = @import("../../EventHandler.zig");
 | 
			
		|||
const MessageFilter = @import("../../MessageFilter.zig");
 | 
			
		||||
 | 
			
		||||
const Self = @This();
 | 
			
		||||
const max_complete_paths = 1024;
 | 
			
		||||
 | 
			
		||||
a: std.mem.Allocator,
 | 
			
		||||
file_path: std.ArrayList(u8),
 | 
			
		||||
query: std.ArrayList(u8),
 | 
			
		||||
query_pending: bool = false,
 | 
			
		||||
match: std.ArrayList(u8),
 | 
			
		||||
entries: std.ArrayList(Entry),
 | 
			
		||||
complete_trigger_count: usize = 0,
 | 
			
		||||
matched_entry: usize = 0,
 | 
			
		||||
 | 
			
		||||
const Entry = struct {
 | 
			
		||||
    name: []const u8,
 | 
			
		||||
    type: enum { dir, file, link },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub fn create(a: std.mem.Allocator, _: command.Context) !*Self {
 | 
			
		||||
    const self: *Self = try a.create(Self);
 | 
			
		||||
| 
						 | 
				
			
			@ -28,8 +37,10 @@ pub fn create(a: std.mem.Allocator, _: command.Context) !*Self {
 | 
			
		|||
        .a = a,
 | 
			
		||||
        .file_path = std.ArrayList(u8).init(a),
 | 
			
		||||
        .query = std.ArrayList(u8).init(a),
 | 
			
		||||
        .match = std.ArrayList(u8).init(a),
 | 
			
		||||
        .entries = std.ArrayList(Entry).init(a),
 | 
			
		||||
    };
 | 
			
		||||
    try tui.current().message_filters.add(MessageFilter.bind(self, receive_project_manager));
 | 
			
		||||
    try tui.current().message_filters.add(MessageFilter.bind(self, receive_path_entry));
 | 
			
		||||
    if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| {
 | 
			
		||||
        if (editor.is_dirty()) return tp.exit("unsaved changes");
 | 
			
		||||
        if (editor.file_path) |old_path|
 | 
			
		||||
| 
						 | 
				
			
			@ -48,6 +59,9 @@ pub fn create(a: std.mem.Allocator, _: command.Context) !*Self {
 | 
			
		|||
 | 
			
		||||
pub fn deinit(self: *Self) void {
 | 
			
		||||
    tui.current().message_filters.remove_ptr(self);
 | 
			
		||||
    self.clear_entries();
 | 
			
		||||
    self.entries.deinit();
 | 
			
		||||
    self.match.deinit();
 | 
			
		||||
    self.query.deinit();
 | 
			
		||||
    self.file_path.deinit();
 | 
			
		||||
    self.a.destroy(self);
 | 
			
		||||
| 
						 | 
				
			
			@ -119,6 +133,8 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void {
 | 
			
		|||
            else {},
 | 
			
		||||
        },
 | 
			
		||||
        0 => switch (keypress) {
 | 
			
		||||
            key.UP => self.reverse_complete_file(),
 | 
			
		||||
            key.DOWN => self.try_complete_file(),
 | 
			
		||||
            key.TAB => self.try_complete_file(),
 | 
			
		||||
            key.ESC => self.cancel(),
 | 
			
		||||
            key.ENTER => self.navigate(),
 | 
			
		||||
| 
						 | 
				
			
			@ -158,20 +174,35 @@ fn cancel(_: *Self) void {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
fn navigate(self: *Self) void {
 | 
			
		||||
    if (root.is_directory(self.file_path.items) catch false) return;
 | 
			
		||||
    if (self.file_path.items.len > 0)
 | 
			
		||||
        tp.self_pid().send(.{ "cmd", "navigate", .{ .file = self.file_path.items } }) catch {};
 | 
			
		||||
    command.executeName("exit_mini_mode", .{}) catch {};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn clear_entries(self: *Self) void {
 | 
			
		||||
    for (self.entries.items) |entry| self.a.free(entry.name);
 | 
			
		||||
    self.entries.clearRetainingCapacity();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn try_complete_file(self: *Self) !void {
 | 
			
		||||
    self.complete_trigger_count += 1;
 | 
			
		||||
    if (self.complete_trigger_count == 1) {
 | 
			
		||||
        self.query.clearRetainingCapacity();
 | 
			
		||||
        try self.query.appendSlice(self.file_path.items);
 | 
			
		||||
        self.match.clearRetainingCapacity();
 | 
			
		||||
        self.clear_entries();
 | 
			
		||||
        if (try root.is_directory(self.file_path.items)) {
 | 
			
		||||
            try self.query.appendSlice(self.file_path.items);
 | 
			
		||||
        } else if (self.file_path.items.len > 0) {
 | 
			
		||||
            const basename_begin = std.mem.lastIndexOfScalar(u8, self.file_path.items, std.fs.path.sep) orelse 0;
 | 
			
		||||
            try self.query.appendSlice(self.file_path.items[0 .. basename_begin + 1]);
 | 
			
		||||
            try self.match.appendSlice(self.file_path.items[basename_begin + 1 ..]);
 | 
			
		||||
        }
 | 
			
		||||
        // log.logger("open_file").print("query: '{s}' match: '{s}'", .{ self.query.items, self.match.items });
 | 
			
		||||
        try project_manager.request_path_files(max_complete_paths, self.query.items);
 | 
			
		||||
    } else {
 | 
			
		||||
        try self.do_complete();
 | 
			
		||||
    }
 | 
			
		||||
    if (self.query_pending) return;
 | 
			
		||||
    self.query_pending = true;
 | 
			
		||||
    try project_manager.query_recent_files(self.complete_trigger_count, self.query.items);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn reverse_complete_file(self: *Self) !void {
 | 
			
		||||
| 
						 | 
				
			
			@ -186,12 +217,10 @@ fn reverse_complete_file(self: *Self) !void {
 | 
			
		|||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    self.complete_trigger_count -= 1;
 | 
			
		||||
    if (self.query_pending) return;
 | 
			
		||||
    self.query_pending = true;
 | 
			
		||||
    try project_manager.query_recent_files(self.complete_trigger_count, self.query.items);
 | 
			
		||||
    try self.do_complete();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn receive_project_manager(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
 | 
			
		||||
fn receive_path_entry(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
 | 
			
		||||
    if (try m.match(.{ "PRJ", tp.more })) {
 | 
			
		||||
        self.process_project_manager(m) catch |e| return tp.exit_error(e, @errorReturnTrace());
 | 
			
		||||
        return true;
 | 
			
		||||
| 
						 | 
				
			
			@ -206,22 +235,76 @@ fn process_project_manager(self: *Self, m: tp.message) !void {
 | 
			
		|||
            mini_mode.cursor = self.file_path.items.len;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    var file_name: []const u8 = undefined;
 | 
			
		||||
    var query: []const u8 = undefined;
 | 
			
		||||
    if (try m.match(.{ "PRJ", "recent", tp.any, tp.extract(&file_name), tp.any })) {
 | 
			
		||||
        self.file_path.clearRetainingCapacity();
 | 
			
		||||
        try self.file_path.appendSlice(file_name);
 | 
			
		||||
        tui.need_render();
 | 
			
		||||
    } else if (try m.match(.{ "PRJ", "recent", tp.any, tp.extract(&file_name) })) {
 | 
			
		||||
        self.file_path.clearRetainingCapacity();
 | 
			
		||||
        try self.file_path.appendSlice(file_name);
 | 
			
		||||
        tui.need_render();
 | 
			
		||||
    } else if (try m.match(.{ "PRJ", "recent_done", tp.any, tp.extract(&query) })) {
 | 
			
		||||
        self.query_pending = false;
 | 
			
		||||
        if (!std.mem.eql(u8, self.query.items, query))
 | 
			
		||||
            try self.try_complete_file();
 | 
			
		||||
    var count: usize = undefined;
 | 
			
		||||
    if (try m.match(.{ "PRJ", "path_entry", tp.more })) {
 | 
			
		||||
        return self.process_path_entry(m);
 | 
			
		||||
    } else if (try m.match(.{ "PRJ", "path_done", tp.any, tp.any, tp.extract(&count) })) {
 | 
			
		||||
        try self.do_complete();
 | 
			
		||||
    } else {
 | 
			
		||||
        log.logger("open_recent").err("receive", tp.unexpected(m));
 | 
			
		||||
        log.logger("open_file").err("receive", tp.unexpected(m));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn process_path_entry(self: *Self, m: tp.message) !void {
 | 
			
		||||
    var path: []const u8 = undefined;
 | 
			
		||||
    var file_name: []const u8 = undefined;
 | 
			
		||||
    if (try m.match(.{ tp.any, tp.any, tp.any, tp.extract(&path), "DIR", tp.extract(&file_name) })) {
 | 
			
		||||
        (try self.entries.addOne()).* = .{ .name = try self.a.dupe(u8, file_name), .type = .dir };
 | 
			
		||||
    } else if (try m.match(.{ tp.any, tp.any, tp.any, tp.extract(&path), "LINK", tp.extract(&file_name) })) {
 | 
			
		||||
        (try self.entries.addOne()).* = .{ .name = try self.a.dupe(u8, file_name), .type = .link };
 | 
			
		||||
    } else if (try m.match(.{ tp.any, tp.any, tp.any, tp.extract(&path), "FILE", tp.extract(&file_name) })) {
 | 
			
		||||
        (try self.entries.addOne()).* = .{ .name = try self.a.dupe(u8, file_name), .type = .file };
 | 
			
		||||
    } else {
 | 
			
		||||
        log.logger("open_file").err("receive", tp.unexpected(m));
 | 
			
		||||
    }
 | 
			
		||||
    tui.need_render();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn do_complete(self: *Self) !void {
 | 
			
		||||
    self.complete_trigger_count = @min(self.complete_trigger_count, self.entries.items.len);
 | 
			
		||||
    self.file_path.clearRetainingCapacity();
 | 
			
		||||
    if (self.match.items.len > 0) {
 | 
			
		||||
        try self.match_path();
 | 
			
		||||
    } else {
 | 
			
		||||
        try self.construct_path(self.query.items, self.entries.items[self.complete_trigger_count - 1], self.complete_trigger_count - 1);
 | 
			
		||||
    }
 | 
			
		||||
    log.logger("open_file").print("{d}/{d}", .{ self.matched_entry + 1, self.entries.items.len });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn construct_path(self: *Self, path_: []const u8, entry: Entry, entry_no: usize) !void {
 | 
			
		||||
    self.matched_entry = entry_no;
 | 
			
		||||
    const path = project_manager.normalize_file_path(path_);
 | 
			
		||||
    try self.file_path.appendSlice(path);
 | 
			
		||||
    if (path.len > 0 and path[path.len - 1] != std.fs.path.sep)
 | 
			
		||||
        try self.file_path.append(std.fs.path.sep);
 | 
			
		||||
    try self.file_path.appendSlice(entry.name);
 | 
			
		||||
    if (entry.type == .dir)
 | 
			
		||||
        try self.file_path.append(std.fs.path.sep);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn match_path(self: *Self) !void {
 | 
			
		||||
    var matched: usize = 0;
 | 
			
		||||
    var last: ?Entry = null;
 | 
			
		||||
    var last_no: usize = 0;
 | 
			
		||||
    for (self.entries.items, 0..) |entry, i| {
 | 
			
		||||
        if (entry.name.len >= self.match.items.len and
 | 
			
		||||
            std.mem.eql(u8, self.match.items, entry.name[0..self.match.items.len]))
 | 
			
		||||
        {
 | 
			
		||||
            matched += 1;
 | 
			
		||||
            if (matched == self.complete_trigger_count) {
 | 
			
		||||
                try self.construct_path(self.query.items, entry, i);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            last = entry;
 | 
			
		||||
            last_no = i;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (last) |entry| {
 | 
			
		||||
        try self.construct_path(self.query.items, entry, last_no);
 | 
			
		||||
        self.complete_trigger_count = matched;
 | 
			
		||||
    } else {
 | 
			
		||||
        log.logger("open_file").print("no match for '{s}'", .{self.match.items});
 | 
			
		||||
        try self.construct_path(self.query.items, .{ .name = self.match.items, .type = .file }, 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue