Ensure filename are store relative to project directory

This commit is contained in:
Francesco Abbate 2021-03-06 23:36:05 +01:00
parent 35abce1ce1
commit 991db14877
3 changed files with 31 additions and 30 deletions

View File

@ -238,4 +238,32 @@ function common.normalize_path(filename)
end end
local function split_on_slash(s, sep_pattern)
local t = {}
for fragment in string.gmatch(s, "([^/\\]+)") do
t[#t + 1] = fragment
end
return t
end
function common.relative_path(ref_dir, dir)
local ref_ls = split_on_slash(ref_dir)
local dir_ls = split_on_slash(dir)
local i = 1
while i <= #ref_ls do
if dir_ls[i] ~= ref_ls[i] then
break
end
i = i + 1
end
local ups = ""
for k = i, #ref_ls do
ups = ups .. "../"
end
local rel_path = ups .. table.concat(dir_ls, "/", i)
return rel_path ~= "" and rel_path or "."
end
return common return common

View File

@ -185,7 +185,8 @@ function TreeView:on_mouse_pressed(button, x, y, clicks)
end end
else else
core.try(function() core.try(function()
core.root_view:open_doc(core.open_doc(self.hovered_item.abs_filename)) local doc_filename = common.relative_path(core.project_dir, self.hovered_item.abs_filename)
core.root_view:open_doc(core.open_doc(doc_filename))
end) end)
end end
end end

View File

@ -159,39 +159,11 @@ local function load_node(node, t)
end end
local function split_on_slash(s, sep_pattern)
local t = {}
for fragment in string.gmatch(s, "([^/\\]+)") do
t[#t + 1] = fragment
end
return t
end
local function relative_path(ref_dir, dir)
local ref_ls = split_on_slash(ref_dir)
local dir_ls = split_on_slash(dir)
local i = 1
while i <= #ref_ls do
if dir_ls[i] ~= ref_ls[i] then
break
end
i = i + 1
end
local ups = ""
for k = i, #ref_ls do
ups = ups .. "../"
end
local rel_path = ups .. table.concat(dir_ls, "/", i)
return rel_path ~= "" and rel_path or "."
end
local function save_directories() local function save_directories()
local project_dir = core.project_dir local project_dir = core.project_dir
local dir_list = {} local dir_list = {}
for i = 2, #core.project_directories do for i = 2, #core.project_directories do
dir_list[#dir_list + 1] = relative_path(project_dir, core.project_directories[i].name) dir_list[#dir_list + 1] = common.relative_path(project_dir, core.project_directories[i].name)
end end
return dir_list return dir_list
end end