Improve code project filename resolution

This commit is contained in:
Francesco Abbate 2021-05-18 09:15:35 +02:00
parent 4621ee2e7f
commit 89bb106d32
3 changed files with 41 additions and 34 deletions

View File

@ -70,22 +70,14 @@ command.add(nil, {
return command.perform "core:open-file"
end
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)
@ -157,7 +149,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

@ -233,7 +233,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
@ -262,6 +262,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()
@ -764,14 +786,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,8 +23,8 @@ 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
@ -33,7 +33,7 @@ local function find_all_matches_in_file(t, filename, fn)
-- Insert maximum 256 characters. If we insert more, for compiled files, which can have very long lines
-- things tend to get sluggish. If our line is longer than 80 characters, begin to truncate the thing.
local start_index = math.max(s - 80, 1)
table.insert(t, { file = filename, text = (start_index > 1 and "..." or "") .. line:sub(start_index, 256 + start_index), line = n, col = s })
table.insert(t, { file = dirname .. PATHSEP .. filename, text = (start_index > 1 and "..." or "") .. line:sub(start_index, 256 + start_index), line = n, col = s })
core.redraw = true
end
if n % 100 == 0 then coroutine.yield() end
@ -54,10 +54,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
@ -102,7 +101,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)
@ -241,7 +241,7 @@ command.add(nil, {
core.command_view:enter("Find Regex In Project", function(text)
local re = regex.compile(text, "i")
begin_search(text, function(line_text)
return regex.cmatch(re, line_text)
return regex.cmatch(re, line_text)
end)
end)
end,
@ -276,22 +276,22 @@ command.add(ResultsView, {
["project-search:refresh"] = function()
core.active_view:refresh()
end,
["project-search:move-to-previous-page"] = function()
local view = core.active_view
view.scroll.to.y = view.scroll.to.y - view.size.y
end,
["project-search:move-to-next-page"] = function()
local view = core.active_view
view.scroll.to.y = view.scroll.to.y + view.size.y
end,
["project-search:move-to-start-of-doc"] = function()
local view = core.active_view
view.scroll.to.y = 0
end,
["project-search:move-to-end-of-doc"] = function()
local view = core.active_view
view.scroll.to.y = view:get_scrollable_size()