Fix bug with expanding directory when file limited

Introduce a new field in items generated by TreeView:each_item()
to point "dir" to the toplevel directory entry.

In this was we can simplify the code and know if the toplevel
directory is files limited.
This commit is contained in:
Francesco Abbate 2021-07-15 12:17:18 +02:00
parent 865111738a
commit 593916ada7
4 changed files with 31 additions and 36 deletions

View File

@ -66,6 +66,7 @@ command.add(nil, {
end,
["core:find-file"] = function()
-- FIXME: core.project_files_limit was removed!
if core.project_files_limit then
return command.perform "core:open-file"
end

View File

@ -150,8 +150,8 @@ local function get_directory_files(root, path, t, recursive, begin_hook)
end
-- FIXME: change the name with core.scan_project_folder
function core.project_scan_topdir(index)
-- Populate a project folder top directory by scanning the filesystem.
function core.scan_project_folder(index)
local dir = core.project_directories[index]
local t, entries_count = get_directory_files(dir.name, "", {}, true)
if entries_count > config.max_project_files then
@ -185,22 +185,12 @@ function core.add_project_directory(path)
is_dirty = true,
watch_id = watch_id,
})
core.project_scan_topdir(#core.project_directories)
core.scan_project_folder(#core.project_directories)
core.redraw = true
end
function core.is_project_folder(dirname)
for _, dir in ipairs(core.project_directories) do
if dir.name == dirname then
return true
end
end
return false
end
function core.scan_project_folder(dirname, filename)
function core.scan_project_subdir(dirname, filename)
for _, dir in ipairs(core.project_directories) do
if dir.name == dirname then
for i, file in ipairs(dir.files) do
@ -212,15 +202,17 @@ function core.scan_project_folder(dirname, filename)
table.insert(dir.files, i + j, new_file)
end
file.scanned = true
return
return true
end
end
end
end
end
-- FIXME: rename this function
local function find_project_files_co(root, path)
-- Find files and directories recursively reading from the filesystem.
-- Filter files and yields file's directory and info table. This latter
-- is filled to be like required by project directories "files" list.
local function find_files_rec(root, path)
local size_limit = config.file_size_limit * 10e5
local all = system.list_dir(root .. path) or {}
for _, file in ipairs(all) do
@ -232,7 +224,7 @@ local function find_project_files_co(root, path)
if info.type == "file" then
coroutine.yield(root, info)
else
find_project_files_co(root, PATHSEP .. info.filename)
find_files_rec(root, PATHSEP .. info.filename)
end
end
end
@ -240,19 +232,25 @@ local function find_project_files_co(root, path)
end
-- Iterator function to list all project files
local function project_files_iter(state)
local dir = core.project_directories[state.dir_index]
if state.co then
-- We have a coroutine to fetch for files, use the coroutine.
-- Used for directories that exceeds the files nuumber limit.
local ok, name, file = coroutine.resume(state.co, dir.name, "")
if ok and name then
return name, file
else
-- The coroutine terminated, increment file/dir counter to scan
-- next project directory.
state.co = false
state.file_index = 1
state.dir_index = state.dir_index + 1
dir = core.project_directories[state.dir_index]
end
else
-- Increase file/dir counter
state.file_index = state.file_index + 1
while dir and state.file_index > #dir.files do
state.dir_index = state.dir_index + 1
@ -262,7 +260,9 @@ local function project_files_iter(state)
end
if not dir then return end
if dir.files_limit then
state.co = coroutine.create(find_project_files_co)
-- The current project directory is files limited: create a couroutine
-- to read files from the filesystem.
state.co = coroutine.create(find_files_rec)
return project_files_iter(state)
end
return dir.name, dir.files[state.file_index]

View File

@ -53,7 +53,7 @@ function TreeView:set_target_size(axis, value)
end
function TreeView:get_cached(item, dirname)
function TreeView:get_cached(dir, item, dirname)
local dir_cache = self.cache[dirname]
if not dir_cache then
dir_cache = {}
@ -79,6 +79,7 @@ function TreeView:get_cached(item, dirname)
end
t.name = basename
t.type = item.type
t.dir = dir -- points to top level "dir" item
dir_cache[cache_name] = t
end
return t
@ -125,14 +126,14 @@ function TreeView:each_item()
for k = 1, #core.project_directories do
local dir = core.project_directories[k]
local dir_cached = self:get_cached(dir.item, dir.name)
local dir_cached = self:get_cached(dir, dir.item, dir.name)
coroutine.yield(dir_cached, ox, y, w, h)
count_lines = count_lines + 1
y = y + h
local i = 1
while i <= #dir.files and dir_cached.expanded do
local item = dir.files[i]
local cached = self:get_cached(item, dir.name)
local cached = self:get_cached(dir, item, dir.name)
coroutine.yield(cached, ox, y, w, h)
count_lines = count_lines + 1
@ -216,19 +217,9 @@ function TreeView:on_mouse_pressed(button, x, y, clicks)
if keymap.modkeys["ctrl"] and button == "left" then
create_directory_in(hovered_item)
else
if core.project_files_limit and not hovered_item.expanded then
local filename, abs_filename = hovered_item.filename, hovered_item.abs_filename
local index = 0
-- The loop below is used to find the first match starting from the end
-- in case there are multiple matches.
while index and index + #filename < #abs_filename do
index = string.find(abs_filename, filename, index + 1, true)
end
-- we assume here index is not nil because the abs_filename must contain the
-- relative filename
local dirname = string.sub(abs_filename, 1, index - 2)
if core.is_project_folder(dirname) then
core.scan_project_folder(dirname, filename)
if hovered_item.dir.files_limit and not hovered_item.expanded then
local dirname = hovered_item.dir.name
if core.scan_project_subdir(dirname, hovered_item.filename) then
self:invalidate_cache(dirname)
end
end

View File

@ -1,6 +1,9 @@
`core.scan_project_folder`:
`core.scan_project_folder`: (renamed to `core.scan_project_subdir`)
scan a single folder, without recursion. Used when too many files.
New `core.scan_project_folder`:
Populate the project folder top directory. Done only once when the directory
is added to the project.
`core.add_project_directory`:
Add a new top-level folder to the project.