Preliminary version of projects with multiple directories

This commit is contained in:
Francesco Abbate 2020-12-26 16:48:08 +01:00
parent 4416f219e4
commit 2f50f21f57
2 changed files with 60 additions and 25 deletions

View File

@ -84,7 +84,7 @@ local function project_scan_thread()
local file = (path ~= "." and path .. PATHSEP or "") .. file local file = (path ~= "." and path .. PATHSEP or "") .. file
local info = system.get_file_info(file) local info = system.get_file_info(file)
if info and info.size < size_limit then if info and info.size < size_limit then
info.filename = file info.filename = system.absolute_path(file)
table.insert(info.type == "dir" and dirs or files, info) table.insert(info.type == "dir" and dirs or files, info)
entries_count = entries_count + 1 entries_count = entries_count + 1
if entries_count > max_entries then break end if entries_count > max_entries then break end
@ -120,6 +120,7 @@ local function project_scan_thread()
config.max_project_files.." files according to config.max_project_files.") config.max_project_files.." files according to config.max_project_files.")
end end
core.project_files = t core.project_files = t
core.project_directories[1] = {filename = system.absolute_path("."), type = "dir"}
core.redraw = true core.redraw = true
end end
@ -252,6 +253,7 @@ function core.init()
core.docs = {} core.docs = {}
core.threads = setmetatable({}, { __mode = "k" }) core.threads = setmetatable({}, { __mode = "k" })
core.project_files = {} core.project_files = {}
core.project_directories = {}
core.redraw = true core.redraw = true
core.visited_files = {} core.visited_files = {}
core.restart_request = false core.restart_request = false

View File

@ -9,7 +9,7 @@ local View = require "core.view"
config.treeview_size = 200 * SCALE config.treeview_size = 200 * SCALE
local function get_depth(filename) local function get_depth(filename)
local n = 0 local n = 1
for sep in filename:gmatch("[\\/]") do for sep in filename:gmatch("[\\/]") do
n = n + 1 n = n + 1
end end
@ -28,16 +28,36 @@ function TreeView:new()
end end
function TreeView:get_cached(item) local function relative_filename(filename, dirname)
local n = #dirname
if filename:sub(1, n) == dirname then
return filename:sub(n + 1):match('[/\\](.*)')
end
end
local function belongs_to_directory(item, dirname)
return relative_filename(item.filename, dirname)
end
function TreeView:get_cached(item, dirname)
local t = self.cache[item.filename] local t = self.cache[item.filename]
if not t then if not t then
t = {} t = {}
t.filename = item.filename if dirname then
t.abs_filename = system.absolute_path(item.filename) local rel = relative_filename(item.filename, dirname)
t.name = t.filename:match("[^\\/]+$") -- FIXME: rel should never be nil here. to be verified.
t.filename = rel or item.filename
t.depth = get_depth(t.filename) t.depth = get_depth(t.filename)
else
t.filename = item.filename:match("[^\\/]+$")
t.depth = 0
end
t.abs_filename = item.filename
t.name = t.filename:match("[^\\/]+$")
t.type = item.type t.type = item.type
self.cache[t.filename] = t self.cache[item.filename] = t
end end
return t return t
end end
@ -73,9 +93,18 @@ function TreeView:each_item()
local h = self:get_item_height() local h = self:get_item_height()
local i = 1 local i = 1
-- FIXME: make this loop more efficient. Currently it takes O(M * N)
-- where M = (directories number) and N = (files number)
-- It should take O(N).
for idir, dir_item in ipairs(core.project_directories) do
local dir_entry = self:get_cached(dir_item)
local dir_name = dir_item.filename
coroutine.yield(dir_entry, ox, y, w, h)
y = y + h
while i <= #core.project_files do while i <= #core.project_files do
local item = core.project_files[i] local item = core.project_files[i]
local cached = self:get_cached(item) if belongs_to_directory(item, dir_name) then
local cached = self:get_cached(item, dir_name)
coroutine.yield(cached, ox, y, w, h) coroutine.yield(cached, ox, y, w, h)
y = y + h y = y + h
@ -87,14 +116,18 @@ function TreeView:each_item()
else else
local depth = cached.depth local depth = cached.depth
while i <= #core.project_files do while i <= #core.project_files do
local filename = core.project_files[i].filename local filename = relative_filename(core.project_files[i].filename, dir_name)
if get_depth(filename) <= depth then break end if get_depth(filename) <= depth then break end
i = i + 1 i = i + 1
end end
cached.skip = i cached.skip = i
end end
end end
else
i = i + 1
end end
end -- while files
end -- for directories
end) end)
end end