feat: some options can be set on a per-buffer basis (#280)

This commit is contained in:
Steven Arcangeli 2023-07-29 10:40:13 -07:00
parent 1a9896a7ca
commit cd446279f1
4 changed files with 35 additions and 13 deletions

View file

@ -651,7 +651,7 @@ NOTES *aerial-note
*aerial-filetype-map*
Certain options can be configured per-filetype by passing in a table. "_" will
be used as the default if the filetype is not present.
>
>lua
backends = {
['_'] = {"lsp", "treesitter"},
python = {"treesitter"},
@ -659,6 +659,12 @@ be used as the default if the filetype is not present.
}
<
You can also specify a value on a per-buffer basis by setting a buffer-local
variable. For example: >lua
vim.b.aerial_backends = { "lsp" }
<
*aerial-filter*
If you don't see any symbols in aerial when you expect to, it could be that
the symbol kinds are being filtered out. Aerial only shows a subset of symbols
@ -670,7 +676,7 @@ filtering.
Aerial can be configured to open automatically in certain conditions. To
replicate the old behavior you could get with `open_automatic_min_lines` and
`open_automatic_min_symbols`, use the following:
>
>lua
local aerial = require("aerial")
aerial.setup({
open_automatic = function(bufnr)

View file

@ -417,13 +417,27 @@ M.get_filetypes = function(bufnr)
return vim.split(ft, "%.")
end
local function create_filetype_opt_getter(option, default)
---@param name string
---@param option any
---@param default any
local function create_filetype_opt_getter(name, option, default)
local buffer_option_name = string.format("aerial_%s", name)
if type(option) ~= "table" or vim.tbl_islist(option) then
return function()
return option
return function(bufnr)
local has_buf_option, buf_option = pcall(vim.api.nvim_buf_get_var, bufnr, buffer_option_name)
print(buffer_option_name, has_buf_option, vim.inspect(buf_option))
if has_buf_option then
return buf_option
else
return option
end
end
else
return function(bufnr)
local has_buf_option, buf_option = pcall(vim.api.nvim_buf_get_var, bufnr, buffer_option_name)
if has_buf_option then
return buf_option
end
for _, ft in ipairs(M.get_filetypes(bufnr)) do
if option[ft] ~= nil then
return option[ft]
@ -523,10 +537,12 @@ 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)
M.manage_folds =
create_filetype_opt_getter("manage_folds", M.manage_folds, default_options.manage_folds)
M.backends = create_filetype_opt_getter("backends", M.backends, default_options.backends)
local get_filter_kind_list =
create_filetype_opt_getter(M.filter_kind, default_options.filter_kind)
create_filetype_opt_getter("filter_kind", M.filter_kind, default_options.filter_kind)
---@param bufnr integer
M.get_filter_kind_map = function(bufnr)
local fk = get_filter_kind_list(bufnr)
if fk == false or fk == 0 then

View file

@ -434,7 +434,7 @@ end
M.info = function()
do_setup()
local util = require("aerial.util")
local bufnr = util.get_buffers(0)
local bufnr = util.get_buffers(0) or 0
local filetype = vim.bo[bufnr].filetype
local ignored, message = util.is_ignored_win()
return {
@ -443,7 +443,7 @@ M.info = function()
message = message,
},
filetype = filetype,
filter_kind_map = require("aerial.config").get_filter_kind_map(),
filter_kind_map = require("aerial.config").get_filter_kind_map(bufnr),
backends = require("aerial.backends").get_status(bufnr),
}
end

View file

@ -61,7 +61,7 @@ describe("config", function()
config.setup({
filter_kind = { "Function" },
})
local fk = config.get_filter_kind_map()
local fk = config.get_filter_kind_map(0)
assert.equals(nil, fk.Class)
assert.equals(true, fk.Function)
end)
@ -70,7 +70,7 @@ describe("config", function()
filter_kind = { foo = { "Function" } },
})
vim.api.nvim_buf_set_option(0, "filetype", "foo")
local fk = config.get_filter_kind_map()
local fk = config.get_filter_kind_map(0)
assert.equals(nil, fk.Class)
assert.equals(true, fk.Function)
end)
@ -79,7 +79,7 @@ describe("config", function()
filter_kind = { foo = false },
})
vim.api.nvim_buf_set_option(0, "filetype", "foo")
local fk = config.get_filter_kind_map()
local fk = config.get_filter_kind_map(0)
assert.equals(true, fk.Class)
assert.equals(true, fk.Function)
end)