MAJOR: Project rename and preparation for v1.0.0
I hope I haven't missed any for the renames!
This commit is contained in:
615
lua/outline/init.lua
Normal file
615
lua/outline/init.lua
Normal file
@@ -0,0 +1,615 @@
|
||||
local parser = require 'outline.parser'
|
||||
local providers = require 'outline.providers.init'
|
||||
local ui = require 'outline.ui'
|
||||
local writer = require 'outline.writer'
|
||||
local cfg = require 'outline.config'
|
||||
local utils = require 'outline.utils.init'
|
||||
local View = require 'outline.view'
|
||||
local folding = require 'outline.folding'
|
||||
|
||||
local M = {}
|
||||
|
||||
local function setup_global_autocmd()
|
||||
if
|
||||
cfg.o.outline_items.highlight_hovered_item or cfg.o.symbol_folding.auto_unfold_hover
|
||||
then
|
||||
vim.api.nvim_create_autocmd('CursorHold', {
|
||||
pattern = '*',
|
||||
callback = function()
|
||||
M._highlight_current_item(nil)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd({
|
||||
'InsertLeave',
|
||||
'WinEnter',
|
||||
'BufEnter',
|
||||
'BufWinEnter',
|
||||
'TabEnter',
|
||||
'BufWritePost',
|
||||
}, {
|
||||
pattern = '*',
|
||||
callback = M._refresh,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd('WinEnter', {
|
||||
pattern = '*',
|
||||
callback = require('outline.preview').close,
|
||||
})
|
||||
end
|
||||
|
||||
-------------------------
|
||||
-- STATE
|
||||
-------------------------
|
||||
M.state = {
|
||||
outline_items = {},
|
||||
flattened_outline_items = {},
|
||||
code_win = 0,
|
||||
-- In case unhide_cursor was called before hide_cursor for _some_ reason,
|
||||
-- this can still be used as a fallback
|
||||
original_cursor = vim.o.guicursor,
|
||||
}
|
||||
|
||||
local function wipe_state()
|
||||
M.state = { outline_items = {}, flattened_outline_items = {}, code_win = 0, opts = {} }
|
||||
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()
|
||||
local current_buffer_is_outline = M.view.bufnr
|
||||
== vim.api.nvim_get_current_buf()
|
||||
if M.view:is_open() and not current_buffer_is_outline then
|
||||
local function refresh_handler(response)
|
||||
if response == nil or type(response) ~= 'table' then
|
||||
return
|
||||
end
|
||||
|
||||
local items = parser.parse(response)
|
||||
_merge_items(items)
|
||||
|
||||
M.state.code_win = vim.api.nvim_get_current_win()
|
||||
|
||||
_update_lines()
|
||||
end
|
||||
|
||||
providers.request_symbols(refresh_handler)
|
||||
end
|
||||
end
|
||||
|
||||
M._refresh = utils.debounce(__refresh, 100)
|
||||
|
||||
function M._current_node()
|
||||
local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1]
|
||||
return M.state.flattened_outline_items[current_line]
|
||||
end
|
||||
|
||||
function M.__goto_location(change_focus)
|
||||
local node = M._current_node()
|
||||
vim.api.nvim_win_set_cursor(
|
||||
M.state.code_win,
|
||||
{ node.line + 1, node.character }
|
||||
)
|
||||
utils.flash_highlight(M.state.code_win, node.line + 1, true)
|
||||
if change_focus then
|
||||
vim.fn.win_gotoid(M.state.code_win)
|
||||
end
|
||||
end
|
||||
|
||||
-- Wraps __goto_location and handles auto_close
|
||||
function M._goto_location(change_focus)
|
||||
M.__goto_location(change_focus)
|
||||
if change_focus and cfg.o.outline_window.auto_close then
|
||||
M.close_outline()
|
||||
end
|
||||
end
|
||||
|
||||
function M._goto_and_close()
|
||||
M.__goto_location(true)
|
||||
M.close_outline()
|
||||
end
|
||||
|
||||
function M._move_and_goto(direction)
|
||||
local move = direction == 'down' and 1 or -1
|
||||
local cur = vim.api.nvim_win_get_cursor(0)
|
||||
cur[1] = cur[1] + move
|
||||
pcall(vim.api.nvim_win_set_cursor, 0, cur)
|
||||
M.__goto_location(false)
|
||||
end
|
||||
|
||||
function M._toggle_fold(move_cursor, node_index)
|
||||
local node = M.state.flattened_outline_items[node_index] or M._current_node()
|
||||
local is_folded = folding.is_folded(node)
|
||||
|
||||
if folding.is_foldable(node) then
|
||||
M._set_folded(not is_folded, move_cursor, node_index)
|
||||
end
|
||||
end
|
||||
|
||||
local function hide_cursor()
|
||||
-- Set cursor color to CursorLine in normal mode
|
||||
M.state.original_cursor = vim.o.guicursor
|
||||
local cur = vim.o.guicursor:match("n.-:(.-)[-,]")
|
||||
vim.opt.guicursor:append("n:"..cur.."-Cursorline")
|
||||
end
|
||||
|
||||
local function unhide_cursor()
|
||||
-- vim.opt doesn't seem to provide a way to remove last item, like a pop()
|
||||
-- vim.o.guicursor = vim.o.guicursor:gsub(",n.-:.-$", "")
|
||||
vim.o.guicursor = M.state.original_cursor
|
||||
end
|
||||
|
||||
local function setup_buffer_autocmd()
|
||||
if cfg.o.preview_window.auto_preview then
|
||||
vim.api.nvim_create_autocmd('CursorMoved', {
|
||||
buffer = 0,
|
||||
callback = require('outline.preview').show,
|
||||
})
|
||||
else
|
||||
vim.api.nvim_create_autocmd('CursorMoved', {
|
||||
buffer = 0,
|
||||
callback = require('outline.preview').close,
|
||||
})
|
||||
end
|
||||
if cfg.o.outline_window.auto_goto then
|
||||
vim.api.nvim_create_autocmd('CursorMoved', {
|
||||
buffer = 0,
|
||||
callback = function()
|
||||
-- Don't use _goto_location because we don't want to auto-close
|
||||
M.__goto_location(false)
|
||||
end
|
||||
})
|
||||
end
|
||||
if cfg.o.outline_window.hide_cursor then
|
||||
-- Unfortunately guicursor is a global option, so we have to make sure to
|
||||
-- set and unset when cursor leaves the outline window.
|
||||
hide_cursor()
|
||||
vim.api.nvim_create_autocmd('BufEnter', {
|
||||
buffer = 0,
|
||||
callback = hide_cursor
|
||||
})
|
||||
vim.api.nvim_create_autocmd('BufLeave', {
|
||||
buffer = 0,
|
||||
callback = unhide_cursor
|
||||
})
|
||||
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
|
||||
local parent_node =
|
||||
M.state.flattened_outline_items[node.parent.line_in_outline]
|
||||
|
||||
if parent_node then
|
||||
M._set_folded(
|
||||
folded,
|
||||
not parent_node.folded and folded,
|
||||
parent_node.line_in_outline
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function M._toggle_all_fold(nodes)
|
||||
nodes = nodes or M.state.outline_items
|
||||
local folded = true
|
||||
|
||||
for _, node in ipairs(nodes) do
|
||||
if folding.is_foldable(node) and not folding.is_folded(node) then
|
||||
folded = false
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
M._set_all_folded(not folded, nodes)
|
||||
end
|
||||
|
||||
function M._set_all_folded(folded, nodes)
|
||||
local stack = { nodes or M.state.outline_items }
|
||||
|
||||
while #stack > 0 do
|
||||
local current_nodes = table.remove(stack, #stack)
|
||||
for _, node in ipairs(current_nodes) do
|
||||
node.folded = folded
|
||||
if node.children then
|
||||
stack[#stack + 1] = node.children
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
_update_lines()
|
||||
end
|
||||
|
||||
function M._highlight_current_item(winnr)
|
||||
local has_provider = M.has_provider()
|
||||
local has_outline_open = M.view:is_open()
|
||||
local current_buffer_is_outline = M.view.bufnr
|
||||
== vim.api.nvim_get_current_buf()
|
||||
|
||||
if not has_provider then
|
||||
return
|
||||
end
|
||||
|
||||
if current_buffer_is_outline and not winnr then
|
||||
-- Don't update cursor pos and content if they are navigating the outline.
|
||||
-- Winnr may be given when user explicitly wants to restore location
|
||||
-- (follow_cursor), or through the open handler.
|
||||
return
|
||||
end
|
||||
|
||||
if not has_outline_open and not winnr then
|
||||
-- Outline not open and no code window given
|
||||
return
|
||||
end
|
||||
|
||||
local win = winnr or vim.api.nvim_get_current_win()
|
||||
local hovered_line = vim.api.nvim_win_get_cursor(win)[1] - 1
|
||||
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
|
||||
|
||||
utils.items_dfs(cb, M.state.outline_items)
|
||||
|
||||
_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
|
||||
|
||||
local function setup_keymaps(bufnr)
|
||||
local map = function(...)
|
||||
utils.nmap(bufnr, ...)
|
||||
end
|
||||
-- goto_location of symbol and focus that window
|
||||
map(cfg.o.keymaps.goto_location, function()
|
||||
M._goto_location(true)
|
||||
end)
|
||||
-- goto_location of symbol but stay in outline
|
||||
map(cfg.o.keymaps.peek_location, function()
|
||||
M._goto_location(false)
|
||||
end)
|
||||
-- Navigate to corresponding outline location for current code location
|
||||
map(cfg.o.keymaps.restore_location, M._map_follow_cursor)
|
||||
-- Navigate to corresponding outline location for current code location
|
||||
map(cfg.o.keymaps.goto_and_close, M._goto_and_close)
|
||||
-- Move down/up in outline and peek that location in code
|
||||
map(cfg.o.keymaps.down_and_goto, function()
|
||||
M._move_and_goto('down')
|
||||
end)
|
||||
-- Move down/up in outline and peek that location in code
|
||||
map(cfg.o.keymaps.up_and_goto, function()
|
||||
M._move_and_goto('up')
|
||||
end)
|
||||
-- hover symbol
|
||||
map(
|
||||
cfg.o.keymaps.hover_symbol,
|
||||
require('outline.hover').show_hover
|
||||
)
|
||||
-- preview symbol
|
||||
map(
|
||||
cfg.o.keymaps.toggle_preview,
|
||||
require('outline.preview').toggle
|
||||
)
|
||||
-- rename symbol
|
||||
map(
|
||||
cfg.o.keymaps.rename_symbol,
|
||||
require('outline.rename').rename
|
||||
)
|
||||
-- code actions
|
||||
map(
|
||||
cfg.o.keymaps.code_actions,
|
||||
require('outline.code_action').show_code_actions
|
||||
)
|
||||
-- show help
|
||||
map(
|
||||
cfg.o.keymaps.show_help,
|
||||
require('outline.config').show_help
|
||||
)
|
||||
-- close outline
|
||||
map(cfg.o.keymaps.close, function()
|
||||
M.view:close()
|
||||
end)
|
||||
-- toggle fold selection
|
||||
map(cfg.o.keymaps.fold_toggle, M._toggle_fold)
|
||||
-- fold selection
|
||||
map(cfg.o.keymaps.fold, function()
|
||||
M._set_folded(true)
|
||||
end)
|
||||
-- unfold selection
|
||||
map(cfg.o.keymaps.unfold, function()
|
||||
M._set_folded(false)
|
||||
end)
|
||||
-- toggle fold all
|
||||
map(cfg.o.keymaps.fold_toggle_all, M._toggle_all_fold)
|
||||
-- fold all
|
||||
map(cfg.o.keymaps.fold_all, function()
|
||||
M._set_all_folded(true)
|
||||
end)
|
||||
-- unfold all
|
||||
map(cfg.o.keymaps.unfold_all, function()
|
||||
M._set_all_folded(false)
|
||||
end)
|
||||
-- fold reset
|
||||
map(cfg.o.keymaps.fold_reset, function()
|
||||
M._set_all_folded(nil)
|
||||
end)
|
||||
end
|
||||
|
||||
local function handler(response, opts)
|
||||
if response == nil or type(response) ~= 'table' or M.view:is_open() then
|
||||
return
|
||||
end
|
||||
|
||||
M.state.code_win = vim.api.nvim_get_current_win()
|
||||
|
||||
M.view:setup_view()
|
||||
-- clear state when buffer is closed
|
||||
vim.api.nvim_buf_attach(M.view.bufnr, false, {
|
||||
on_detach = function(_, _)
|
||||
wipe_state()
|
||||
end,
|
||||
})
|
||||
|
||||
setup_keymaps(M.view.bufnr)
|
||||
setup_buffer_autocmd()
|
||||
|
||||
local items = parser.parse(response)
|
||||
|
||||
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)
|
||||
|
||||
M._highlight_current_item(M.state.code_win)
|
||||
|
||||
if not cfg.o.outline_window.focus_on_open or (opts and not opts.focus_outline) then
|
||||
vim.fn.win_gotoid(M.state.code_win)
|
||||
end
|
||||
end
|
||||
|
||||
---Set position of outline window to match cursor position in code, return
|
||||
---whether the window is just newly opened (previously not open).
|
||||
---@param opts table? Field `focus_outline` = `false` or `nil` means don't focus on outline window after following cursor. If opts is not provided, focus will be on outline window after following cursor.
|
||||
---@return boolean ok Whether it was successful. If ok=false, either the outline window is not open or the code window cannot be found.
|
||||
function M.follow_cursor(opts)
|
||||
if not M.view:is_open() then
|
||||
return false
|
||||
end
|
||||
|
||||
if require('outline.preview').has_code_win() then
|
||||
M._highlight_current_item(M.state.code_win)
|
||||
else
|
||||
return false
|
||||
end
|
||||
|
||||
if not opts then
|
||||
opts = { focus_outline = true }
|
||||
end
|
||||
if opts.focus_outline then
|
||||
M.focus_outline()
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local function _cmd_follow_cursor(opts)
|
||||
local fnopts = { focus_outline = true }
|
||||
if opts.bang then
|
||||
fnopts.focus_outline = false
|
||||
end
|
||||
M.follow_cursor(fnopts)
|
||||
end
|
||||
|
||||
function M._map_follow_cursor()
|
||||
if not M.follow_cursor({ focus_outline = true }) then
|
||||
vim.notify(
|
||||
"Code window no longer active. Try closing and reopening the outline.",
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
---Toggle the outline window, and return whether the outline window is open
|
||||
---after this operation.
|
||||
---@param opts table? Table of options, @see open_outline
|
||||
---@return boolean is_open Whether outline window is open
|
||||
function M.toggle_outline(opts)
|
||||
if M.view:is_open() then
|
||||
M.close_outline()
|
||||
return false
|
||||
else
|
||||
M.open_outline(opts)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
-- Used for Outline user command
|
||||
local function _cmd_toggle_outline(opts)
|
||||
if opts.bang then
|
||||
M.toggle_outline({ focus_outline = false })
|
||||
else
|
||||
M.toggle_outline({ focus_outline = true })
|
||||
end
|
||||
end
|
||||
|
||||
---Open the outline window.
|
||||
---@param opts table? Field focus_outline=false means don't focus on outline window after opening. If opts is not provided, focus will be on outline window after opening.
|
||||
function M.open_outline(opts)
|
||||
if not opts then
|
||||
opts = { focus_outline = true }
|
||||
end
|
||||
if not M.view:is_open() then
|
||||
local found = providers.request_symbols(handler, opts)
|
||||
if not found then
|
||||
vim.notify("[outline]: No providers found for current buffer", vim.log.levels.WARN)
|
||||
-- else
|
||||
-- print("Using provider ".._G._symbols_outline_current_provider.name.."...")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function _cmd_open_outline(opts)
|
||||
if opts.bang then
|
||||
M.open_outline({ focus_outline = false })
|
||||
else
|
||||
M.open_outline({ focus_outline = true })
|
||||
end
|
||||
end
|
||||
|
||||
---Close the outline window.
|
||||
function M.close_outline()
|
||||
M.view:close()
|
||||
end
|
||||
|
||||
---Set cursor to focus on the outline window, return whether the window is currently open..
|
||||
---@return boolean is_open Whether the window is open
|
||||
function M.focus_outline()
|
||||
if M.view:is_open() then
|
||||
vim.fn.win_gotoid(M.view.winnr)
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
---Set cursor to focus on the code window, return whether this operation was successful.
|
||||
---@return boolean ok Whether it was successful. If unsuccessful, it might mean that the attached code window has been closed or is no longer valid.
|
||||
function M.focus_code()
|
||||
if require('outline.preview').has_code_win() then
|
||||
vim.fn.win_gotoid(M.state.code_win)
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
---Toggle focus between outline and code window, returns whether it was successful.
|
||||
---@return boolean ok Whether it was successful. If `ok=false`, either the outline window is not open or the code window is no longer valid.
|
||||
function M.focus_toggle()
|
||||
if M.view:is_open() and require('outline.preview').has_code_win() then
|
||||
local winid = vim.fn.win_getid()
|
||||
if winid == M.state.code_win then
|
||||
vim.fn.win_gotoid(M.view.winnr)
|
||||
else
|
||||
vim.fn.win_gotoid(M.state.code_win)
|
||||
end
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
---Whether the outline window is currently open.
|
||||
---@return boolean is_open
|
||||
function M.is_open()
|
||||
return M.view:is_open()
|
||||
end
|
||||
|
||||
---Display outline window status in the message area.
|
||||
function M.show_status()
|
||||
if M.has_provider() then
|
||||
print("Current provider:")
|
||||
print(' ' .. _G._symbols_outline_current_provider.name)
|
||||
if M.view:is_open() then
|
||||
print("Outline window is open.")
|
||||
else
|
||||
print("Outline window is not open.")
|
||||
end
|
||||
if require('outline.preview').has_code_win() then
|
||||
print("Code window is active.")
|
||||
else
|
||||
print("Warning: code window is either closed or invalid. Please close and reopen the outline window.")
|
||||
end
|
||||
else
|
||||
print("No providers")
|
||||
end
|
||||
end
|
||||
|
||||
---Whether there is currently an available provider.
|
||||
---@return boolean has_provider
|
||||
function M.has_provider()
|
||||
local winid = vim.fn.win_getid()
|
||||
if M.view:is_open() and winid == M.view.winnr then
|
||||
return _G._symbols_outline_current_provider ~= nil
|
||||
end
|
||||
return providers.has_provider() and _G._symbols_outline_current_provider
|
||||
end
|
||||
|
||||
local function setup_commands()
|
||||
local cmd = function(n, c, o)
|
||||
vim.api.nvim_create_user_command('Outline'..n, c, o)
|
||||
end
|
||||
|
||||
cmd('', _cmd_toggle_outline, {
|
||||
desc = "Toggle the outline window. \
|
||||
With bang, keep focus on initial window after opening.",
|
||||
nargs = 0,
|
||||
bang = true,
|
||||
})
|
||||
cmd('Open', _cmd_open_outline, {
|
||||
desc = "With bang, keep focus on initial window after opening.",
|
||||
nargs = 0,
|
||||
bang = true,
|
||||
})
|
||||
cmd('Close', M.close_outline, { nargs = 0 })
|
||||
cmd('FocusOutline', M.focus_outline, { nargs = 0 })
|
||||
cmd('FocusCode', M.focus_code, { nargs = 0 })
|
||||
cmd('Focus', M.focus_toggle, { nargs = 0 })
|
||||
cmd('Status', M.show_status, {
|
||||
desc = "Show a message about the current status of the outline window.",
|
||||
nargs = 0,
|
||||
})
|
||||
cmd('Follow', _cmd_follow_cursor, {
|
||||
desc = "Update position of outline with position of cursor. \
|
||||
With bang, don't switch cursor focus to outline window.",
|
||||
nargs = 0,
|
||||
bang = true,
|
||||
})
|
||||
end
|
||||
|
||||
---Set up configuration options for outline.
|
||||
function M.setup(opts)
|
||||
cfg.setup(opts)
|
||||
ui.setup_highlights()
|
||||
|
||||
M.view = View:new()
|
||||
setup_global_autocmd()
|
||||
setup_commands()
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user