Refactor symbol types

This commit is contained in:
hedy
2023-11-26 16:49:25 +08:00
parent 5764294eb7
commit 5b70292780
10 changed files with 66 additions and 68 deletions

View File

@@ -1,7 +1,7 @@
local M = {} local M = {}
local cfg = require('outline.config') local cfg = require('outline.config')
---@param node outline.SymbolNode|outline.FlatSymbolNode ---@param node outline.Symbol|outline.FlatSymbol
function M.is_foldable(node) function M.is_foldable(node)
return node.children and #node.children > 0 return node.children and #node.children > 0
end end
@@ -16,7 +16,7 @@ local function get_default_folded(depth)
end end
end end
---@param node outline.SymbolNode|outline.FlatSymbolNode ---@param node outline.Symbol|outline.FlatSymbol
function M.is_folded(node) function M.is_folded(node)
local hover = cfg.o.symbol_folding.auto_unfold_hover local hover = cfg.o.symbol_folding.auto_unfold_hover
local only = cfg.o.symbol_folding.auto_unfold.only local only = cfg.o.symbol_folding.auto_unfold.only

View File

@@ -20,7 +20,7 @@ end
---Add single hover highlights ---Add single hover highlights
---@param bufnr integer ---@param bufnr integer
---@param nodes outline.FlatSymbolNode[] ---@param nodes outline.FlatSymbol[]
function M.hovers(bufnr, nodes) function M.hovers(bufnr, nodes)
for line, node in ipairs(nodes) do for line, node in ipairs(nodes) do
if node.hovered then if node.hovered then

View File

@@ -9,12 +9,12 @@ local M = {}
---Parses result from LSP into a reorganized tree of symbols (not flattened, ---Parses result from LSP into a reorganized tree of symbols (not flattened,
-- simply reoganized by merging each property table from the arguments into a -- simply reoganized by merging each property table from the arguments into a
-- table for each symbol) -- table for each symbol)
---@param result table The result from a language server. ---@param result outline.ProviderSymbol The result from a language server.
---@param depth number? The current depth of the symbol in the hierarchy. ---@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 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 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 ---@param bufnr integer The buffer number which the result was from
---@return outline.SymbolNode[] ---@return outline.Symbol[]
local function parse_result(result, depth, hierarchy, parent, bufnr) local function parse_result(result, depth, hierarchy, parent, bufnr)
local ret = {} local ret = {}
@@ -48,7 +48,7 @@ local function parse_result(result, depth, hierarchy, parent, bufnr)
isLast = isLast, isLast = isLast,
hierarchy = hir, hierarchy = hir,
parent = parent, parent = parent,
traversal_child = 1, _i = 1,
} }
table.insert(ret, node) table.insert(ret, node)
@@ -74,7 +74,7 @@ end
---Used when refreshing and setting up new symbols ---Used when refreshing and setting up new symbols
---@param response table The result from buf_request_all ---@param response table The result from buf_request_all
---@param bufnr integer ---@param bufnr integer
---@return outline.SymbolNode[] ---@return outline.Symbol[]
function M.parse(response, bufnr) function M.parse(response, bufnr)
local sorted = lsp_utils.sort_symbols(response) local sorted = lsp_utils.sort_symbols(response)
@@ -83,11 +83,11 @@ end
---Iterator that traverses the tree parent first before children, returning each node. ---Iterator that traverses the tree parent first before children, returning each node.
-- Essentailly 'flatten' items, but returns an iterator. -- Essentailly 'flatten' items, but returns an iterator.
---@param items outline.SymbolNode[] Tree of symbols parsed by parse_result ---@param items outline.Symbol[] Tree of symbols parsed by parse_result
---@param children_check function? Takes a node and return whether the children should be explored. ---@param children_check function? Takes a node and return whether the children should be explored.
---Note that the root node (param items) is always explored regardless of children_check. ---Note that the root node (param items) is always explored regardless of children_check.
function M.preorder_iter(items, children_check) function M.preorder_iter(items, children_check)
local node = { children = items, traversal_child = 1, depth = 1, is_root = true } local node = { children = items, _i = 1, depth = 1, is_root = true }
local prev local prev
local visited = {} local visited = {}
@@ -104,19 +104,15 @@ function M.preorder_iter(items, children_check)
return node return node
end end
if if node.children and node._i <= #node.children and (node.is_root or children_check(node)) then
node.children
and node.traversal_child <= #node.children
and (node.is_root or children_check(node))
then
prev = node prev = node
if node.children[node.traversal_child] then if node.children[node._i] then
node.children[node.traversal_child].parent_node = node node.children[node._i].parent_node = node
node = node.children[node.traversal_child] node = node.children[node._i]
end end
prev.traversal_child = prev.traversal_child + 1 prev._i = prev._i + 1
else else
node.traversal_child = 1 node._i = 1
node = node.parent_node node = node.parent_node
end end
end end

