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" return command.perform "core:open-file"
end end
local files = {} local files = {}
for dir, item in core.get_project_files() do for dirpath, dirname, item in core.get_project_files() do
local dirname = common.basename(dir)
if item.type == "file" then if item.type == "file" then
table.insert(files, dirname .. PATHSEP .. item.filename) table.insert(files, dirname .. PATHSEP .. item.filename)
end end
end end
core.command_view:enter("Open File From Project", function(text, item) core.command_view:enter("Open File From Project", function(text, item)
local filename = common.home_expand(item and item.text or text) local text = item and item.text or text
local dirname, basename = filename:match("(.-)[/\\](.+)") local filename = core.resolve_project_filename(text) or common.home_expand(text)
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)) core.root_view:open_doc(core.open_doc(filename))
end, function(text) end, function(text)
return common.fuzzy_match_with_recents(files, core.visited_files, 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:change-project-folder"] = function()
core.command_view:enter("Change Project Folder", function(text, item) core.command_view:enter("Change Project Folder", function(text, item)
text = system.absolute_path(common.home_expand(item and item.text or text)) 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) local path_stat = system.get_file_info(text)
if not path_stat or path_stat.type ~= 'dir' then if not path_stat or path_stat.type ~= 'dir' then
core.error("Cannot open folder %q", text) 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] dir = core.project_entries[state.dir_index]
end end
if not dir then return 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 end
@ -262,6 +262,28 @@ function core.project_files_number()
end 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 -- create a directory using mkdir but may need to create the parent
-- directories as well. -- directories as well.
local function create_user_directory() local function create_user_directory()
@ -764,14 +786,7 @@ end
function core.set_visited(filename) function core.set_visited(filename)
for i = 1, #core.project_entries do filename = core.as_project_filename(filename) or common.home_encode(filename)
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 for i = 1, #core.visited_files do
if core.visited_files[i] == filename then if core.visited_files[i] == filename then
table.remove(core.visited_files, i) table.remove(core.visited_files, i)

View File

@ -23,8 +23,8 @@ function ResultsView:get_name()
end end
local function find_all_matches_in_file(t, filename, fn) local function find_all_matches_in_file(t, dirpath, dirname, filename, fn)
local fp = io.open(filename) local fp = io.open(dirpath .. PATHSEP .. filename)
if not fp then return t end if not fp then return t end
local n = 1 local n = 1
for line in fp:lines() do 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 -- 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. -- 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) 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 core.redraw = true
end end
if n % 100 == 0 then coroutine.yield() end if n % 100 == 0 then coroutine.yield() end
@ -54,10 +54,9 @@ function ResultsView:begin_search(text, fn)
core.add_thread(function() core.add_thread(function()
local i = 1 local i = 1
for dir_name, file in core.get_project_files() do for dirpath, dirname, item in core.get_project_files() do
if file.type == "file" then if item.type == "file" then
local path = (dir_name == core.project_dir and "" or (dir_name .. PATHSEP)) find_all_matches_in_file(self.results, dirpath, dirname, item.filename, fn)
find_all_matches_in_file(self.results, path .. file.filename, fn)
end end
self.last_file_idx = i self.last_file_idx = i
i = i + 1 i = i + 1
@ -102,7 +101,8 @@ function ResultsView:open_selected_result()
return return
end end
core.try(function() 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() core.root_view.root_node:update_layout()
dv.doc:set_selection(res.line, res.col) dv.doc:set_selection(res.line, res.col)
dv:scroll_to_line(res.line, false, true) dv:scroll_to_line(res.line, false, true)