Change project files output to not include a prefix '/' in filename

It make sense to not put the initial '/' in the filename as the name
is relative to the top directory it belongs to.

By removing the the initial '/' in the filename we can re-introduce the
variable

core.project_files

to stay compatible with standard Lite. The project_files variable will
always point to the files table of the first entry of
core.project_directories.
This commit is contained in:
Francesco Abbate 2020-12-28 10:26:27 +01:00
parent 4e260cd03e
commit c042bfc907
5 changed files with 42 additions and 17 deletions

View File

@ -75,8 +75,8 @@ command.add(nil, {
local files = {}
for dir, item in core.get_project_files() do
if item.type == "file" then
local filename = dir == core.project_dir and item.filename:match('^[/\\](.+)') or dir .. item.filename
table.insert(files, common.home_encode(filename))
local path = (dir == core.project_dir and "" or dir .. PATHSEP)
table.insert(files, common.home_encode(path .. item.filename))
end
end
core.command_view:enter("Open File From Project", function(text, item)

View File

@ -61,6 +61,12 @@ function core.open_folder_project(dirname)
core.request_project_scan()
end
local function strip_leading_path(filename)
return filename:sub(2)
end
local function project_scan_thread()
local function diff_files(a, b)
if #a ~= #b then return true end
@ -76,6 +82,14 @@ local function project_scan_thread()
return a.filename < b.filename
end
-- "root" will by an absolute path without trailing '/'
-- "path" will be a path starting with '/' and without trailing '/'
-- or the empty string.
-- It will identifies a sub-path within "root.
-- The current path location will therefore always be: root .. path.
-- When recursing "root" will always be the same, only "path" will change.
-- Returns a list of file "items". In eash item the "filename" will be the
-- complete file path relative to "root" *without* the trailing '/'.
local function get_files(root, path, t)
coroutine.yield()
t = t or {}
@ -90,7 +104,7 @@ local function project_scan_thread()
local file = path .. PATHSEP .. file
local info = system.get_file_info(root .. file)
if info and info.size < size_limit then
info.filename = file
info.filename = strip_leading_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
@ -102,7 +116,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(root, f.filename, t)
local subdir_t, subdir_count = get_files(root, PATHSEP .. f.filename, t)
entries_count = entries_count + subdir_count
end
end
@ -128,6 +142,9 @@ local function project_scan_thread()
config.max_project_files.." files according to config.max_project_files.")
end
dir.files = t
if i == 1 then
core.project_files = t
end
core.redraw = true
end
end
@ -258,9 +275,12 @@ function core.load_user_directory()
end
function core.add_project_directory(path)
-- top directories has a file-like "item" but the item.filename
-- will be simply the name of the directory, without its path.
-- The field item.topdir will identify it as a top level directory.
table.insert(core.project_directories, {
name = path,
item = {filename = path:match("[^\\/]+$"), type = "dir"},
item = {filename = path:match("[^\\/]+$"), type = "dir", topdir = true},
files = {}
})
end
@ -295,9 +315,17 @@ function core.init()
core.log_items = {}
core.docs = {}
core.threads = setmetatable({}, { __mode = "k" })
-- core.project_files will always point to the files of
-- core.project_directories[1]. We assume the first entry of
-- project_directories will always be the project's directory.
-- core.project_files will therefore not include any of the added
-- directories.
core.project_dir = system.absolute_path(".")
core.project_directories = {}
core.add_project_directory(core.project_dir)
core.project_files = core.project_directories[1].files
core.redraw = true
core.visited_files = {}
core.restart_request = false

View File

@ -120,7 +120,7 @@ function StatusView:get_items()
style.icon_font, "g",
style.font, style.dim, self.separator2,
#core.docs, style.text, " / ",
core.project_files_number(), " files"
#core.project_files, " files"
}
end

View File

@ -52,7 +52,7 @@ function ResultsView:begin_search(text, fn)
local i = 1
for dir_name, file in core.get_project_files() do
if file.type == "file" then
find_all_matches_in_file(self.results, dir_name .. file.filename, fn)
find_all_matches_in_file(self.results, dir_name .. PATHSEP .. file.filename, fn)
end
self.last_file_idx = i
i = i + 1

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,10 +28,6 @@ function TreeView:new()
self.last = {}
end
local function strip_leading_path(filename)
return filename:sub(2)
end
function TreeView:get_cached(item, dirname)
local dir_cache = self.cache[dirname]
@ -43,14 +39,15 @@ function TreeView:get_cached(item, dirname)
if not t then
t = {}
local basename = item.filename:match("[^\\/]+$")
if item.filename:match('^[/\\]') then
t.filename = strip_leading_path(item.filename)
else
if item.topdir then
t.filename = basename
t.expanded = true
t.depth = 0
else
t.filename = item.filename
t.depth = get_depth(item.filename)
end
t.depth = get_depth(item.filename)
t.abs_filename = dirname .. item.filename
t.abs_filename = dirname .. PATHSEP .. item.filename
t.name = basename
t.type = item.type
dir_cache[item.filename] = t