refractor(debug): Move debug stuff to its own module
This commit is contained in:
@@ -4,19 +4,9 @@ local parser = require('symbols-outline.parser')
|
|||||||
local ui = require('symbols-outline.ui')
|
local ui = require('symbols-outline.ui')
|
||||||
local writer = require('symbols-outline.writer')
|
local writer = require('symbols-outline.writer')
|
||||||
|
|
||||||
local D = {}
|
local M = {}
|
||||||
|
|
||||||
-- needs plenary
|
|
||||||
local reload = require('plenary.reload').reload_module
|
|
||||||
|
|
||||||
function D.R(name)
|
|
||||||
reload(name)
|
|
||||||
return require(name)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function setup_commands()
|
local function setup_commands()
|
||||||
vim.cmd("command! " .. "DSymbolsOutline " ..
|
|
||||||
":lua require'symbols-outline'.R('symbols-outline').toggle_outline()")
|
|
||||||
vim.cmd("command! " .. "SymbolsOutline " ..
|
vim.cmd("command! " .. "SymbolsOutline " ..
|
||||||
":lua require'symbols-outline'.toggle_outline()")
|
":lua require'symbols-outline'.toggle_outline()")
|
||||||
end
|
end
|
||||||
@@ -25,7 +15,7 @@ local function setup_autocmd()
|
|||||||
vim.cmd(
|
vim.cmd(
|
||||||
"au InsertLeave,BufEnter,BufWinEnter,TabEnter,BufWritePost * :lua require('symbols-outline')._refresh()")
|
"au InsertLeave,BufEnter,BufWinEnter,TabEnter,BufWritePost * :lua require('symbols-outline')._refresh()")
|
||||||
vim.cmd "au BufDelete * lua require'symbols-outline'._prevent_buffer_override()"
|
vim.cmd "au BufDelete * lua require'symbols-outline'._prevent_buffer_override()"
|
||||||
if D.opts.highlight_hovered_item then
|
if M.opts.highlight_hovered_item then
|
||||||
vim.cmd(
|
vim.cmd(
|
||||||
"autocmd CursorHold * :lua require('symbols-outline')._highlight_current_item()")
|
"autocmd CursorHold * :lua require('symbols-outline')._highlight_current_item()")
|
||||||
end
|
end
|
||||||
@@ -38,7 +28,7 @@ end
|
|||||||
-------------------------
|
-------------------------
|
||||||
-- STATE
|
-- STATE
|
||||||
-------------------------
|
-------------------------
|
||||||
D.state = {
|
M.state = {
|
||||||
outline_items = {},
|
outline_items = {},
|
||||||
flattened_outline_items = {},
|
flattened_outline_items = {},
|
||||||
outline_win = nil,
|
outline_win = nil,
|
||||||
@@ -47,43 +37,43 @@ D.state = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
local function wipe_state()
|
local function wipe_state()
|
||||||
D.state = {outline_items = {}, flattened_outline_items = {}}
|
M.state = {outline_items = {}, flattened_outline_items = {}}
|
||||||
end
|
end
|
||||||
|
|
||||||
function D._refresh()
|
function M._refresh()
|
||||||
if D.state.outline_buf ~= nil then
|
if M.state.outline_buf ~= nil then
|
||||||
vim.lsp.buf_request(0, "textDocument/documentSymbol", getParams(),
|
vim.lsp.buf_request(0, "textDocument/documentSymbol", getParams(),
|
||||||
function(_, _, result)
|
function(_, _, result)
|
||||||
if result == nil or type(result) ~= 'table' then return end
|
if result == nil or type(result) ~= 'table' then return end
|
||||||
|
|
||||||
D.state.code_win = vim.api.nvim_get_current_win()
|
M.state.code_win = vim.api.nvim_get_current_win()
|
||||||
D.state.outline_items = parser.parse(result)
|
M.state.outline_items = parser.parse(result)
|
||||||
D.state.flattened_outline_items =
|
M.state.flattened_outline_items =
|
||||||
parser.flatten(parser.parse(result))
|
parser.flatten(parser.parse(result))
|
||||||
|
|
||||||
writer.parse_and_write(D.state.outline_buf, D.state.outline_win,
|
writer.parse_and_write(M.state.outline_buf, M.state.outline_win,
|
||||||
D.state.outline_items,
|
M.state.outline_items,
|
||||||
D.state.flattened_outline_items)
|
M.state.flattened_outline_items)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function D._goto_location()
|
function M._goto_location()
|
||||||
local current_line = vim.api.nvim_win_get_cursor(D.state.outline_win)[1]
|
local current_line = vim.api.nvim_win_get_cursor(M.state.outline_win)[1]
|
||||||
local node = D.state.flattened_outline_items[current_line]
|
local node = M.state.flattened_outline_items[current_line]
|
||||||
vim.fn.win_gotoid(D.state.code_win)
|
vim.fn.win_gotoid(M.state.code_win)
|
||||||
vim.fn.cursor(node.line + 1, node.character + 1)
|
vim.fn.cursor(node.line + 1, node.character + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
function D._highlight_current_item()
|
function M._highlight_current_item()
|
||||||
if D.state.outline_buf == nil or vim.api.nvim_get_current_buf() ==
|
if M.state.outline_buf == nil or vim.api.nvim_get_current_buf() ==
|
||||||
D.state.outline_buf then return end
|
M.state.outline_buf then return end
|
||||||
|
|
||||||
local hovered_line = vim.api.nvim_win_get_cursor(
|
local hovered_line = vim.api.nvim_win_get_cursor(
|
||||||
vim.api.nvim_get_current_win())[1] - 1
|
vim.api.nvim_get_current_win())[1] - 1
|
||||||
|
|
||||||
local nodes = {}
|
local nodes = {}
|
||||||
for index, value in ipairs(D.state.flattened_outline_items) do
|
for index, value in ipairs(M.state.flattened_outline_items) do
|
||||||
if value.line == hovered_line or
|
if value.line == hovered_line or
|
||||||
(hovered_line > value.range_start and hovered_line < value.range_end) then
|
(hovered_line > value.range_start and hovered_line < value.range_end) then
|
||||||
value.line_in_outline = index
|
value.line_in_outline = index
|
||||||
@@ -92,27 +82,27 @@ function D._highlight_current_item()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- clear old highlight
|
-- clear old highlight
|
||||||
ui.clear_hover_highlight(D.state.outline_buf)
|
ui.clear_hover_highlight(M.state.outline_buf)
|
||||||
for _, value in ipairs(nodes) do
|
for _, value in ipairs(nodes) do
|
||||||
ui.add_hover_highlight(D.state.outline_buf, value.line_in_outline - 1,
|
ui.add_hover_highlight(M.state.outline_buf, value.line_in_outline - 1,
|
||||||
value.depth * 2)
|
value.depth * 2)
|
||||||
vim.api.nvim_win_set_cursor(D.state.outline_win,
|
vim.api.nvim_win_set_cursor(M.state.outline_win,
|
||||||
{value.line_in_outline, 1})
|
{value.line_in_outline, 1})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- credits: https://github.com/kyazdani42/nvim-tree.lua
|
-- credits: https://github.com/kyazdani42/nvim-tree.lua
|
||||||
function D._prevent_buffer_override()
|
function M._prevent_buffer_override()
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
local curwin = vim.api.nvim_get_current_win()
|
local curwin = vim.api.nvim_get_current_win()
|
||||||
local curbuf = vim.api.nvim_win_get_buf(curwin)
|
local curbuf = vim.api.nvim_win_get_buf(curwin)
|
||||||
local wins = vim.api.nvim_list_wins()
|
local wins = vim.api.nvim_list_wins()
|
||||||
|
|
||||||
if curwin ~= D.state.outline_win or curbuf ~= D.state.outline_buf then
|
if curwin ~= M.state.outline_win or curbuf ~= M.state.outline_buf then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.cmd("buffer " .. D.state.outline_buf)
|
vim.cmd("buffer " .. M.state.outline_buf)
|
||||||
|
|
||||||
local current_win_width = vim.api.nvim_win_get_width(curwin)
|
local current_win_width = vim.api.nvim_win_get_width(curwin)
|
||||||
if #wins < 2 then
|
if #wins < 2 then
|
||||||
@@ -149,58 +139,57 @@ end
|
|||||||
-- WINDOW AND BUFFER STUFF
|
-- WINDOW AND BUFFER STUFF
|
||||||
----------------------------
|
----------------------------
|
||||||
local function setup_buffer()
|
local function setup_buffer()
|
||||||
D.state.outline_buf = vim.api.nvim_create_buf(false, true)
|
M.state.outline_buf = vim.api.nvim_create_buf(false, true)
|
||||||
vim.api.nvim_buf_attach(D.state.outline_buf, false,
|
vim.api.nvim_buf_attach(M.state.outline_buf, false,
|
||||||
{on_detach = function(_, _) wipe_state() end})
|
{on_detach = function(_, _) wipe_state() end})
|
||||||
vim.api.nvim_buf_set_option(D.state.outline_buf, "bufhidden", "delete")
|
vim.api.nvim_buf_set_option(M.state.outline_buf, "bufhidden", "delete")
|
||||||
|
|
||||||
local current_win = vim.api.nvim_get_current_win()
|
local current_win = vim.api.nvim_get_current_win()
|
||||||
local current_win_width = vim.api.nvim_win_get_width(current_win)
|
local current_win_width = vim.api.nvim_win_get_width(current_win)
|
||||||
|
|
||||||
vim.cmd("vsplit")
|
vim.cmd("vsplit")
|
||||||
vim.cmd("vertical resize " .. math.ceil(current_win_width * 0.25))
|
vim.cmd("vertical resize " .. math.ceil(current_win_width * 0.25))
|
||||||
D.state.outline_win = vim.api.nvim_get_current_win()
|
M.state.outline_win = vim.api.nvim_get_current_win()
|
||||||
vim.api.nvim_win_set_buf(D.state.outline_win, D.state.outline_buf)
|
vim.api.nvim_win_set_buf(M.state.outline_win, M.state.outline_buf)
|
||||||
|
|
||||||
setup_keymaps(D.state.outline_buf)
|
setup_keymaps(M.state.outline_buf)
|
||||||
|
|
||||||
vim.api.nvim_win_set_option(D.state.outline_win, "number", false)
|
vim.api.nvim_win_set_option(M.state.outline_win, "number", false)
|
||||||
vim.api.nvim_win_set_option(D.state.outline_win, "relativenumber", false)
|
vim.api.nvim_win_set_option(M.state.outline_win, "relativenumber", false)
|
||||||
vim.api.nvim_buf_set_name(D.state.outline_buf, "OUTLINE")
|
vim.api.nvim_buf_set_name(M.state.outline_buf, "OUTLINE")
|
||||||
vim.api.nvim_buf_set_option(D.state.outline_buf, "modifiable", false)
|
vim.api.nvim_buf_set_option(M.state.outline_buf, "modifiable", false)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function handler(_, _, result)
|
local function handler(_, _, result)
|
||||||
if result == nil or type(result) ~= 'table' then return end
|
if result == nil or type(result) ~= 'table' then return end
|
||||||
|
|
||||||
D.state.code_win = vim.api.nvim_get_current_win()
|
M.state.code_win = vim.api.nvim_get_current_win()
|
||||||
|
|
||||||
setup_buffer()
|
setup_buffer()
|
||||||
D.state.outline_items = parser.parse(result)
|
M.state.outline_items = parser.parse(result)
|
||||||
D.state.flattened_outline_items = parser.flatten(parser.parse(result))
|
M.state.flattened_outline_items = parser.flatten(parser.parse(result))
|
||||||
|
|
||||||
writer.parse_and_write(D.state.outline_buf, D.state.outline_win,
|
writer.parse_and_write(M.state.outline_buf, M.state.outline_win,
|
||||||
D.state.outline_items,
|
M.state.outline_items,
|
||||||
D.state.flattened_outline_items)
|
M.state.flattened_outline_items)
|
||||||
ui.setup_highlights()
|
ui.setup_highlights()
|
||||||
end
|
end
|
||||||
|
|
||||||
function D.toggle_outline()
|
function M.toggle_outline()
|
||||||
if D.state.outline_buf == nil then
|
if M.state.outline_buf == nil then
|
||||||
vim.lsp.buf_request(0, "textDocument/documentSymbol", getParams(),
|
vim.lsp.buf_request(0, "textDocument/documentSymbol", getParams(),
|
||||||
handler)
|
handler)
|
||||||
else
|
else
|
||||||
vim.api.nvim_win_close(D.state.outline_win, true)
|
vim.api.nvim_win_close(M.state.outline_win, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function D.setup(opts)
|
function M.setup(opts)
|
||||||
vim.tbl_deep_extend("force", D.opts, opts or {})
|
vim.tbl_deep_extend("force", M.opts, opts or {})
|
||||||
|
|
||||||
setup_commands()
|
setup_commands()
|
||||||
setup_autocmd()
|
setup_autocmd()
|
||||||
end
|
end
|
||||||
|
M.opts = {highlight_hovered_item = true}
|
||||||
|
|
||||||
D.opts = {highlight_hovered_item = true}
|
return M
|
||||||
|
|
||||||
return D
|
|
||||||
|
|||||||
18
lua/symbols-outline/debug.lua
Normal file
18
lua/symbols-outline/debug.lua
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
local vim = vim
|
||||||
|
|
||||||
|
local D = {}
|
||||||
|
|
||||||
|
-- needs plenary
|
||||||
|
local reload = require('plenary.reload').reload_module
|
||||||
|
|
||||||
|
function D.R(name)
|
||||||
|
reload(name)
|
||||||
|
return require(name)
|
||||||
|
end
|
||||||
|
|
||||||
|
function D.setup_commands()
|
||||||
|
vim.cmd("command! " .. "DSymbolsOutline " ..
|
||||||
|
":lua require'symbols-outline'.R('symbols-outline').toggle_outline()")
|
||||||
|
end
|
||||||
|
|
||||||
|
return D
|
||||||
Reference in New Issue
Block a user