Finalize add project directory feature

This commit is contained in:
Francesco Abbate 2020-12-27 11:32:52 +01:00
parent 39181a56fd
commit e4fa196b33
3 changed files with 63 additions and 16 deletions

View File

@ -73,14 +73,14 @@ command.add(nil, {
["core:find-file"] = function()
local files = {}
for _, item in pairs(core.project_files) do
for dir, item in core.project_files() do
if item.type == "file" then
table.insert(files, item.filename)
local filename = dir == core.project_dir and item.filename:match('^[/\\](.+)') or dir .. item.filename
table.insert(files, common.home_encode(filename))
end
end
core.command_view:enter("Open File From Project", function(text, item)
text = item and item.text or text
core.root_view:open_doc(core.open_doc(text))
core.root_view:open_doc(core.open_doc(common.home_expand(text)))
end, function(text)
if text == "" then
local recent_files = {}
@ -164,4 +164,21 @@ command.add(nil, {
system.exec(string.format("%q %q", EXEFILE, text))
end, suggest_directory)
end,
["core:add-directory"] = function()
core.command_view:enter("Open Project", function(text)
text = common.home_expand(text)
local path_stat, err = system.get_file_info(text)
if not path_stat then
core.error("cannot open %q: %s", text, err)
return
elseif path_stat.type ~= 'dir' then
core.error("%q is not a directory", text)
return
end
core.add_project_directory(system.absolute_path(text))
core.request_project_scan()
end, suggest_directory)
end,
})

View File

@ -47,12 +47,18 @@ local function save_projects()
end
end
function core.request_project_scan()
core.threads[core.project_scan_thread_id].wake = 0
end
function core.open_folder_project(dirname)
core.root_view:close_all_docviews()
add_project_to_recents(dirname)
save_projects()
core.switch_project = dirname
core.threads[core.project_scan_thread_id].wake = 0
core.request_project_scan()
end
local function project_scan_thread()
@ -137,6 +143,24 @@ local function project_scan_thread()
end
local function project_files_iter(state)
local dir = core.project_directories[state.dir_index]
state.file_index = state.file_index + 1
while dir and state.file_index > #dir.files do
state.dir_index = state.dir_index + 1
state.file_index = 1
dir = core.project_directories[state.dir_index]
end
if not dir then return end
return dir.name, dir.files[state.file_index]
end
function core.project_files()
local state = { dir_index = 1, file_index = 0 }
return project_files_iter, state
end
-- create a directory using mkdir but may need to create the parent
-- directories as well.
local function create_user_directory()
@ -223,6 +247,13 @@ function core.load_user_directory()
end)
end
function core.add_project_directory(path)
table.insert(core.project_directories, {
name = path,
item = {filename = path:match("[^\\/]+$"), type = "dir"},
files = {}
})
end
function core.init()
command = require "core.command"
@ -254,15 +285,9 @@ function core.init()
core.log_items = {}
core.docs = {}
core.threads = setmetatable({}, { __mode = "k" })
local dir_path = system.absolute_path(".")
local dir_name = dir_path:match("[^\\/]+$")
core.project_directories = {
{
name = dir_path,
item = {filename = dir_name, type = "dir"},
files = {},
}
}
core.project_dir = system.absolute_path(".")
core.project_directories = {}
core.add_project_directory(core.project_dir)
core.redraw = true
core.visited_files = {}
core.restart_request = false

View File

@ -43,7 +43,12 @@ function TreeView:get_cached(item, dirname)
if not t then
t = {}
local basename = item.filename:match("[^\\/]+$")
t.filename = item.filename:match('^[/\\]') and strip_leading_path(item.filename) or basename
if item.filename:match('^[/\\]') then
t.filename = strip_leading_path(item.filename)
else
t.filename = basename
t.expanded = true
end
t.depth = get_depth(item.filename)
t.abs_filename = dirname .. item.filename
t.name = basename
@ -97,7 +102,7 @@ function TreeView:each_item()
coroutine.yield(dir_cached, ox, y, w, h)
y = y + h
local i = 1
while i <= #dir.files do
while i <= #dir.files and dir_cached.expanded do
local item = dir.files[i]
local cached = self:get_cached(item, dir.name)