From 9af2fa8e1ee295b01640f237b947a26310c200a1 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Mon, 16 Nov 2020 14:45:45 +0100 Subject: [PATCH] Stop scanning project file when a maximum limit is reached To avoid excessive memory usage when opening in a directory with too many files. Introduce the config variable config.max_project_files to choose the limit. The mechanism introduced avoid using excessive memory but it fails to let user access all the files in the directory. A better implementation should not impose any limits but read each subdirectory on-demand, only as they are expanded in the tree-view. --- data/core/config.lua | 1 + data/core/init.lua | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/data/core/config.lua b/data/core/config.lua index a9f106f..c12e1e1 100644 --- a/data/core/config.lua +++ b/data/core/config.lua @@ -16,5 +16,6 @@ config.line_height = 1.2 config.indent_size = 2 config.tab_type = "soft" config.line_limit = 80 +config.max_project_files = 2000 return config diff --git a/data/core/init.lua b/data/core/init.lua index cef305e..d5a7163 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -34,6 +34,8 @@ local function project_scan_thread() local all = system.list_dir(path) or {} local dirs, files = {}, {} + local entries_count = 0 + local max_entries = config.max_project_files for _, file in ipairs(all) do if not common.match_pattern(file, config.ignore_files) then local file = (path ~= "." and path .. PATHSEP or "") .. file @@ -41,6 +43,8 @@ local function project_scan_thread() if info and info.size < size_limit then info.filename = file table.insert(info.type == "dir" and dirs or files, info) + entries_count = entries_count + 1 + if entries_count > max_entries then break end end end end @@ -48,7 +52,10 @@ local function project_scan_thread() table.sort(dirs, compare_file) for _, f in ipairs(dirs) do table.insert(t, f) - get_files(f.filename, t) + if entries_count <= max_entries then + local subdir_t, subdir_count = get_files(f.filename, t) + entries_count = entries_count + subdir_count + end end table.sort(files, compare_file) @@ -56,14 +63,19 @@ local function project_scan_thread() table.insert(t, f) end - return t + return t, entries_count end while true do -- get project files and replace previous table if the new table is -- different - local t = get_files(".") + local t, entries_count = get_files(".") if diff_files(core.project_files, t) then + if entries_count > config.max_project_files then + core.status_view:show_message("!", style.accent, + "Too many files in project directory: stopping reading at ".. + config.max_project_files.." files according to config.max_project_files.") + end core.project_files = t core.redraw = true end