Scope: - Parsing of symbol tree - Producing the flattened tree - Producing the lines shown in the outline based on the symbol tree - API of exported functions for parser.lua and writer.lua Note that the formatting of the outline remains the same as before. Fixes: - Guide highlights sometimes cover fold marker areas (may be related to the issue brought up by @silvercircle on reddit) - Guide highlights do not work when using guide markers of different widths than the default (such as setting all markers to ascii chars) All of these issues are now fixed after integrating the a parser algorithm. This commit introduces: 1. A better algorithm for flattening & parsing the tree in one go 2. `OutlineFoldMarker` highlight group 3. Fixed inconsistent highlighting of guides and legacy (somewhat weaker code), through (1). 4. Minor performance improvements 5. Type hints for the symbol tree 6. Removed several functions from writer.lua and parser.lua due to them being merged into writer.make_outline This can be seen as a breaking change because functions that were exported had altered behaviours. However I doubt these functions actually have any critical use outside of this plugin, hence it isn't really a breaking change as the user-experience remains the same. The extraneous left padding on the outline window is now a relic of the past 🎉 The old implementation, parser.get_lines used a flattened tree and was inefficient, full of off-by-one corrections. While trying to look for bug fixes in that function I realized it's the sort of "if it works, don't touch it" portion of code. Hence, I figured a complete rewrite is necessary. Now, the function responsible for making the outline lines lives at writer.make_outline. Building the flattened tree, getting lines, details and linenos are now done in one go. This is a tradeoff between modular design and efficiency. parser.lua still serve important purposes: - local parse_result function converts the hierarchical tables from provider into a nested form tree, used everywhere in outline.nvim. The type hint of the return value is now defined -- outline.SymbolNode - preorder_iter is an iterator that traverses the aforementioned tree in pre-order style. First the parents, all the childrens, and so on until the last node of the root tree. This is used in writer.make_outline to abstract a way the traversal code from the code of making the lines. Thanks to stack overflow I did not have to consult a DS book to figure out the cleanest way of this traversal method without using recursion. This, of course, closes #14 on github. Note that another minor 'breaking' change is that previously, hl for the guides where grouped per-character, now they are grouped together for all the guide markers in the same line. This should not be a problem for those who only style the fg color for guide hl. However, if you're styling the bg color, they will now take on that bg collectively rather than individually. This change eliminates future maintenance burden because controlling per-character guide highlights requires careful avoidance of off-by-one errors. I have tested most common features to work as before. I may have missed particular edge cases. Please take note of "scope" at the top of this commit message.
187 lines
4.2 KiB
Lua
187 lines
4.2 KiB
Lua
local config = require 'outline.config'
|
|
local tbl_utils = require 'outline.utils.table'
|
|
|
|
local M = {}
|
|
|
|
function M.is_buf_attached_to_lsp(bufnr)
|
|
local clients = vim.lsp.buf_get_clients(bufnr or 0)
|
|
return clients ~= nil and #clients > 0
|
|
end
|
|
|
|
function M.is_buf_markdown(bufnr)
|
|
return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown'
|
|
end
|
|
|
|
--- Merge all client token lists in an LSP response
|
|
function M.flatten_response(response)
|
|
local all_results = {}
|
|
|
|
-- flatten results to one giant table of symbols
|
|
for client_id, client_response in pairs(response) do
|
|
if config.is_client_blacklisted(client_id) then
|
|
print('skipping client ' .. client_id)
|
|
goto continue
|
|
end
|
|
|
|
local result = client_response['result']
|
|
if result == nil or type(result) ~= 'table' then
|
|
goto continue
|
|
end
|
|
|
|
for _, value in pairs(result) do
|
|
table.insert(all_results, value)
|
|
end
|
|
|
|
::continue::
|
|
end
|
|
|
|
return all_results
|
|
end
|
|
|
|
function M.get_selection_range(token)
|
|
-- support symbolinformation[]
|
|
-- https://microsoft.github.io/language-server-protocol/specification#textdocument_documentsymbol
|
|
if token.selectionRange == nil then
|
|
return token.location.range
|
|
end
|
|
|
|
return token.selectionRange
|
|
end
|
|
|
|
function M.get_range(token)
|
|
if token == nil then
|
|
return {
|
|
start={ line=math.huge, character=math.huge },
|
|
['end']={ line=-math.huge, character=-math.huge },
|
|
}
|
|
end
|
|
|
|
-- support symbolinformation[]
|
|
-- https://microsoft.github.io/language-server-protocol/specification#textdocument_documentsymbol
|
|
if token.range == nil then
|
|
return token.location.range
|
|
end
|
|
|
|
return token.range
|
|
end
|
|
|
|
--- lexicographically strict compare Ranges, line first
|
|
--- https://microsoft.github.io/language-server-protocol/specification/#range
|
|
local function range_compare(a, b)
|
|
if a == nil and b == nil then
|
|
return true
|
|
end
|
|
|
|
if a == nil then
|
|
return true
|
|
end
|
|
|
|
if b == nil then
|
|
return false
|
|
end
|
|
|
|
return (a.line < b.line) or (a.line == b.line and a.character < b.character)
|
|
end
|
|
|
|
---Sort the result from LSP by where the symbols start.
|
|
---Recursively sorts all children of each symbol
|
|
function M.sort_symbols(symbols)
|
|
table.sort(symbols, function(a, b)
|
|
return range_compare(
|
|
M.get_range(a).start,
|
|
M.get_range(b).start
|
|
)
|
|
end)
|
|
|
|
for _, child in ipairs(symbols) do
|
|
if child.children ~= nil then
|
|
M.sort_symbols(child.children)
|
|
end
|
|
end
|
|
|
|
return symbols
|
|
end
|
|
|
|
--- Preorder DFS iterator on the symbol tree
|
|
function M.symbol_preorder_iter(symbols)
|
|
local stk = {}
|
|
|
|
local function push_stk(symbols_list)
|
|
for i = #symbols_list, 1, -1 do
|
|
table.insert(stk, symbols_list[i])
|
|
end
|
|
end
|
|
|
|
push_stk(symbols)
|
|
|
|
local function is_empty()
|
|
return #stk == 0
|
|
end
|
|
|
|
local function next()
|
|
if not is_empty() then
|
|
local top = table.remove(stk)
|
|
|
|
push_stk(top and top.children or {})
|
|
|
|
return top
|
|
end
|
|
end
|
|
|
|
local function peek()
|
|
return stk[#stk]
|
|
end
|
|
|
|
return { next=next, is_empty=is_empty, peek=peek }
|
|
end
|
|
|
|
local function merge_symbols_rec(iter1, iter2, ub)
|
|
local res = {}
|
|
|
|
while not (iter1.is_empty() and iter2.is_empty()) do
|
|
local bv1 = ((not iter1.is_empty()) and M.get_range(iter1.peek()).start) or { line=math.huge, character=math.huge }
|
|
local bv2 = ((not iter2.is_empty()) and M.get_range(iter2.peek()).start) or { line=math.huge, character=math.huge }
|
|
|
|
local iter = (range_compare(bv1, bv2) and iter1) or iter2
|
|
|
|
if ub ~= nil and range_compare(ub, M.get_range(iter.peek()).start) then
|
|
break
|
|
end
|
|
|
|
local node = iter.next()
|
|
|
|
node.new_children = merge_symbols_rec(iter1, iter2, M.get_range(node)['end'])
|
|
|
|
table.insert(res, node)
|
|
end
|
|
|
|
return res
|
|
end
|
|
|
|
--- Merge symbols from two symbol trees
|
|
--- NOTE: Symbols are mutated!
|
|
function M.merge_symbols(symbols1, symbols2)
|
|
M.sort_symbols(symbols1)
|
|
M.sort_symbols(symbols2)
|
|
|
|
local iter1 = M.symbol_preorder_iter(symbols1)
|
|
local iter2 = M.symbol_preorder_iter(symbols2)
|
|
|
|
local symbols = merge_symbols_rec(iter1, iter2)
|
|
|
|
local function dfs(nodes)
|
|
for _, node in ipairs(nodes) do
|
|
dfs(node.new_children or {})
|
|
|
|
node.children = node.new_children
|
|
node.new_children = nil
|
|
end
|
|
end
|
|
|
|
dfs(symbols)
|
|
|
|
return symbols
|
|
end
|
|
|
|
return M
|