From fa219c33aff2e0f27f2844bb03ce0d7d91aae10c Mon Sep 17 00:00:00 2001 From: hedy Date: Sun, 19 Nov 2023 17:43:15 +0800 Subject: [PATCH] feat: More auto-unfold options Option to auto unfold when there is only N root nodes in outline. Defaults to 1, meaning if there is only one 'root' parent, it should always be unfolded. This is useful if the entire file is a single function or a single 'return'. The old auto_unfold_hover option **still works as expected**. --- lua/outline/config.lua | 16 +++++++++++++++- lua/outline/folding.lua | 7 ++++++- lua/outline/parser.lua | 2 +- lua/outline/types/outline.lua | 2 ++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lua/outline/config.lua b/lua/outline/config.lua index a8c7dbe..83c4c06 100644 --- a/lua/outline/config.lua +++ b/lua/outline/config.lua @@ -60,7 +60,11 @@ M.defaults = { }, symbol_folding = { autofold_depth = nil, - auto_unfold_hover = true, + auto_unfold_nodes = { + hovered = true, + ---@type boolean|integer + only = true, + }, markers = { '', '' }, }, keymaps = { @@ -306,6 +310,16 @@ function M.resolve_config() end ----- SYMBOLS FILTER ----- M.resolve_filter_config() + ----- AUTO UNFOLD ----- + local au = M.o.symbol_folding.auto_unfold_nodes + if M.o.symbol_folding.auto_unfold_hover == nil then + if au.hovered ~= nil then + M.o.symbol_folding.auto_unfold_hover = au.hovered + end + end + if type(au.only) ~= 'number' then + au.only = (au.only and 1) or 0 + end end ---Ensure l is either table, false, or nil. If not, print warning using given diff --git a/lua/outline/folding.lua b/lua/outline/folding.lua index 46d83e9..8f59894 100644 --- a/lua/outline/folding.lua +++ b/lua/outline/folding.lua @@ -18,9 +18,14 @@ end ---@param node outline.SymbolNode|outline.FlatSymbolNode function M.is_folded(node) + local hover = cfg.o.symbol_folding.auto_unfold_hover + local only = cfg.o.symbol_folding.auto_unfold_nodes.only + if node.folded ~= nil then return node.folded - elseif node.hovered and cfg.o.symbol_folding.auto_unfold_hover then + elseif node.parent.is_root and node.parent.child_count <= only then + return false + elseif node.hovered and hover then return false else return get_default_folded(node.depth) diff --git a/lua/outline/parser.lua b/lua/outline/parser.lua index 99185d5..5396fdc 100644 --- a/lua/outline/parser.lua +++ b/lua/outline/parser.lua @@ -79,7 +79,7 @@ end function M.parse(response, bufnr) local sorted = lsp_utils.sort_symbols(response) - return parse_result(sorted, nil, nil, nil, bufnr) + return parse_result(sorted, nil, nil, { is_root = true, child_count = #sorted }, bufnr) end ---Iterator that traverses the tree parent first before children, returning each node. diff --git a/lua/outline/types/outline.lua b/lua/outline/types/outline.lua index 2851098..85b4c53 100644 --- a/lua/outline/types/outline.lua +++ b/lua/outline/types/outline.lua @@ -38,6 +38,7 @@ ---@field hierarchy boolean ---@field children? outline.SymbolNode[] ---@field traversal_child integer Should NOT be modified during iteration using parser.preorder_iter +---@field is_root boolean? ---@class outline.FlatSymbolNode ---@field name string @@ -55,6 +56,7 @@ ---@field hierarchy boolean ---@field children? outline.FlatSymbolNode[] ---@field traversal_child integer +---@field is_root boolean? ---@field line_in_outline integer ---@field prefix_length integer ---@field hovered boolean