Merge pull request #888 from Guldoman/PR_treeview_collapse_to_parent

Make `treeview:collapse` select parent if current item can't collapse
This commit is contained in:
Jefferson González 2022-03-18 18:01:44 -04:00 committed by GitHub
commit b5ead3992e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 1 deletions

View File

@ -427,6 +427,16 @@ function TreeView:draw()
end
end
function TreeView:get_parent(item)
local parent_path = common.dirname(item.abs_filename)
if not parent_path then return end
for it, _, y in self:each_item() do
if it.abs_filename == parent_path then
return it, y
end
end
end
function TreeView:toggle_expand(toggle)
local item = self.selected_item
@ -619,7 +629,16 @@ command.add(TreeView, {
end,
["treeview:collapse"] = function()
view:toggle_expand(false)
if view.selected_item then
if view.selected_item.type == "dir" and view.selected_item.expanded then
view:toggle_expand(false)
else
local parent_item, y = view:get_parent(view.selected_item)
if parent_item then
view:set_selection(parent_item, y)
end
end
end
end,
["treeview:expand"] = function()