View File

@@ -1,5 +1,3 @@
local cfg = require('outline.config')
local M = {} local M = {}
local import_prefix = 'outline/providers/' local import_prefix = 'outline/providers/'
@@ -8,7 +6,7 @@ function M.find_provider()
if not M.providers then if not M.providers then
M.providers = vim.tbl_map(function(p) M.providers = vim.tbl_map(function(p)
return import_prefix .. p return import_prefix .. p
end, cfg.get_providers()) end, require('outline.config').get_providers())
end end
for _, path in ipairs(M.providers) do for _, path in ipairs(M.providers) do
local provider = require(path) local provider = require(path)

View File

@@ -15,7 +15,6 @@ local M = {
name = 'markdown', name = 'markdown',
} }
---@return boolean ft_is_markdown ---@return boolean ft_is_markdown
function M.supports_buffer(bufnr) function M.supports_buffer(bufnr)
return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown' return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown'
@@ -32,7 +31,7 @@ end
-- Parses markdown files and returns a table of SymbolInformation[] which is -- Parses markdown files and returns a table of SymbolInformation[] which is
-- used by the plugin to show the outline. -- used by the plugin to show the outline.
---@return table ---@return outline.ProviderSymbol[]
function M.handle_markdown() function M.handle_markdown()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local level_symbols = { { children = {} } } local level_symbols = { { children = {} } }
@@ -119,7 +118,7 @@ function M.handle_markdown()
return level_symbols[1].children return level_symbols[1].children
end end
---@param on_symbols function ---@param on_symbols fun(symbols?:outline.ProviderSymbol[], opts?:table)
---@param opts table ---@param opts table
function M.request_symbols(on_symbols, opts) function M.request_symbols(on_symbols, opts)
on_symbols(M.handle_markdown(), opts) on_symbols(M.handle_markdown(), opts)

View File

@@ -53,6 +53,8 @@ if not _G._outline_nvim_has[8] then
end end
end end
---@param node outline.ProviderSymbol
---@param field string
local function rec_remove_field(node, field) local function rec_remove_field(node, field)
node[field] = nil node[field] = nil
if node.children then if node.children then
@@ -62,6 +64,8 @@ local function rec_remove_field(node, field)
end end
end end
---@param callback fun(symbols?:outline.ProviderSymbol[], opts?:table)
---@param opts table
function M.request_symbols(callback, opts) function M.request_symbols(callback, opts)
if not M.parser then if not M.parser then
local status, parser = pcall(vim.treesitter.get_parser, 0, 'norg') local status, parser = pcall(vim.treesitter.get_parser, 0, 'norg')

View File

