From 66275fe207430336747fbf6e90cf5de7baa86c3f Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Thu, 17 Jun 2021 18:23:30 +0200 Subject: [PATCH] Fix error in dirname computation in TreeView In TreeView:on_mouse_pressed() we need to find the directory a relative filename belongs to from its absolute filename. The code was using string.find to locate the relative filename within the absolute path but in some very specific cases we can find a pattern which is not the right-most one leading to a wrong directory name. Fix the error by adding a loop to make sure we find the right-most match. The standard Lua library has not a string.rfind to make a reverse search. Add a check to avoid trying to updating a topdir project directory. --- data/core/init.lua | 10 ++++++++++ data/plugins/treeview.lua | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index 95eb7973..992eb5de 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -194,6 +194,16 @@ local function project_scan_thread() end +function core.is_project_folder(dirname) + for _, dir in ipairs(core.project_directories) do + if dir.name == dirname then + return true + end + end + return false +end + + function core.scan_project_folder(dirname, filename) for _, dir in ipairs(core.project_directories) do if dir.name == dirname then diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 8615343a..8214bda4 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -222,10 +222,19 @@ function TreeView:on_mouse_pressed(button, x, y, clicks) else if core.project_files_limit and not hovered_item.expanded then local filename, abs_filename = hovered_item.filename, hovered_item.abs_filename - local index = string.find(abs_filename, filename, 1, true) + local index = 0 + -- The loop below is used to find the first match starting from the end + -- in case there are multiple matches. + while index and index + #filename < #abs_filename do + index = string.find(abs_filename, filename, index + 1, true) + end + -- we assume here index is not nil because the abs_filename must contain the + -- relative filename local dirname = string.sub(abs_filename, 1, index - 2) - core.scan_project_folder(dirname, filename) - self:invalidate_cache(dirname) + if core.is_project_folder(dirname) then + core.scan_project_folder(dirname, filename) + self:invalidate_cache(dirname) + end end hovered_item.expanded = not hovered_item.expanded end