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 info = system.get_file_info(file)
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)
entries_count = entries_count + 1
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.")
end
core.project_files = t
core.project_directories[1] = {filename = system.absolute_path("."), type = "dir"}
core.redraw = true
end
@ -252,6 +253,7 @@ function core.init()
core.docs = {}
core.threads = setmetatable({}, { __mode = "k" })
core.project_files = {}
core.project_directories = {}
core.redraw = true
core.visited_files = {}
core.restart_request = false

View File

@ -9,7 +9,7 @@ local View = require "core.view"
config.treeview_size = 200 * SCALE
local function get_depth(filename)
local n = 0
local n = 1
for sep in filename:gmatch("[\\/]") do
n = n + 1
end
@ -28,16 +28,36 @@ function TreeView:new()
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]
if not t then
t = {}
t.filename = item.filename
t.abs_filename = system.absolute_path(item.filename)
t.name = t.filename:match("[^\\/]+$")
if dirname then
local rel = relative_filename(item.filename, dirname)
-- FIXME: rel should never be nil here. to be verified.
t.filename = rel or item.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
self.cache[t.filename] = t
self.cache[item.filename] = t
end
return t
end
@ -73,9 +93,18 @@ function TreeView:each_item()
local h = self:get_item_height()
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
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)
y = y + h
@ -87,14 +116,18 @@ function TreeView:each_item()
else
local depth = cached.depth
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
i = i + 1
end
cached.skip = i
end
end
else
i = i + 1
end
end -- while files
end -- for directories
end)
end