@@ -15,10 +15,6 @@ function M.get_status()
return { 'client: ' .. M.client.name } return { 'client: ' .. M.client.name }
end end
local function get_params()
return { textDocument = vim.lsp.util.make_text_document_params() }
end
function M.hover_info(bufnr, params, on_info) function M.hover_info(bufnr, params, on_info)
local clients = vim.lsp.get_active_clients({ bufnr = bufnr }) local clients = vim.lsp.get_active_clients({ bufnr = bufnr })
local use_client local use_client
@@ -49,6 +45,7 @@ function M.hover_info(bufnr, params, on_info)
use_client.request('textDocument/hover', params, on_info, bufnr) use_client.request('textDocument/hover', params, on_info, bufnr)
end end
---@return boolean
function M.supports_buffer(bufnr) function M.supports_buffer(bufnr)
local clients = vim.lsp.get_active_clients({ bufnr = bufnr }) local clients = vim.lsp.get_active_clients({ bufnr = bufnr })
local ret = false local ret = false
@@ -69,6 +66,8 @@ function M.supports_buffer(bufnr)
return ret return ret
end end
---@param response outline.ProviderSymbol[]
---@return outline.ProviderSymbol[]
local function postprocess_symbols(response) local function postprocess_symbols(response)
local symbols = lsp_utils.flatten_response(response) local symbols = lsp_utils.flatten_response(response)
@@ -81,9 +80,13 @@ local function postprocess_symbols(response)
end end
end end
---@param on_symbols function ---@param on_symbols fun(symbols?:outline.ProviderSymbol[], opts?:table)
---@param opts table
function M.request_symbols(on_symbols, opts) function M.request_symbols(on_symbols, opts)
vim.lsp.buf_request_all(0, 'textDocument/documentSymbol', get_params(), function(response) local params = {
textDocument = vim.lsp.util.make_text_document_params(),
}
vim.lsp.buf_request_all(0, 'textDocument/documentSymbol', params, function(response)
response = postprocess_symbols(response) response = postprocess_symbols(response)
on_symbols(response, opts) on_symbols(response, opts)
end) end)

View File

