From ddd56ec615a976803005947bf663da85c56a508d Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Mon, 28 Dec 2020 15:51:16 +0100 Subject: [PATCH] Add command to remove a directory from the project In addition directories can be removed with a mouse middle-click in the tree view pane. --- data/core/commands/core.lua | 17 ++++++++++++++++- data/core/common.lua | 12 ++++++++++++ data/core/init.lua | 13 +++++++++++++ data/plugins/treeview.lua | 9 +++++++-- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/data/core/commands/core.lua b/data/core/commands/core.lua index e36f0489..43a04ba4 100644 --- a/data/core/commands/core.lua +++ b/data/core/commands/core.lua @@ -166,7 +166,7 @@ command.add(nil, { end, ["core:add-directory"] = function() - core.command_view:enter("Open Project", function(text) + core.command_view:enter("Add Directory", function(text) text = common.home_expand(text) local path_stat, err = system.get_file_info(text) if not path_stat then @@ -182,4 +182,19 @@ command.add(nil, { end, suggest_directory) end, + ["core:remove-directory"] = function() + local dir_list = {} + local n = #core.project_directories + for i = n, 1, -1 do + dir_list[n - i + 1] = core.project_directories[i].name + end + core.command_view:enter("Remove Directory", function(text) + if not core.remove_project_directory(text) then + core.error("The project has no directory %q", text) + end + end, function(text) + text = common.home_expand(text) + return home_encode_list(common.dir_list_suggest(text, dir_list)) + end) + end, }) diff --git a/data/core/common.lua b/data/core/common.lua index 61ab355d..64ac2b6a 100644 --- a/data/core/common.lua +++ b/data/core/common.lua @@ -117,6 +117,18 @@ function common.dir_path_suggest(text) end +function common.dir_list_suggest(text, dir_list) + local path, name = text:match("^(.-)([^/\\]*)$") + local res = {} + for _, dir_path in ipairs(dir_list) do + if dir_path:lower():find(text:lower(), nil, true) == 1 then + table.insert(res, dir_path) + end + end + return res +end + + function common.match_pattern(text, pattern, ...) if type(pattern) == "string" then return text:find(pattern, ...) diff --git a/data/core/init.lua b/data/core/init.lua index a4c18dd9..eaa03000 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -274,6 +274,7 @@ function core.load_user_directory() end) end + function core.add_project_directory(path) -- top directories has a file-like "item" but the item.filename -- will be simply the name of the directory, without its path. @@ -285,6 +286,18 @@ function core.add_project_directory(path) }) end + +function core.remove_project_directory(path) + for i, dir in ipairs(core.project_directories) do + if dir.name == path then + table.remove(core.project_directories, i) + return true + end + end + return false +end + + function core.init() command = require "core.command" keymap = require "core.keymap" diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index deb1faa2..bd95cc79 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -43,11 +43,12 @@ function TreeView:get_cached(item, dirname) t.filename = basename t.expanded = true t.depth = 0 + t.abs_filename = dirname else t.filename = item.filename t.depth = get_depth(item.filename) + t.abs_filename = dirname .. PATHSEP .. item.filename end - t.abs_filename = dirname .. PATHSEP .. item.filename t.name = basename t.type = item.type dir_cache[item.filename] = t @@ -140,7 +141,11 @@ function TreeView:on_mouse_pressed(button, x, y) if not self.hovered_item then return elseif self.hovered_item.type == "dir" then - self.hovered_item.expanded = not self.hovered_item.expanded + if button == "middle" and self.hovered_item.depth == 0 then + core.remove_project_directory(self.hovered_item.abs_filename) + else + self.hovered_item.expanded = not self.hovered_item.expanded + end else core.try(function() core.root_view:open_doc(core.open_doc(self.hovered_item.abs_filename))