Skip to content

Commit

Permalink
feat: allow manage_folds to be a filetype map
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Oct 23, 2022
1 parent 26f0320 commit 88b5192
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ require("aerial").setup({
wintypes = "special",
},

-- Use symbol tree for folding. Set to true or false to enable/disable
-- Set to "auto" to manage folds if your previous foldmethod was 'manual'
-- This can be a filetype map (see :help aerial-filetype-map)
manage_folds = false,

-- When you fold code with za, zo, or zc, update the aerial tree as well.
-- Only works when manage_folds = true
link_folds_to_tree = false,
Expand All @@ -345,10 +350,6 @@ require("aerial").setup({
-- Only works when manage_folds = true
link_tree_to_folds = true,

-- Use symbol tree for folding. Set to true or false to enable/disable
-- 'auto' will manage folds if your previous foldmethod was 'manual'
manage_folds = false,

-- Set default symbol icons to use patched font icons (see https://www.nerdfonts.com/)
-- "auto" will set it to true if nvim-web-devicons or lspkind-nvim is installed.
nerd_font = "auto",
Expand Down
9 changes: 5 additions & 4 deletions doc/aerial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ OPTIONS *aerial-option
wintypes = "special",
},
-- Use symbol tree for folding. Set to true or false to enable/disable
-- Set to "auto" to manage folds if your previous foldmethod was 'manual'
-- This can be a filetype map (see :help aerial-filetype-map)
manage_folds = false,
-- When you fold code with za, zo, or zc, update the aerial tree as well.
-- Only works when manage_folds = true
link_folds_to_tree = false,
Expand All @@ -185,10 +190,6 @@ OPTIONS *aerial-option
-- Only works when manage_folds = true
link_tree_to_folds = true,
-- Use symbol tree for folding. Set to true or false to enable/disable
-- 'auto' will manage folds if your previous foldmethod was 'manual'
manage_folds = false,
-- Set default symbol icons to use patched font icons (see https://www.nerdfonts.com/)
-- "auto" will set it to true if nvim-web-devicons or lspkind-nvim is installed.
nerd_font = "auto",
Expand Down
16 changes: 6 additions & 10 deletions lua/aerial/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ local default_options = {
wintypes = "special",
},

-- Use symbol tree for folding. Set to true or false to enable/disable
-- Set to "auto" to manage folds if your previous foldmethod was 'manual'
-- This can be a filetype map (see :help aerial-filetype-map)
manage_folds = false,

-- When you fold code with za, zo, or zc, update the aerial tree as well.
-- Only works when manage_folds = true
link_folds_to_tree = false,
Expand All @@ -171,10 +176,6 @@ local default_options = {
-- Only works when manage_folds = true
link_tree_to_folds = true,

-- Use symbol tree for folding. Set to true or false to enable/disable
-- 'auto' will manage folds if your previous foldmethod was 'manual'
manage_folds = false,

-- Set default symbol icons to use patched font icons (see https://www.nerdfonts.com/)
-- "auto" will set it to true if nvim-web-devicons or lspkind-nvim is installed.
nerd_font = "auto",
Expand Down Expand Up @@ -479,12 +480,6 @@ M.setup = function(opts)
newconf.use_lspkind = true
end

-- If not managing folds, don't link either direction
if newconf.manage_folds == false then
newconf.link_tree_to_folds = false
newconf.link_folds_to_tree = false
end

-- for backwards compatibility
for k, _ in pairs(default_options.lsp) do
if newconf[k] ~= nil then
Expand Down Expand Up @@ -538,6 +533,7 @@ M.setup = function(opts)
for k, v in pairs(newconf) do
M[k] = v
end
M.manage_folds = create_filetype_opt_getter(M.manage_folds, default_options.manage_folds)
M.backends = create_filetype_opt_getter(M.backends, default_options.backends)
local get_filter_kind_list =
create_filetype_opt_getter(M.filter_kind, default_options.filter_kind)
Expand Down
16 changes: 8 additions & 8 deletions lua/aerial/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,26 @@ local config = require("aerial.config")
---@field children? aerial.Symbol[]

---@class aerial.BufData
---@field bufnr integer
---@field items aerial.Symbol[]
---@field positions any
---@field last_win integer
---@field collapsed table<string, boolean>
---@field collapse_level integer
local BufData = {}

function BufData.new()
---@param bufnr integer
function BufData.new(bufnr)
local new = {
bufnr = bufnr,
items = {},
positions = {},
last_win = -1,
collapsed = {},
collapse_level = 99,
}
-- cache the evaluation of managing folds
new.manage_folds = config.manage_folds(bufnr)
setmetatable(new, { __index = BufData })
return new
end
Expand Down Expand Up @@ -98,7 +103,7 @@ end
---@param item aerial.Symbol
---@return boolean
function BufData:is_collapsable(item)
return config.manage_folds or (item.children and not vim.tbl_isempty(item.children))
return self.manage_folds or (item.children and not vim.tbl_isempty(item.children))
end

---@generic T
Expand Down Expand Up @@ -166,19 +171,14 @@ local buf_to_symbols = {}

local M = {}

---@return aerial.BufData
function M.create()
return BufData.new()
end

---@param buf nil|integer
---@return aerial.BufData
function M.get_or_create(buf)
buf = buf or 0
local bufnr, _ = util.get_buffers(buf)
local bufdata = buf_to_symbols[bufnr]
if not bufdata then
bufdata = BufData.new()
bufdata = BufData.new(bufnr)
if bufnr then
buf_to_symbols[bufnr] = bufdata
end
Expand Down
4 changes: 2 additions & 2 deletions lua/aerial/fold.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local M = {}
---@param bufnr nil|integer
M.add_fold_mappings = function(bufnr)
bufnr = bufnr or 0
if config.link_folds_to_tree then
if config.manage_folds(bufnr) and config.link_folds_to_tree then
local aerial = require("aerial")
local tree = require("aerial.tree")
vim.keymap.set("n", "za", tree.toggle, { buffer = bufnr })
Expand Down Expand Up @@ -98,7 +98,7 @@ M.restore_foldmethod = function()
end

M.maybe_set_foldmethod = function(bufnr)
local manage_folds = config.manage_folds
local manage_folds = config.manage_folds(bufnr)
if not manage_folds then
return
end
Expand Down

0 comments on commit 88b5192

Please sign in to comment.