feat: Add folding
This commit is contained in:
@@ -5,11 +5,12 @@ local writer = require 'symbols-outline.writer'
|
||||
local config = require 'symbols-outline.config'
|
||||
local utils = require 'symbols-outline.utils.init'
|
||||
local View = require 'symbols-outline.view'
|
||||
local folding = require 'symbols-outline.folding'
|
||||
|
||||
local M = {}
|
||||
|
||||
local function setup_global_autocmd()
|
||||
if config.options.highlight_hovered_item then
|
||||
if config.options.highlight_hovered_item or config.options.auto_unfold_hover then
|
||||
vim.api.nvim_create_autocmd('CursorHold', {
|
||||
pattern = '*',
|
||||
callback = function()
|
||||
@@ -63,6 +64,15 @@ local function wipe_state()
|
||||
M.state = { outline_items = {}, flattened_outline_items = {}, code_win = 0 }
|
||||
end
|
||||
|
||||
local function _update_lines()
|
||||
M.state.flattened_outline_items = parser.flatten(M.state.outline_items)
|
||||
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
|
||||
end
|
||||
|
||||
local function _merge_items(items)
|
||||
utils.merge_items_rec({ children = items }, { children = M.state.outline_items })
|
||||
end
|
||||
|
||||
local function __refresh()
|
||||
if M.view:is_open() then
|
||||
local function refresh_handler(response)
|
||||
@@ -71,12 +81,11 @@ local function __refresh()
|
||||
end
|
||||
|
||||
local items = parser.parse(response)
|
||||
_merge_items(items)
|
||||
|
||||
M.state.code_win = vim.api.nvim_get_current_win()
|
||||
M.state.outline_items = items
|
||||
M.state.flattened_outline_items = parser.flatten(items)
|
||||
|
||||
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
|
||||
_update_lines()
|
||||
end
|
||||
|
||||
providers.request_symbols(refresh_handler)
|
||||
@@ -85,9 +94,13 @@ end
|
||||
|
||||
M._refresh = utils.debounce(__refresh, 100)
|
||||
|
||||
local function goto_location(change_focus)
|
||||
function M._current_node()
|
||||
local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1]
|
||||
local node = M.state.flattened_outline_items[current_line]
|
||||
return M.state.flattened_outline_items[current_line]
|
||||
end
|
||||
|
||||
local function goto_location(change_focus)
|
||||
local node = M._current_node()
|
||||
vim.api.nvim_win_set_cursor(
|
||||
M.state.code_win,
|
||||
{ node.line + 1, node.character }
|
||||
@@ -100,6 +113,53 @@ local function goto_location(change_focus)
|
||||
end
|
||||
end
|
||||
|
||||
function M._set_folded(folded, move_cursor, node_index)
|
||||
local node = M.state.flattened_outline_items[node_index] or M._current_node()
|
||||
local changed = (folded ~= folding.is_folded(node))
|
||||
|
||||
if folding.is_foldable(node) and changed then
|
||||
node.folded = folded
|
||||
|
||||
if move_cursor then
|
||||
vim.api.nvim_win_set_cursor(M.view.winnr, { node_index, 0 })
|
||||
end
|
||||
|
||||
_update_lines()
|
||||
elseif node.parent then
|
||||
for i, n in ipairs(M.state.flattened_outline_items) do
|
||||
if n == node.parent then
|
||||
M._set_folded(folded, not node.parent.folded and folded, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function M._set_all_folded(folded, nodes)
|
||||
local is_root_exec = not nodes
|
||||
nodes = nodes or M.state.outline_items
|
||||
|
||||
for _, node in ipairs(nodes) do
|
||||
node.folded = folded
|
||||
if node.children then
|
||||
M._set_all_folded(folded, node.children)
|
||||
end
|
||||
end
|
||||
|
||||
_update_lines()
|
||||
end
|
||||
|
||||
local function _items_dfs(callback, children)
|
||||
children = children or M.state.outline_items
|
||||
|
||||
for _, val in ipairs(children) do
|
||||
callback(val)
|
||||
|
||||
if val.children then
|
||||
_items_dfs(callback, val.children)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function M._highlight_current_item(winnr)
|
||||
local has_provider = providers.has_provider()
|
||||
|
||||
@@ -127,26 +187,28 @@ function M._highlight_current_item(winnr)
|
||||
|
||||
local hovered_line = vim.api.nvim_win_get_cursor(win)[1] - 1
|
||||
|
||||
local nodes = {}
|
||||
for index, value in ipairs(M.state.flattened_outline_items) do
|
||||
if
|
||||
value.line == hovered_line
|
||||
or (hovered_line > value.range_start and hovered_line < value.range_end)
|
||||
then
|
||||
value.line_in_outline = index
|
||||
table.insert(nodes, value)
|
||||
local leaf_node = nil
|
||||
|
||||
local cb = function(value)
|
||||
value.hovered = nil
|
||||
|
||||
if value.line == hovered_line or (hovered_line > value.range_start and hovered_line < value.range_end) then
|
||||
value.hovered = true
|
||||
leaf_node = value
|
||||
end
|
||||
end
|
||||
|
||||
-- clear old highlight
|
||||
ui.clear_hover_highlight(M.view.bufnr)
|
||||
for _, value in ipairs(nodes) do
|
||||
ui.add_hover_highlight(
|
||||
M.view.bufnr,
|
||||
value.line_in_outline - 1,
|
||||
value.depth * 2
|
||||
)
|
||||
vim.api.nvim_win_set_cursor(M.view.winnr, { value.line_in_outline, 1 })
|
||||
_items_dfs(cb)
|
||||
|
||||
_update_lines()
|
||||
|
||||
if leaf_node then
|
||||
for index, node in ipairs(M.state.flattened_outline_items) do
|
||||
if node == leaf_node then
|
||||
vim.api.nvim_win_set_cursor(M.view.winnr, { index, 1 })
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -188,6 +250,26 @@ local function setup_keymaps(bufnr)
|
||||
map(config.options.keymaps.close, function()
|
||||
M.view:close()
|
||||
end)
|
||||
-- fold selection
|
||||
map( config.options.keymaps.fold, function ()
|
||||
M._set_folded(true)
|
||||
end)
|
||||
-- unfold selection
|
||||
map(config.options.keymaps.unfold, function ()
|
||||
M._set_folded(false)
|
||||
end)
|
||||
-- fold all
|
||||
map(config.options.keymaps.fold_all, function ()
|
||||
M._set_all_folded(true)
|
||||
end)
|
||||
-- unfold all
|
||||
map(config.options.keymaps.unfold_all, function ()
|
||||
M._set_all_folded(false)
|
||||
end)
|
||||
-- fold reset
|
||||
map(config.options.keymaps.fold_reset,function ()
|
||||
M._set_all_folded(nil)
|
||||
end)
|
||||
end
|
||||
|
||||
local function handler(response)
|
||||
|
||||
Reference in New Issue
Block a user