@@ -17,9 +17,9 @@ local Sidebar = {}
---@class outline.Sidebar ---@class outline.Sidebar
---@field view outline.View ---@field view outline.View
---@field items outline.SymbolNode[] ---@field items outline.Symbol[]
---@field flats outline.FlatSymbolNode[] ---@field flats outline.FlatSymbol[]
---@field hovered outline.FlatSymbolNode[] ---@field hovered outline.FlatSymbol[]
---@field original_cursor string ---@field original_cursor string
---@field code outline.SidebarCodeState ---@field code outline.SidebarCodeState
---@field autocmds { [integer]: integer } winnr to autocmd id ---@field autocmds { [integer]: integer } winnr to autocmd id
@@ -68,7 +68,7 @@ function Sidebar:destroy()
end end
---Handler for provider request_symbols when outline is opened for the first time. ---Handler for provider request_symbols when outline is opened for the first time.
---@param response table? ---@param response outline.ProviderSymbol[]?
---@param opts outline.OutlineOpts? ---@param opts outline.OutlineOpts?
function Sidebar:initial_handler(response, opts) function Sidebar:initial_handler(response, opts)
if response == nil or type(response) ~= 'table' or self.view:is_open() then if response == nil or type(response) ~= 'table' or self.view:is_open() then
@@ -110,7 +110,7 @@ end
---Convenience function for setup_keymaps ---Convenience function for setup_keymaps
---@param cfg_name string Field in cfg.o.keymaps ---@param cfg_name string Field in cfg.o.keymaps
---@param method string|function If string, field in Sidebar ---@param method string|function If string, field in Sidebar
---@param args table Passed to method ---@param args any[] Passed to method
function Sidebar:nmap(cfg_name, method, args) function Sidebar:nmap(cfg_name, method, args)
local keys = cfg.o.keymaps[cfg_name] local keys = cfg.o.keymaps[cfg_name]
local fn local fn
@@ -255,7 +255,7 @@ function Sidebar:reset_cursor_style()
end end
---Set the cursor to current.line_in_outline and column to a convenient place ---Set the cursor to current.line_in_outline and column to a convenient place
---@param current outline.FlatSymbolNode? ---@param current outline.FlatSymbol?
function Sidebar:update_cursor_pos(current) function Sidebar:update_cursor_pos(current)
local col = 0 local col = 0
local buf = vim.api.nvim_win_get_buf(self.code.win) local buf = vim.api.nvim_win_get_buf(self.code.win)
@@ -271,7 +271,7 @@ end
---Calls build_outline and then calls update_cursor_pos if update_cursor is ---Calls build_outline and then calls update_cursor_pos if update_cursor is
---not false ---not false
---@param update_cursor boolean? ---@param update_cursor boolean?
---@param set_cursor_to_node outline.SymbolNode|outline.FlatSymbolNode? ---@param set_cursor_to_node outline.Symbol|outline.FlatSymbol?
function Sidebar:_update_lines(update_cursor, set_cursor_to_node) function Sidebar:_update_lines(update_cursor, set_cursor_to_node)
local current = self:build_outline(set_cursor_to_node) local current = self:build_outline(set_cursor_to_node)
if update_cursor ~= false then if update_cursor ~= false then
@@ -280,6 +280,7 @@ function Sidebar:_update_lines(update_cursor, set_cursor_to_node)
end end
---Handler for provider request_symbols for refreshing outline ---Handler for provider request_symbols for refreshing outline
---@param response outline.ProviderSymbol[]
function Sidebar:refresh_handler(response) function Sidebar:refresh_handler(response)
if response == nil or type(response) ~= 'table' then if response == nil or type(response) ~= 'table' then
return return
@@ -308,7 +309,7 @@ function Sidebar:refresh_handler(response)
self:_update_lines(update_cursor) self:_update_lines(update_cursor)
end end
---@param items outline.SymbolNode[] ---@param items outline.Symbol[]
function Sidebar:_merge_items(items) function Sidebar:_merge_items(items)
parser.merge_items_rec({ children = items }, { children = self.items }) parser.merge_items_rec({ children = items }, { children = self.items })
end end
@@ -335,7 +336,7 @@ end
-- stylua: ignore end -- stylua: ignore end
---Currently hovered node in outline ---Currently hovered node in outline
---@return outline.FlatSymbolNode ---@return outline.FlatSymbol
function Sidebar:_current_node() function Sidebar:_current_node()
local current_line = vim.api.nvim_win_get_cursor(self.view.win)[1] local current_line = vim.api.nvim_win_get_cursor(self.view.win)[1]
return self.flats[current_line] return self.flats[current_line]
@@ -432,7 +433,7 @@ function Sidebar:_set_folded(folded, move_cursor, node_index)
end end
end end
---@param nodes outline.SymbolNode[] ---@param nodes outline.Symbol[]
function Sidebar:_toggle_all_fold(nodes) function Sidebar:_toggle_all_fold(nodes)
nodes = nodes or self.items nodes = nodes or self.items
local folded = true local folded = true
@@ -448,7 +449,7 @@ function Sidebar:_toggle_all_fold(nodes)
end end
---@param folded boolean? ---@param folded boolean?
---@param nodes? outline.SymbolNode[] ---@param nodes? outline.Symbol[]
function Sidebar:_set_all_folded(folded, nodes) function Sidebar:_set_all_folded(folded, nodes)
local stack = { nodes or self.items } local stack = { nodes or self.items }
local current = self:_current_node() local current = self:_current_node()
@@ -639,12 +640,12 @@ end
---@note Ensure new outlines are already set to `self.items` before calling ---@note Ensure new outlines are already set to `self.items` before calling
---this function. `self.flats` will be overwritten and current line is obtained ---this function. `self.flats` will be overwritten and current line is obtained
---from `win_get_cursor` using `self.code.win`. ---from `win_get_cursor` using `self.code.win`.
---@param find_node outline.FlatSymbolNode|outline.SymbolNode? Find a given node rather than node matching cursor position in codewin ---@param find_node outline.FlatSymbol|outline.Symbol? Find a given node rather than node matching cursor position in codewin
---@return outline.FlatSymbolNode? set_cursor_to_this_node ---@return outline.FlatSymbol? set_cursor_to_this_node
function Sidebar:build_outline(find_node) function Sidebar:build_outline(find_node)
---@type integer 0-indexed ---@type integer 0-indexed
local hovered_line = vim.api.nvim_win_get_cursor(self.code.win)[1] - 1 local hovered_line = vim.api.nvim_win_get_cursor(self.code.win)[1] - 1
---@type outline.FlatSymbolNode Deepest visible matching node to set cursor ---@type outline.FlatSymbol Deepest visible matching node to set cursor
local put_cursor local put_cursor
self.flats = {} self.flats = {}
local line_count = 0 local line_count = 0

