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**.
This commit is contained in:
hedy
2023-11-19 17:43:15 +08:00
parent 3b27272319
commit fa219c33af
4 changed files with 24 additions and 3 deletions

View File

@@ -60,7 +60,11 @@ M.defaults = {
}, },
symbol_folding = { symbol_folding = {
autofold_depth = nil, autofold_depth = nil,
auto_unfold_hover = true, auto_unfold_nodes = {
hovered = true,
---@type boolean|integer
only = true,
},
markers = { '', '' }, markers = { '', '' },
}, },
keymaps = { keymaps = {
@@ -306,6 +310,16 @@ function M.resolve_config()
end end
----- SYMBOLS FILTER ----- ----- SYMBOLS FILTER -----
M.resolve_filter_config() 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 end
---Ensure l is either table, false, or nil. If not, print warning using given ---Ensure l is either table, false, or nil. If not, print warning using given

View File

@@ -18,9 +18,14 @@ end
---@param node outline.SymbolNode|outline.FlatSymbolNode ---@param node outline.SymbolNode|outline.FlatSymbolNode
function M.is_folded(node) 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 if node.folded ~= nil then
return node.folded 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 return false
else else
return get_default_folded(node.depth) return get_default_folded(node.depth)

View File

@@ -79,7 +79,7 @@ end
function M.parse(response, bufnr) function M.parse(response, bufnr)
local sorted = lsp_utils.sort_symbols(response) 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 end
---Iterator that traverses the tree parent first before children, returning each node. ---Iterator that traverses the tree parent first before children, returning each node.

View File

@@ -38,6 +38,7 @@
---@field hierarchy boolean ---@field hierarchy boolean
---@field children? outline.SymbolNode[] ---@field children? outline.SymbolNode[]
---@field traversal_child integer Should NOT be modified during iteration using parser.preorder_iter ---@field traversal_child integer Should NOT be modified during iteration using parser.preorder_iter
---@field is_root boolean?
---@class outline.FlatSymbolNode ---@class outline.FlatSymbolNode
---@field name string ---@field name string
@@ -55,6 +56,7 @@
---@field hierarchy boolean ---@field hierarchy boolean
---@field children? outline.FlatSymbolNode[] ---@field children? outline.FlatSymbolNode[]
---@field traversal_child integer ---@field traversal_child integer
---@field is_root boolean?
---@field line_in_outline integer ---@field line_in_outline integer
---@field prefix_length integer ---@field prefix_length integer
---@field hovered boolean ---@field hovered boolean