Allow `common.path_suggest` to specify a root directory
This will make relative paths start from `root`.
This commit is contained in:
parent
9a428648a9
commit
295e6b7e5a
|
@ -140,9 +140,25 @@ function common.fuzzy_match_with_recents(haystack, recents, needle)
|
||||||
end
|
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 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 = {}
|
local res = {}
|
||||||
for _, file in ipairs(files) do
|
for _, file in ipairs(files) do
|
||||||
file = path .. file
|
file = path .. file
|
||||||
|
@ -151,6 +167,13 @@ function common.path_suggest(text)
|
||||||
if info.type == "dir" then
|
if info.type == "dir" then
|
||||||
file = file .. PATHSEP
|
file = file .. PATHSEP
|
||||||
end
|
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
|
if file:lower():find(text:lower(), nil, true) == 1 then
|
||||||
table.insert(res, file)
|
table.insert(res, file)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue