feat: Add folding

This commit is contained in:
charburgx
2022-08-17 16:14:16 -05:00
parent 7d4050f716
commit ced4e476cf
6 changed files with 281 additions and 54 deletions

View File

@@ -2,6 +2,7 @@ local symbols = require 'symbols-outline.symbols'
local ui = require 'symbols-outline.ui'
local config = require 'symbols-outline.config'
local t_utils = require 'symbols-outline.utils.table'
local folding = require 'symbols-outline.folding'
local M = {}
@@ -9,8 +10,9 @@ local M = {}
---@param result table The result from a language server.
---@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
---@return table
local function parse_result(result, depth, hierarchy)
local function parse_result(result, depth, hierarchy, parent)
local ret = {}
for index, value in pairs(result) do
@@ -24,14 +26,6 @@ local function parse_result(result, depth, hierarchy)
-- whether this node is the last in its group
local isLast = index == #result
local children = nil
if value.children ~= nil then
-- 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)
end
-- support SymbolInformation[]
-- https://microsoft.github.io/language-server-protocol/specification#textDocument_documentSymbol
local selectionRange = value.selectionRange
@@ -44,7 +38,7 @@ local function parse_result(result, depth, hierarchy)
range = value.location.range
end
table.insert(ret, {
local node = {
deprecated = value.deprecated,
kind = value.kind,
icon = symbols.icon_from_kind(value.kind),
@@ -54,11 +48,24 @@ local function parse_result(result, depth, hierarchy)
character = selectionRange.start.character,
range_start = range.start.line,
range_end = range['end'].line,
children = children,
depth = level,
isLast = isLast,
hierarchy = hir,
})
parent = parent,
}
table.insert(ret, node)
local children = nil
if value.children ~= nil then
-- 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)
end
node.children = children
end
end
return ret
@@ -139,14 +146,23 @@ function M.parse(response)
return parse_result(sorted, nil, nil)
end
function M.flatten(outline_items, ret)
function M.flatten(outline_items, ret, depth)
depth = depth or 1
ret = ret or {}
for _, value in ipairs(outline_items) do
table.insert(ret, value)
if value.children ~= nil then
M.flatten(value.children, ret)
value.line_in_outline = #ret
if value.children ~= nil and not folding.is_folded(value) then
M.flatten(value.children, ret, depth + 1)
end
end
-- if depth == 1 then
-- for index, value in ipairs(ret) do
-- value.line_in_outline = index
-- end
-- end
return ret
end
@@ -155,7 +171,10 @@ function M.get_lines(flattened_outline_items)
local hl_info = {}
for node_line, node in ipairs(flattened_outline_items) do
local line = t_utils.str_to_table(string.rep(' ', node.depth))
local depth = node.depth
local marker_space = (config.options.fold_markers and 1) or 0
local line = t_utils.str_to_table(string.rep(' ', depth + marker_space))
local running_length = 1
local function add_guide_hl(from, to)
@@ -176,27 +195,43 @@ function M.get_lines(flattened_outline_items)
-- i f index is last, add a bottom marker if current item is last,
-- else add a middle marker
elseif index == #line then
if node.isLast then
line[index] = ui.markers.bottom
-- add fold markers
if config.options.fold_markers and folding.is_foldable(node) then
if folding.is_folded(node) then
line[index] = config.options.fold_markers[1]
else
line[index] = config.options.fold_markers[2]
end
add_guide_hl(
running_length,
running_length + vim.fn.strlen(ui.markers.bottom) - 1
)
else
line[index] = ui.markers.middle
add_guide_hl(
running_length,
running_length + vim.fn.strlen(ui.markers.middle) - 1
running_length + vim.fn.strlen(line[index]) - 1
)
-- the root level has no vertical markers
elseif depth > 1 then
if node.isLast then
line[index] = ui.markers.bottom
add_guide_hl(
running_length,
running_length + vim.fn.strlen(ui.markers.bottom) - 1
)
else
line[index] = ui.markers.middle
add_guide_hl(
running_length,
running_length + vim.fn.strlen(ui.markers.middle) - 1
)
end
end
-- else if the parent was not the last in its group, add a
-- vertical marker because there are items under us and we need
-- to point to those
elseif not node.hierarchy[index] then
line[index] = ui.markers.vertical
elseif not node.hierarchy[index] and depth > 1 then
line[index + marker_space] = ui.markers.vertical
add_guide_hl(
running_length,
running_length + vim.fn.strlen(ui.markers.vertical) - 1
running_length - 1 + 2*marker_space,
running_length + vim.fn.strlen(ui.markers.vertical) - 1 + 2 * marker_space
)
end
end