feat: Symbol filtering config structure

This commit introduces a basic framework for symbol filtering in
outline.nvim, where users can set per-filetype kinds to filter - include
or exclude for each filetype.

As a side effect the checking of symbol inclusion function has been
improved to O(1) time-complexity (previously O(n)). You can see this
from types/outline.lua and config.lua: a lookup table is used to check
if a kind is filtered, rather than looping through a list each time.
Former takes O(1) for lookup whereas the old implementation would be
O(n) for *each* node!

The old symbols.blacklist option *still works as expected*.

The schema for the new confit is detailed in #23 and types/outline.lua.
By the way, this commit also closes #23.

These should equivalent:
    symbols.blacklist = { 'Function', 'Method' }
    symbols.filter = { 'Function', 'Method', exclude=true }
    symbols.filter = {
      ['*'] = { 'Function', 'Method', exclude=true }
    }

And these should be equivalent:
    symbols.blacklist = {}
    symbols.filter = false
    symbols.filter = nil
    symbols.filter = { ['*'] = false }
    symbols.filter = { ['*'] = { exclude = true } }
    symbols.filter = { exclude = true }

The last two of which could be considered unidiomatic.

When multiple filetypes are specified, filetype specific filters
are NOT merged with the default ('*') filter, they are independent. If a
filetype is used, the default filter is not considered. The default
filter is only considered if a filetype filter for the given buffer is
not provided.

LIMITATIONS:
This is carried over from the implementation from symbols-outline:
filters can only be applied to parents at the moment. I.e.: If some node
has a kind that is excluded, all its children will NOT be considered.

Filters are only applied to children if its parent was not excluded
during filtering.

Also extracted all types into types module, and updated conversion
script to use the new symbols.filter opt.

NOTE:
On outline open it appears that parsing functions are called twice?
I should definitely add tests soon.
This commit is contained in:
hedy
2023-11-16 21:21:55 +08:00
parent 5278eb5b2b
commit 24680f13f7
7 changed files with 244 additions and 80 deletions

View File

@@ -7,23 +7,6 @@ local folding = require 'outline.folding'
local M = {}
---@class outline.SymbolNode
---@field name string
---@field depth integer
---@field parent outline.SymbolNode
---@field deprecated boolean
---@field kind integer|string
---@field icon string
---@field detail string
---@field line integer
---@field character integer
---@field range_start integer
---@field range_end integer
---@field isLast boolean
---@field hierarchy boolean
---@field children? outline.SymbolNode[]
---@field traversal_child integer
---Parses result from LSP into a reorganized tree of symbols (not flattened,
-- simply reoganized by merging each property table from the arguments into a
-- table for each symbol)
@@ -31,12 +14,14 @@ local M = {}
---@param depth number? The current depth of the symbol in the hierarchy.
---@param hierarchy table? A table of booleans which tells if a symbols parent was the last in its group.
---@param parent table? A reference to the current symbol's parent in the function's recursion
---@param bufnr integer The buffer number which the result was from
---@return outline.SymbolNode[]
local function parse_result(result, depth, hierarchy, parent)
local function parse_result(result, depth, hierarchy, parent, bufnr)
local ret = {}
for index, value in pairs(result) do
if not cfg.is_symbol_blacklisted(symbols.kinds[value.kind]) then
-- FIXME: If a parent was excluded, all children will not be considered
if cfg.should_include_symbol(symbols.kinds[value.kind], bufnr) then
-- the hierarchy is basically a table of booleans which
-- tells whether the parent was the last in its group or
-- not
@@ -74,7 +59,7 @@ local function parse_result(result, depth, hierarchy, parent)
-- copy by value because we dont want it messing with the hir table
local child_hir = t_utils.array_copy(hir)
table.insert(child_hir, isLast)
children = parse_result(value.children, level + 1, child_hir, node)
children = parse_result(value.children, level + 1, child_hir, node, bufnr)
else
value.children = {}
end
@@ -89,11 +74,12 @@ end
--'textDocument/documentSymbol', buf_request_all.
---Used when refreshing and setting up new symbols
---@param response table The result from buf_request_all
---@param bufnr integer
---@return outline.SymbolNode[]
function M.parse(response)
function M.parse(response, bufnr)
local sorted = lsp_utils.sort_symbols(response)
return parse_result(sorted, nil, nil)
return parse_result(sorted, nil, nil, nil, bufnr)
end
---Iterator that traverses the tree parent first before children, returning each node.