Use relative path for filenames in project directories

This commit is contained in:
Francesco Abbate 2020-12-27 09:47:58 +01:00
parent 5449781353
commit 39181a56fd
2 changed files with 17 additions and 28 deletions

View File

@ -70,11 +70,11 @@ local function project_scan_thread()
return a.filename < b.filename
end
local function get_files(path, t)
local function get_files(root, path, t)
coroutine.yield()
t = t or {}
local size_limit = config.file_size_limit * 10e5
local all = system.list_dir(path) or {}
local all = system.list_dir(root .. path) or {}
local dirs, files = {}, {}
local entries_count = 0
@ -82,7 +82,7 @@ local function project_scan_thread()
for _, file in ipairs(all) do
if not common.match_pattern(file, config.ignore_files) then
local file = path .. PATHSEP .. file
local info = system.get_file_info(file)
local info = system.get_file_info(root .. file)
if info and info.size < size_limit then
info.filename = file
table.insert(info.type == "dir" and dirs or files, info)
@ -96,7 +96,7 @@ local function project_scan_thread()
for _, f in ipairs(dirs) do
table.insert(t, f)
if entries_count <= max_entries then
local subdir_t, subdir_count = get_files(f.filename, t)
local subdir_t, subdir_count = get_files(root, f.filename, t)
entries_count = entries_count + subdir_count
end
end
@ -114,7 +114,7 @@ local function project_scan_thread()
-- different
for i = 1, #core.project_directories do
local dir = core.project_directories[i]
local t, entries_count = get_files(dir.name)
local t, entries_count = get_files(dir.name, "")
if diff_files(dir.files, t) then
if entries_count > config.max_project_files then
core.status_view:show_message("!", style.accent,
@ -255,10 +255,11 @@ function core.init()
core.docs = {}
core.threads = setmetatable({}, { __mode = "k" })
local dir_path = system.absolute_path(".")
local dir_name = dir_path:match("[^\\/]+$")
core.project_directories = {
{
name = dir_path,
item = {filename = dir_path, type = "dir", top_dir = true},
item = {filename = dir_name, type = "dir"},
files = {},
}
}

View File

@ -9,7 +9,7 @@ local View = require "core.view"
config.treeview_size = 200 * SCALE
local function get_depth(filename)
local n = 1
local n = 0
for sep in filename:gmatch("[\\/]") do
n = n + 1
end
@ -28,14 +28,9 @@ function TreeView:new()
self.last = {}
end
local function relative_filename(filename, dirname)
local n = #dirname
if filename:sub(1, n) == dirname then
return filename:sub(n + 1):match('[/\\](.*)')
local function strip_leading_path(filename)
return filename:sub(2)
end
end
function TreeView:get_cached(item, dirname)
@ -47,17 +42,11 @@ function TreeView:get_cached(item, dirname)
local t = dir_cache[item.filename]
if not t then
t = {}
local rel = relative_filename(item.filename, dirname)
-- FIXME: rel should never be nil here. to be verified.
if item.top_dir then
t.filename = item.filename:match("[^\\/]+$")
t.depth = 0
else
t.filename = rel or item.filename
t.depth = get_depth(t.filename)
end
t.abs_filename = item.filename
t.name = t.filename:match("[^\\/]+$")
local basename = item.filename:match("[^\\/]+$")
t.filename = item.filename:match('^[/\\]') and strip_leading_path(item.filename) or basename
t.depth = get_depth(item.filename)
t.abs_filename = dirname .. item.filename
t.name = basename
t.type = item.type
dir_cache[item.filename] = t
end
@ -122,8 +111,7 @@ function TreeView:each_item()
else
local depth = cached.depth
while i <= #dir.files do
local filename = relative_filename(dir.files[i].filename, dir.name)
if get_depth(filename) <= depth then break end
if get_depth(dir.files[i].filename) <= depth then break end
i = i + 1
end
cached.skip = i
@ -153,7 +141,7 @@ function TreeView:on_mouse_pressed(button, x, y)
self.hovered_item.expanded = not self.hovered_item.expanded
else
core.try(function()
core.root_view:open_doc(core.open_doc(self.hovered_item.filename))
core.root_view:open_doc(core.open_doc(self.hovered_item.abs_filename))
end)
end
end