Skip to content

Commit

Permalink
feat: some options can be set on a per-buffer basis (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Jul 29, 2023
1 parent 1a9896a commit cd44627
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
10 changes: 8 additions & 2 deletions doc/aerial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -651,14 +651,20 @@ 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"},
rust = {"lsp"},
}
<

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
Expand All @@ -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)
Expand Down
28 changes: 22 additions & 6 deletions lua/aerial/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/aerial/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit cd44627

Please sign in to comment.