From 295e6b7e5ac19348fa4052beec376bf2573122f1 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Wed, 1 Jun 2022 03:51:29 +0200 Subject: [PATCH] Allow `common.path_suggest` to specify a root directory This will make relative paths start from `root`. --- data/core/common.lua | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/data/core/common.lua b/data/core/common.lua index 7ed87456..c123bb4b 100644 --- a/data/core/common.lua +++ b/data/core/common.lua @@ -140,9 +140,25 @@ function common.fuzzy_match_with_recents(haystack, recents, needle) end -function common.path_suggest(text) +function common.path_suggest(text, root) + if root and root:sub(-1) ~= PATHSEP then + root = root .. PATHSEP + end local path, name = text:match("^(.-)([^/\\]*)$") - local files = system.list_dir(path == "" and "." or path) or {} + -- ignore root if path is absolute + local is_absolute = common.is_absolute_path(text) + if not is_absolute then + if path == "" then + path = root or "." + else + path = (root or "") .. path + end + end + + local files = system.list_dir(path) or {} + if path:sub(-1) ~= PATHSEP then + path = path .. PATHSEP + end local res = {} for _, file in ipairs(files) do file = path .. file @@ -151,6 +167,13 @@ function common.path_suggest(text) if info.type == "dir" then file = file .. PATHSEP end + if root then + -- remove root part from file path + local s, e = file:find(root, nil, true) + if s == 1 then + file = file:sub(e + 1) + end + end if file:lower():find(text:lower(), nil, true) == 1 then table.insert(res, file) end