Make `TreeView` more multi-project-dir aware
This commit is contained in:
parent
e94c996a26
commit
730ea0c91b
|
@ -518,6 +518,15 @@ function core.on_quit_project()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function is_project_folder(path)
|
local function is_project_folder(path)
|
||||||
|
for _,dir in pairs(core.project_directories) do
|
||||||
|
if dir.name == path then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local function is_primary_project_folder(path)
|
||||||
return core.project_dir == path
|
return core.project_dir == path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -547,6 +556,17 @@ menu:register(
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
menu:register(
|
||||||
|
function()
|
||||||
|
return view.hovered_item
|
||||||
|
and not is_primary_project_folder(view.hovered_item.abs_filename)
|
||||||
|
and is_project_folder(view.hovered_item.abs_filename)
|
||||||
|
end,
|
||||||
|
{
|
||||||
|
{ text = "Remove directory", command = "treeview:remove-project-directory" },
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
local previous_view = nil
|
local previous_view = nil
|
||||||
|
|
||||||
-- Register the TreeView commands and keymap
|
-- Register the TreeView commands and keymap
|
||||||
|
@ -637,7 +657,17 @@ command.add(TreeView, {
|
||||||
end,
|
end,
|
||||||
|
|
||||||
["treeview:expand"] = function()
|
["treeview:expand"] = function()
|
||||||
|
local item = view.selected_item
|
||||||
|
if not item or item.type ~= "dir" then return end
|
||||||
|
|
||||||
|
if item.expanded then
|
||||||
|
local next_item, _, next_y = view:get_next(item)
|
||||||
|
if next_item.depth > item.depth then
|
||||||
|
view:set_selection(next_item, next_y)
|
||||||
|
end
|
||||||
|
else
|
||||||
view:toggle_expand(true)
|
view:toggle_expand(true)
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -656,8 +686,13 @@ command.add(
|
||||||
)
|
)
|
||||||
end, {
|
end, {
|
||||||
["treeview:delete"] = function()
|
["treeview:delete"] = function()
|
||||||
local filename = treeitem().abs_filename
|
local item = treeitem()
|
||||||
local relfilename = treeitem().filename
|
local filename = item.abs_filename
|
||||||
|
local relfilename = item.filename
|
||||||
|
if item.dir_name ~= core.project_dir then
|
||||||
|
-- add secondary project dirs names to the file path to show
|
||||||
|
relfilename = common.basename(item.dir_name) .. PATHSEP .. relfilename
|
||||||
|
end
|
||||||
local file_info = system.get_file_info(filename)
|
local file_info = system.get_file_info(filename)
|
||||||
local file_type = file_info.type == "dir" and "Directory" or "File"
|
local file_type = file_info.type == "dir" and "Directory" or "File"
|
||||||
-- Ask before deleting
|
-- Ask before deleting
|
||||||
|
@ -697,13 +732,16 @@ command.add(
|
||||||
|
|
||||||
command.add(function() return treeitem() ~= nil end, {
|
command.add(function() return treeitem() ~= nil end, {
|
||||||
["treeview:rename"] = function()
|
["treeview:rename"] = function()
|
||||||
local old_filename = treeitem().filename
|
local item = treeitem()
|
||||||
local old_abs_filename = treeitem().abs_filename
|
local old_filename = item.filename
|
||||||
|
local old_abs_filename = item.abs_filename
|
||||||
core.command_view:set_text(old_filename)
|
core.command_view:set_text(old_filename)
|
||||||
core.command_view:enter("Rename", {
|
core.command_view:enter("Rename", {
|
||||||
submit = function(filename)
|
submit = function(filename)
|
||||||
filename = core.normalize_to_project_dir(filename)
|
local abs_filename = filename
|
||||||
local abs_filename = core.project_absolute_path(filename)
|
if not common.is_absolute_path(filename) then
|
||||||
|
abs_filename = item.dir_name .. PATHSEP .. filename
|
||||||
|
end
|
||||||
local res, err = os.rename(old_abs_filename, abs_filename)
|
local res, err = os.rename(old_abs_filename, abs_filename)
|
||||||
if res then -- successfully renamed
|
if res then -- successfully renamed
|
||||||
for _, doc in ipairs(core.docs) do
|
for _, doc in ipairs(core.docs) do
|
||||||
|
@ -718,38 +756,47 @@ command.add(function() return treeitem() ~= nil end, {
|
||||||
core.error("Error while renaming \"%s\" to \"%s\": %s", old_abs_filename, abs_filename, err)
|
core.error("Error while renaming \"%s\" to \"%s\": %s", old_abs_filename, abs_filename, err)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
suggest = common.path_suggest
|
suggest = function(text)
|
||||||
|
return common.path_suggest(text, item.dir_name)
|
||||||
|
end
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
|
|
||||||
["treeview:new-file"] = function()
|
["treeview:new-file"] = function()
|
||||||
if not is_project_folder(treeitem().abs_filename) then
|
local item = treeitem()
|
||||||
core.command_view:set_text(treeitem().filename .. "/")
|
if not is_project_folder(item.abs_filename) then
|
||||||
|
core.command_view:set_text(item.filename .. PATHSEP)
|
||||||
end
|
end
|
||||||
core.command_view:enter("Filename", {
|
core.command_view:enter("Filename", {
|
||||||
submit = function(filename)
|
submit = function(filename)
|
||||||
local doc_filename = core.project_dir .. PATHSEP .. filename
|
local doc_filename = item.dir_name .. PATHSEP .. filename
|
||||||
|
core.log(doc_filename)
|
||||||
local file = io.open(doc_filename, "a+")
|
local file = io.open(doc_filename, "a+")
|
||||||
file:write("")
|
file:write("")
|
||||||
file:close()
|
file:close()
|
||||||
core.root_view:open_doc(core.open_doc(doc_filename))
|
core.root_view:open_doc(core.open_doc(doc_filename))
|
||||||
core.log("Created %s", doc_filename)
|
core.log("Created %s", doc_filename)
|
||||||
end,
|
end,
|
||||||
suggest = common.path_suggest
|
suggest = function(text)
|
||||||
|
return common.path_suggest(text, item.dir_name)
|
||||||
|
end
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
|
|
||||||
["treeview:new-folder"] = function()
|
["treeview:new-folder"] = function()
|
||||||
if not is_project_folder(treeitem().abs_filename) then
|
local item = treeitem()
|
||||||
core.command_view:set_text(treeitem().filename .. "/")
|
if not is_project_folder(item.abs_filename) then
|
||||||
|
core.command_view:set_text(item.filename .. "/")
|
||||||
end
|
end
|
||||||
core.command_view:enter("Folder Name", {
|
core.command_view:enter("Folder Name", {
|
||||||
submit = function(filename)
|
submit = function(filename)
|
||||||
local dir_path = core.project_dir .. PATHSEP .. filename
|
local dir_path = item.dir_name .. PATHSEP .. filename
|
||||||
common.mkdirp(dir_path)
|
common.mkdirp(dir_path)
|
||||||
core.log("Created %s", dir_path)
|
core.log("Created %s", dir_path)
|
||||||
end,
|
end,
|
||||||
suggest = common.path_suggest
|
suggest = function(text)
|
||||||
|
return common.path_suggest(text, item.dir_name)
|
||||||
|
end
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
@ -766,6 +813,18 @@ command.add(function() return treeitem() ~= nil end, {
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
|
command.add(function()
|
||||||
|
local item = treeitem()
|
||||||
|
return item
|
||||||
|
and not is_primary_project_folder(item.abs_filename)
|
||||||
|
and is_project_folder(item.abs_filename)
|
||||||
|
end, {
|
||||||
|
["treeview:remove-project-directory"] = function()
|
||||||
|
core.remove_project_directory(treeitem().dir_name)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
keymap.add {
|
keymap.add {
|
||||||
["ctrl+\\"] = "treeview:toggle",
|
["ctrl+\\"] = "treeview:toggle",
|
||||||
["up"] = "treeview:previous",
|
["up"] = "treeview:previous",
|
||||||
|
|
Loading…
Reference in New Issue