Improve code project filename resolution
This commit is contained in:
parent
0cc87591e6
commit
fd47d646c6
|
@ -67,22 +67,14 @@ command.add(nil, {
|
||||||
|
|
||||||
["core:find-file"] = function()
|
["core:find-file"] = function()
|
||||||
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)
|
||||||
|
@ -154,7 +146,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)
|
||||||
|
|
|
@ -190,7 +190,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
|
||||||
|
|
||||||
|
|
||||||
|
@ -209,6 +209,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()
|
||||||
|
@ -687,14 +709,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)
|
||||||
|
|
|
@ -23,14 +23,14 @@ 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
|
||||||
local s = fn(line)
|
local s = fn(line)
|
||||||
if s then
|
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
|
core.redraw = true
|
||||||
end
|
end
|
||||||
if n % 100 == 0 then coroutine.yield() end
|
if n % 100 == 0 then coroutine.yield() end
|
||||||
|
@ -51,10 +51,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
|
||||||
|
@ -99,7 +98,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)
|
||||||
|
|
Loading…
Reference in New Issue