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

@@ -1,5 +1,6 @@
local parser = require 'symbols-outline.parser'
local config = require 'symbols-outline.config'
local ui = require 'symbols-outline.ui'
local M = {}
@@ -21,7 +22,7 @@ function M.write_outline(bufnr, lines)
vim.api.nvim_buf_set_option(bufnr, 'modifiable', false)
end
function M.add_highlights(bufnr, hl_info)
function M.add_highlights(bufnr, hl_info, nodes)
for _, line_hl in ipairs(hl_info) do
local line, hl_start, hl_end, hl_type = unpack(line_hl)
vim.api.nvim_buf_add_highlight(
@@ -33,6 +34,8 @@ function M.add_highlights(bufnr, hl_info)
hl_end
)
end
M.add_hover_highlights(bufnr, nodes)
end
local ns = vim.api.nvim_create_namespace 'symbols-outline-virt-text'
@@ -58,6 +61,24 @@ local function clear_virt_text(bufnr)
vim.api.nvim_buf_clear_namespace(bufnr, -1, 0, -1)
end
M.add_hover_highlights = function(bufnr, nodes)
if not config.options.highlight_hovered_item then
return
end
-- clear old highlight
ui.clear_hover_highlight(bufnr)
for _, node in ipairs(nodes) do
if not node.hovered then
goto continue
end
local marker_fac = (config.options.fold_markers and 1) or 0
ui.add_hover_highlight(bufnr, node.line_in_outline - 1, (node.depth + marker_fac) * 2)
::continue::
end
end
-- runs the whole writing routine where the text is cleared, new data is parsed
-- and then written
function M.parse_and_write(bufnr, flattened_outline_items)
@@ -66,7 +87,7 @@ function M.parse_and_write(bufnr, flattened_outline_items)
clear_virt_text(bufnr)
local details = parser.get_details(flattened_outline_items)
M.add_highlights(bufnr, hl_info)
M.add_highlights(bufnr, hl_info, flattened_outline_items)
M.write_details(bufnr, details)
end