feat: treesitter respects filter_kind and filter_kind can by per-filetype

This commit is contained in:
Steven Arcangeli 2021-11-20 17:55:53 -08:00
parent ccc06af808
commit 611757da0a
5 changed files with 87 additions and 63 deletions

View file

@ -98,9 +98,16 @@ vim.g.aerial = {
-- different buffer in the way of the preferred direction
default_direction = "prefer_right",
-- Set to true to only open aerial at the far right/left of the editor
-- Default behavior opens aerial relative to current window
placement_editor_edge = false,
-- A list of all symbols to display. Set to false to display all symbols.
filter_kind = {
"Class",
"Constructor",
"Enum",
"Function",
"Interface",
"Method",
"Struct",
},
-- Enum: split_width, full_width, last, none
-- Determines line highlighting mode when multiple buffers are visible
@ -141,6 +148,10 @@ vim.g.aerial = {
-- If open_automatic is true, only open aerial if there are at least this many symbols
open_automatic_min_symbols = 0,
-- Set to true to only open aerial at the far right/left of the editor
-- Default behavior opens aerial relative to current window
placement_editor_edge = false,
-- Run this command after jumping to a symbol (false will disable)
post_jump_cmd = "normal! zz",
@ -151,17 +162,6 @@ vim.g.aerial = {
-- 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 = {
@ -191,6 +191,15 @@ vim.g.aerial = {
}
}
-- filter_kind can also be specified as a filetype map.
vim.g.aerial = {
filter_kind = {
-- use underscore to specify the default behavior
['_'] = {"Class", "Function", "Interface", "Method", "Struct"},
c = {"Namespace", "Function", "Struct", "Enum"}
}
}
-- You can also override the default icons.
vim.g.aerial = {
icons = {

View file

@ -9,7 +9,8 @@ local function get_symbol_kind_name(kind_number)
return protocol.SymbolKind[kind_number] or "Unknown"
end
local function process_symbols(symbols)
local function process_symbols(symbols, bufnr)
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
local function _process_symbols(_symbols, parent, list, level)
for _, symbol in ipairs(_symbols) do
local kind = get_symbol_kind_name(symbol.kind)
@ -19,7 +20,7 @@ local function process_symbols(symbols)
elseif symbol.range then -- DocumentSymbol type
range = symbol.range
end
local include_item = range and config.include_kind(kind)
local include_item = range and config.include_kind(kind, filetype)
if include_item then
local item = {
@ -67,7 +68,7 @@ local function process_symbols(symbols)
end
M.handle_symbols = function(result, bufnr)
backends.set_symbols(bufnr, process_symbols(result))
backends.set_symbols(bufnr, process_symbols(result, bufnr))
end
local results = {}

View file

@ -18,6 +18,7 @@ M.fetch_symbols_sync = function(timeout)
local ts_utils = require("nvim-treesitter.ts_utils")
local utils = require("nvim-treesitter.utils")
local bufnr = 0
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
local parser = parsers.get_parser(bufnr)
local items = {}
if parser then
@ -46,12 +47,6 @@ M.fetch_symbols_sync = function(timeout)
local loc_node = (utils.get_at_path(match, "location") or {}).node
local parent, parent_node, level = get_parent(type_node)
if type_node and type_node ~= parent_node then
local row, col
if loc_node then
row, col = loc_node:start()
else
row, col = type_node:start()
end
local kind = kind_map[type_node:type()]
if not kind then
vim.api.nvim_err_writeln(
@ -59,29 +54,37 @@ M.fetch_symbols_sync = function(timeout)
)
break
end
local name
if name_node then
name = ts_utils.get_node_text(name_node, bufnr)[1] or "<parse error>"
else
name = "<Anonymous>"
end
local item = {
kind = kind_map[type_node:type()],
name = name,
level = level,
parent = parent,
lnum = row + 1,
col = col,
}
if parent then
if not parent.children then
parent.children = {}
if config.include_kind(kind, filetype) then
local row, col
if loc_node then
row, col = loc_node:start()
else
row, col = type_node:start()
end
table.insert(parent.children, item)
else
table.insert(items, item)
local name
if name_node then
name = ts_utils.get_node_text(name_node, bufnr)[1] or "<parse error>"
else
name = "<Anonymous>"
end
local item = {
kind = kind,
name = name,
level = level,
parent = parent,
lnum = row + 1,
col = col,
}
if parent then
if not parent.children then
parent.children = {}
end
table.insert(parent.children, item)
else
table.insert(items, item)
end
table.insert(stack, { type_node, item })
end
table.insert(stack, { type_node, item })
end
end
end

View file

@ -23,9 +23,16 @@ local default_options = {
-- different buffer in the way of the preferred direction
default_direction = "prefer_right",
-- Set to true to only open aerial at the far right/left of the editor
-- Default behavior opens aerial relative to current window
placement_editor_edge = false,
-- A list of all symbols to display. Set to false to display all symbols.
filter_kind = {
"Class",
"Constructor",
"Enum",
"Function",
"Interface",
"Method",
"Struct",
},
-- Enum: split_width, full_width, last, none
-- Determines line highlighting mode when multiple buffers are visible
@ -66,6 +73,10 @@ local default_options = {
-- If open_automatic is true, only open aerial if there are at least this many symbols
open_automatic_min_symbols = 0,
-- Set to true to only open aerial at the far right/left of the editor
-- Default behavior opens aerial relative to current window
placement_editor_edge = false,
-- Run this command after jumping to a symbol (false will disable)
post_jump_cmd = "normal! zz",
@ -76,17 +87,6 @@ local default_options = {
-- 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 = {
@ -135,9 +135,7 @@ local addl_bool_opts = {
manage_folds = true,
nerd_font = true,
post_jump_cmd = true,
lsp = {
filter_kind = true,
},
filter_kind = true,
}
local function get_option(opt)
@ -185,8 +183,18 @@ local function get_table_opt(opt, key, default_key, default)
return get_table_default(get_option(opt), key, default_key, default)
end
M.include_kind = function(kind)
local fk = M["lsp.filter_kind"]
M.include_kind = function(kind, filetype)
local fk = M.filter_kind
if type(fk) == "table" and not vim.tbl_islist(fk) then
local filetype_fk = fk[filetype]
if filetype_fk == nil then
filetype_fk = fk["_"]
if filetype_fk == nil then
filetype_fk = default_options.filter_kind
end
end
fk = filetype_fk
end
return not fk or vim.tbl_contains(fk, kind)
end

View file

@ -4,6 +4,9 @@ vim.cmd([[runtime! plugin/nvim-treesitter.vim]])
vim.o.swapfile = false
vim.bo.swapfile = false
vim.g.aerial = {
filter_kind = false,
}
require("nvim-treesitter.configs").setup({
ensure_installed = "maintained",