Accept relative filenames in command find-file

Use name within project entries to resolve filenames in
command find-file.
This commit is contained in:
Francesco Abbate 2021-05-17 23:10:15 +02:00
parent 6dd5743be8
commit 4621ee2e7f
3 changed files with 21 additions and 4 deletions

View File

@ -71,13 +71,22 @@ command.add(nil, {
end
local files = {}
for dir, item in core.get_project_files() do
local dirname = common.basename(dir)
if item.type == "file" then
table.insert(files, common.home_encode(dir .. PATHSEP .. item.filename))
table.insert(files, dirname .. PATHSEP .. item.filename)
end
end
core.command_view:enter("Open File From Project", function(text, item)
text = item and item.text or text
core.root_view:open_doc(core.open_doc(common.home_expand(text)))
local filename = common.home_expand(item and item.text or text)
local dirname, basename = filename:match("(.-)[/\\](.+)")
for i = 1, #core.project_entries do
local dir = core.project_entries[i]
if dir.item.filename == dirname then
filename = dir.name .. PATHSEP .. basename
break
end
end
core.root_view:open_doc(core.open_doc(filename))
end, function(text)
return common.fuzzy_match_with_recents(files, core.visited_files, text)
end)

View File

@ -764,6 +764,13 @@ end
function core.set_visited(filename)
for i = 1, #core.project_entries do
local dir = core.project_entries[i]
if common.path_belongs_to(filename, dir.name) then
local dirpath = common.dirname(dir.name)
filename = filename:sub(#dirpath + 2)
end
end
filename = common.home_encode(filename)
for i = 1, #core.visited_files do
if core.visited_files[i] == filename then

View File

@ -143,11 +143,12 @@ local function load_workspace()
if active_view then
core.set_active_view(active_view)
end
core.project_entries = {}
for _, entry in ipairs(workspace.project_entries) do
if entry.type == "dir" then
core.add_project_directory(entry.path)
elseif entry.type == "dir" then
core.add_project_dfile(entry.path)
core.add_project_file(entry.path)
end
end
system.chdir(workspace.working_dir)