View File

@@ -22,28 +22,23 @@
-- SYMBOLS -- SYMBOLS
---@class outline.SymbolNode ---@class outline.ProviderSymbol
---@field name string ---@field name string
---@field depth integer ---@field kind integer
---@field parent outline.SymbolNode ---@field detail? string
---@field deprecated boolean ---@field range outline.ProviderSymbolRange
---@field kind integer|string ---@field selectionRange outline.ProviderSymbolRange
---@field icon string ---@field parent outline.ProviderSymbol
---@field detail string ---@field children outline.ProviderSymbol[]
---@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 Should NOT be modified during iteration using parser.preorder_iter
---@field is_root boolean?
---@class outline.FlatSymbolNode ---@class outline.ProviderSymbolRange
---@field start integer
---@field end integer
---@class outline.Symbol
---@field name string ---@field name string
---@field depth integer ---@field depth integer
---@field parent outline.FlatSymbolNode ---@field parent outline.Symbol
---@field deprecated boolean ---@field deprecated boolean
---@field kind integer|string ---@field kind integer|string
---@field icon string ---@field icon string
@@ -54,9 +49,11 @@
---@field range_end integer ---@field range_end integer
---@field isLast boolean ---@field isLast boolean
---@field hierarchy boolean ---@field hierarchy boolean
---@field children? outline.FlatSymbolNode[] ---@field children? outline.Symbol[]
---@field traversal_child integer ---@field _i integer Should NOT be modified during iteration using parser.preorder_iter
---@field is_root boolean? ---@field is_root? boolean
---@class outline.FlatSymbol : outline.Symbol
---@field line_in_outline integer ---@field line_in_outline integer
---@field prefix_length integer ---@field prefix_length integer
---@field hovered boolean ---@field hovered boolean
@@ -68,7 +65,7 @@
---@field name string ---@field name string
---@field get_status? fun():string[] ---@field get_status? fun():string[]
---@field supports_buffer fun(bufnr:integer):boolean ---@field supports_buffer fun(bufnr:integer):boolean
---@field request_symbols fun(on_symbols:function, opts:table?) ---@field request_symbols fun(on_symbols:fun(symbols?:outline.ProviderSymbol[], opts:table?), opts:table?)
---@field hover_info? fun(bufnr:integer, params:table, on_info:function) ---@field hover_info? fun(bufnr:integer, params:table, on_info:function)
---@field rename_symbol? fun(sidebar:outline.Sidebar) ---@field rename_symbol? fun(sidebar:outline.Sidebar)
---@field code_actions? fun(sidebar:outline.Sidebar) ---@field code_actions? fun(sidebar:outline.Sidebar)

View File

@@ -98,7 +98,7 @@ end
---Ensure all existing highlights are already cleared before calling! ---Ensure all existing highlights are already cleared before calling!
---@param hl outline.HL[] ---@param hl outline.HL[]
---@param nodes outline.FlatSymbolNode[] ---@param nodes outline.FlatSymbol[]
---@param details string[] ---@param details string[]
---@param linenos string[] ---@param linenos string[]
function View:add_hl_and_ns(hl, nodes, details, linenos) function View:add_hl_and_ns(hl, nodes, details, linenos)