Improve code project filename resolution

This commit is contained in:
Francesco Abbate 2021-05-18 09:15:35 +02:00
parent 0cc87591e6
commit fd47d646c6
3 changed files with 36 additions and 29 deletions

View File

@ -67,22 +67,14 @@ command.add(nil, {
["core:find-file"] = function()
local files = {}
for dir, item in core.get_project_files() do
local dirname = common.basename(dir)
for dirpath, dirname, item in core.get_project_files() do
if item.type == "file" then
table.insert(files, dirname .. PATHSEP .. item.filename)
end
end
core.command_view:enter("Open File From Project", function(text, item)
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
local text = item and item.text or text
local filename = core.resolve_project_filename(text) or common.home_expand(text)
core.root_view:open_doc(core.open_doc(filename))
end, function(text)
return common.fuzzy_match_with_recents(files, core.visited_files, text)
@ -154,7 +146,7 @@ command.add(nil, {
["core:change-project-folder"] = function()
core.command_view:enter("Change Project Folder", function(text, item)
text = system.absolute_path(common.home_expand(item and item.text or text))
if text == core.project_dir then return end
if text == core.working_dir then return end
local path_stat = system.get_file_info(text)
if not path_stat or path_stat.type ~= 'dir' then
core.error("Cannot open folder %q", text)

View File

@ -190,7 +190,7 @@ local function project_files_iter(state)
dir = core.project_entries[state.dir_index]
end
if not dir then return end
return dir.name, dir.files[state.file_index]
return dir.name, dir.item.filename, dir.files[state.file_index]
end
@ -209,6 +209,28 @@ function core.project_files_number()
end
function core.resolve_project_filename(filename)
local dirname, basename = filename:match("(.-)[/\\](.+)")
for i = 1, #core.project_entries do
local dir = core.project_entries[i]
if dir.item.filename == dirname then
return dir.name .. PATHSEP .. basename
end
end
end
function core.as_project_filename(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)
return filename:sub(#dirpath + 2)
end
end
end
-- create a directory using mkdir but may need to create the parent
-- directories as well.
local function create_user_directory()
@ -687,14 +709,7 @@ 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)
filename = core.as_project_filename(filename) or common.home_encode(filename)
for i = 1, #core.visited_files do
if core.visited_files[i] == filename then
table.remove(core.visited_files, i)

View File

@ -23,14 +23,14 @@ function ResultsView:get_name()
end
local function find_all_matches_in_file(t, filename, fn)
local fp = io.open(filename)
local function find_all_matches_in_file(t, dirpath, dirname, filename, fn)
local fp = io.open(dirpath .. PATHSEP .. filename)
if not fp then return t end
local n = 1
for line in fp:lines() do
local s = fn(line)
if s then
table.insert(t, { file = filename, text = line, line = n, col = s })
table.insert(t, { file = dirname .. PATHSEP .. filename, text = line, line = n, col = s })
core.redraw = true
end
if n % 100 == 0 then coroutine.yield() end
@ -51,10 +51,9 @@ function ResultsView:begin_search(text, fn)
core.add_thread(function()
local i = 1
for dir_name, file in core.get_project_files() do
if file.type == "file" then
local path = (dir_name == core.project_dir and "" or (dir_name .. PATHSEP))
find_all_matches_in_file(self.results, path .. file.filename, fn)
for dirpath, dirname, item in core.get_project_files() do
if item.type == "file" then
find_all_matches_in_file(self.results, dirpath, dirname, item.filename, fn)
end
self.last_file_idx = i
i = i + 1
@ -99,7 +98,8 @@ function ResultsView:open_selected_result()
return
end
core.try(function()
local dv = core.root_view:open_doc(core.open_doc(res.file))
local filename = core.resolve_project_filename(res.file)
local dv = core.root_view:open_doc(core.open_doc(filename))
core.root_view.root_node:update_layout()
dv.doc:set_selection(res.line, res.col)
dv:scroll_to_line(res.line, false, true)