mirror of
https://github.com/stevearc/aerial.nvim
synced 2024-09-16 14:34:08 +02:00
feat: config options for backends and treesitter
This commit is contained in:
parent
f327661d97
commit
dc2baa5f50
7 changed files with 122 additions and 53 deletions
50
README.md
50
README.md
|
@ -78,6 +78,9 @@ Command | arg | description
|
|||
|
||||
```lua
|
||||
vim.g.aerial = {
|
||||
-- Priority list of preferred backends for aerial
|
||||
backends = { "lsp", "treesitter" },
|
||||
|
||||
-- Enum: persist, close, auto, global
|
||||
-- persist - aerial window will stay open until closed
|
||||
-- close - aerial window will close when original file is no longer visible
|
||||
|
@ -99,10 +102,6 @@ vim.g.aerial = {
|
|||
-- Default behavior opens aerial relative to current window
|
||||
placement_editor_edge = false,
|
||||
|
||||
-- Fetch document symbols when LSP diagnostics change.
|
||||
-- If you set this to false, you will need to manually fetch symbols
|
||||
diagnostics_trigger_update = true,
|
||||
|
||||
-- Enum: split_width, full_width, last, none
|
||||
-- Determines line highlighting mode when multiple buffers are visible
|
||||
highlight_mode = "split_width",
|
||||
|
@ -145,18 +144,29 @@ vim.g.aerial = {
|
|||
-- Run this command after jumping to a symbol (false will disable)
|
||||
post_jump_cmd = "normal! zz",
|
||||
|
||||
-- Set to false to not update the symbols when there are LSP errors
|
||||
update_when_errors = true,
|
||||
lsp = {
|
||||
-- Fetch document symbols when LSP diagnostics change.
|
||||
-- If you set this to false, you will need to manually fetch symbols
|
||||
diagnostics_trigger_update = true,
|
||||
|
||||
-- A list of all symbols to display. Set to false to display all symbols.
|
||||
filter_kind = {
|
||||
"Class",
|
||||
"Constructor",
|
||||
"Enum",
|
||||
"Function",
|
||||
"Interface",
|
||||
"Method",
|
||||
"Struct",
|
||||
-- Set to false to not update the symbols when there are LSP errors
|
||||
update_when_errors = true,
|
||||
|
||||
-- A list of all symbols to display. Set to false to display all symbols.
|
||||
filter_kind = {
|
||||
"Class",
|
||||
"Constructor",
|
||||
"Enum",
|
||||
"Function",
|
||||
"Interface",
|
||||
"Method",
|
||||
"Struct",
|
||||
},
|
||||
},
|
||||
|
||||
treesitter = {
|
||||
-- How long to wait after a buffer change before updating
|
||||
update_delay = 300,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -171,6 +181,16 @@ vim.g.aerial = {
|
|||
}
|
||||
}
|
||||
|
||||
-- backends can also be specified as a filetype map.
|
||||
vim.g.aerial = {
|
||||
backends = {
|
||||
-- use underscore to specify the default behavior
|
||||
['_'] = {'lsp', 'treesitter'},
|
||||
python = {'treesitter'},
|
||||
rust = {'lsp'},
|
||||
}
|
||||
}
|
||||
|
||||
-- You can also override the default icons.
|
||||
vim.g.aerial = {
|
||||
icons = {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
local config = require("aerial.config")
|
||||
local M = {}
|
||||
|
||||
local function get_backend(name)
|
||||
|
@ -8,8 +9,7 @@ M.get = function(bufnr)
|
|||
if not bufnr or bufnr == 0 then
|
||||
bufnr = vim.api.nvim_get_current_buf()
|
||||
end
|
||||
-- TODO this should be a config option
|
||||
local candidates = { "lsp", "treesitter" }
|
||||
local candidates = config.get_backends(bufnr)
|
||||
for _, name in ipairs(candidates) do
|
||||
local backend = get_backend(name)
|
||||
if backend.is_supported(bufnr) then
|
||||
|
|
|
@ -79,7 +79,7 @@ M.symbol_callback = function(_err, result, context, _config)
|
|||
-- Don't update if there are diagnostics errors (or override by setting)
|
||||
local error_count = vim.lsp.diagnostic.get_count(bufnr, "Error")
|
||||
local has_symbols = data:has_symbols(bufnr)
|
||||
if not config.update_when_errors and error_count > 0 and has_symbols then
|
||||
if not config["lsp.update_when_errors"] and error_count > 0 and has_symbols then
|
||||
return
|
||||
end
|
||||
|
||||
|
|
|
@ -88,7 +88,7 @@ M.on_attach = function(client, bufnr, opts)
|
|||
end
|
||||
|
||||
M.attach = function(bufnr)
|
||||
if config.diagnostics_trigger_update then
|
||||
if config["lsp.diagnostics_trigger_update"] then
|
||||
vim.cmd([[augroup AerialDiagnostics
|
||||
au!
|
||||
au User LspDiagnosticsChanged lua require'aerial.backends.lsp'._on_diagnostics_changed()
|
||||
|
@ -96,7 +96,7 @@ M.attach = function(bufnr)
|
|||
augroup END
|
||||
]])
|
||||
end
|
||||
if config.open_automatic() and not config.diagnostics_trigger_update then
|
||||
if config.open_automatic() and not config["lsp.diagnostics_trigger_update"] then
|
||||
M.fetch_symbols()
|
||||
end
|
||||
end
|
||||
|
@ -111,7 +111,7 @@ M._on_diagnostics_changed = function()
|
|||
end
|
||||
local errors = vim.lsp.diagnostic.get_count(0, "Error")
|
||||
-- if no errors, refresh symbols
|
||||
if config.update_when_errors or errors == 0 or not data:has_symbols() then
|
||||
if config["lsp.update_when_errors"] or errors == 0 or not data:has_symbols() then
|
||||
M.fetch_symbols()
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
local backends = require("aerial.backends")
|
||||
local config = require("aerial.config")
|
||||
local parsers = require("nvim-treesitter.parsers")
|
||||
local query = require("nvim-treesitter.query")
|
||||
local ts_utils = require("nvim-treesitter.ts_utils")
|
||||
local utils = require("nvim-treesitter.utils")
|
||||
local M = {}
|
||||
|
||||
-- TODO this should be a config option
|
||||
local CHANGE_DELAY = 1000
|
||||
local kind_map = {
|
||||
local_function = "Function",
|
||||
function_definition = "Function",
|
||||
|
@ -123,7 +122,7 @@ local function throttle_update()
|
|||
end
|
||||
timer = vim.loop.new_timer()
|
||||
timer:start(
|
||||
CHANGE_DELAY,
|
||||
config["treesitter.update_delay"],
|
||||
0,
|
||||
vim.schedule_wrap(function()
|
||||
timer:close()
|
||||
|
|
|
@ -3,6 +3,9 @@ local has_devicons = pcall(require, "nvim-web-devicons")
|
|||
|
||||
-- Copy this to the README after modification
|
||||
local default_options = {
|
||||
-- Priority list of preferred backends for aerial
|
||||
backends = { "lsp", "treesitter" },
|
||||
|
||||
-- Enum: persist, close, auto, global
|
||||
-- persist - aerial window will stay open until closed
|
||||
-- close - aerial window will close when original file is no longer visible
|
||||
|
@ -24,10 +27,6 @@ local default_options = {
|
|||
-- Default behavior opens aerial relative to current window
|
||||
placement_editor_edge = false,
|
||||
|
||||
-- Fetch document symbols when LSP diagnostics change.
|
||||
-- If you set this to false, you will need to manually fetch symbols
|
||||
diagnostics_trigger_update = true,
|
||||
|
||||
-- Enum: split_width, full_width, last, none
|
||||
-- Determines line highlighting mode when multiple buffers are visible
|
||||
highlight_mode = "split_width",
|
||||
|
@ -70,21 +69,65 @@ local default_options = {
|
|||
-- Run this command after jumping to a symbol (false will disable)
|
||||
post_jump_cmd = "normal! zz",
|
||||
|
||||
-- Set to false to not update the symbols when there are LSP errors
|
||||
update_when_errors = true,
|
||||
lsp = {
|
||||
-- Fetch document symbols when LSP diagnostics change.
|
||||
-- If you set this to false, you will need to manually fetch symbols
|
||||
diagnostics_trigger_update = true,
|
||||
|
||||
-- A list of all symbols to display. Set to false to display all symbols.
|
||||
filter_kind = {
|
||||
"Class",
|
||||
"Constructor",
|
||||
"Enum",
|
||||
"Function",
|
||||
"Interface",
|
||||
"Method",
|
||||
"Struct",
|
||||
-- Set to false to not update the symbols when there are LSP errors
|
||||
update_when_errors = true,
|
||||
|
||||
-- A list of all symbols to display. Set to false to display all symbols.
|
||||
filter_kind = {
|
||||
"Class",
|
||||
"Constructor",
|
||||
"Enum",
|
||||
"Function",
|
||||
"Interface",
|
||||
"Method",
|
||||
"Struct",
|
||||
},
|
||||
},
|
||||
|
||||
treesitter = {
|
||||
-- How long to wait after a buffer change before updating
|
||||
update_delay = 300,
|
||||
},
|
||||
}
|
||||
|
||||
local function split(string, pattern)
|
||||
local ret = {}
|
||||
for token in string.gmatch(string, "[^" .. pattern .. "]+") do
|
||||
table.insert(ret, token)
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
local function getkey(t, key)
|
||||
local cur = t[key]
|
||||
if cur ~= nil then
|
||||
return cur
|
||||
end
|
||||
-- This is for backwards compatibility with lsp options that used to be in the
|
||||
-- global namespace
|
||||
if string.find(key, "lsp%.") == 1 then
|
||||
cur = getkey(t, string.sub(key, 5))
|
||||
if cur ~= nil then
|
||||
return cur
|
||||
end
|
||||
end
|
||||
|
||||
local pieces = split(key, "\\.")
|
||||
cur = t
|
||||
for _, piece in ipairs(pieces) do
|
||||
if cur == nil then
|
||||
return nil
|
||||
end
|
||||
cur = cur[piece]
|
||||
end
|
||||
return cur
|
||||
end
|
||||
|
||||
-- config options that are valid as bools, but don't have bools as the default
|
||||
local addl_bool_opts = {
|
||||
highlight_mode = true,
|
||||
|
@ -92,16 +135,18 @@ local addl_bool_opts = {
|
|||
manage_folds = true,
|
||||
nerd_font = true,
|
||||
post_jump_cmd = true,
|
||||
filter_kind = true,
|
||||
lsp = {
|
||||
filter_kind = true,
|
||||
},
|
||||
}
|
||||
|
||||
local function get_option(opt)
|
||||
local ret = vim.g[string.format("aerial_%s", opt)]
|
||||
local ret = vim.g[string.format("aerial_%s", string.gsub(opt, "%.", "_"))]
|
||||
if ret == nil then
|
||||
ret = (vim.g.aerial or {})[opt]
|
||||
ret = getkey((vim.g.aerial or {}), opt)
|
||||
end
|
||||
-- People are used to using 1/0 for v:true/v:false in vimscript
|
||||
if type(default_options[opt]) == "boolean" or addl_bool_opts[opt] then
|
||||
if type(getkey(default_options, opt)) == "boolean" or getkey(addl_bool_opts, opt) then
|
||||
if ret == 0 then
|
||||
ret = false
|
||||
elseif ret == 1 then
|
||||
|
@ -109,7 +154,7 @@ local function get_option(opt)
|
|||
end
|
||||
end
|
||||
if ret == nil then
|
||||
return default_options[opt]
|
||||
return getkey(default_options, opt)
|
||||
else
|
||||
return ret
|
||||
end
|
||||
|
@ -121,13 +166,13 @@ setmetatable(M, {
|
|||
end,
|
||||
})
|
||||
|
||||
local function get_table_default(table, key, default_key, default)
|
||||
if type(table) ~= "table" then
|
||||
return table
|
||||
local function get_table_default(tab, key, default_key, default)
|
||||
if type(tab) ~= "table" or vim.tbl_islist(tab) then
|
||||
return tab
|
||||
end
|
||||
local ret = table[key]
|
||||
local ret = tab[key]
|
||||
if ret == nil and default_key then
|
||||
ret = table[default_key]
|
||||
ret = tab[default_key]
|
||||
end
|
||||
if ret == nil then
|
||||
return default
|
||||
|
@ -137,12 +182,12 @@ local function get_table_default(table, key, default_key, default)
|
|||
end
|
||||
|
||||
local function get_table_opt(opt, key, default_key, default)
|
||||
return get_table_default(get_option(opt) or {}, key, default_key, default)
|
||||
return get_table_default(get_option(opt), key, default_key, default)
|
||||
end
|
||||
|
||||
M.include_kind = function(kind)
|
||||
local fk = M.filter_kind
|
||||
return not fk or vim.tbl_contains(M.filter_kind, kind)
|
||||
local fk = M["lsp.filter_kind"]
|
||||
return not fk or vim.tbl_contains(fk, kind)
|
||||
end
|
||||
|
||||
M.open_automatic = function()
|
||||
|
@ -152,6 +197,11 @@ M.open_automatic = function()
|
|||
return ret and ret ~= 0
|
||||
end
|
||||
|
||||
M.get_backends = function(bufnr)
|
||||
local ft = vim.api.nvim_buf_get_option(bufnr or 0, "filetype")
|
||||
return get_table_opt("backends", ft, "_", {})
|
||||
end
|
||||
|
||||
-- stylua: ignore
|
||||
local plain_icons = {
|
||||
Array = '[a]';
|
||||
|
|
|
@ -18,7 +18,7 @@ M.update_aerial_buffer = function(buf)
|
|||
end
|
||||
if not data:has_symbols(bufnr) then
|
||||
local lines = { "No symbols" }
|
||||
if config.filter_kind ~= false then
|
||||
if config["lsp.filter_kind"] ~= false then
|
||||
table.insert(lines, ":help filter_kind")
|
||||
end
|
||||
util.render_centered_text(aer_bufnr, lines)
|
||||
|
|
Loading…
Reference in a new issue