feat(view): Turn View into a class and refactor closing
This commit is contained in:
@@ -6,7 +6,7 @@ local ui = require 'symbols-outline.ui'
|
||||
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 View = require 'symbols-outline.view'
|
||||
|
||||
local M = {}
|
||||
|
||||
@@ -40,7 +40,7 @@ local function wipe_state()
|
||||
end
|
||||
|
||||
local function __refresh()
|
||||
if M.state.outline_buf ~= nil then
|
||||
if M.view.bufnr ~= nil then
|
||||
local function refresh_handler(response)
|
||||
if response == nil or type(response) ~= 'table' then
|
||||
return
|
||||
@@ -52,7 +52,7 @@ local function __refresh()
|
||||
M.state.outline_items = items
|
||||
M.state.flattened_outline_items = parser.flatten(items)
|
||||
|
||||
writer.parse_and_write(M.state.outline_buf, M.state.flattened_outline_items)
|
||||
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
|
||||
end
|
||||
|
||||
providers.request_symbols(refresh_handler)
|
||||
@@ -62,7 +62,7 @@ end
|
||||
M._refresh = utils.debounce(__refresh, 100)
|
||||
|
||||
function M._goto_location(change_focus)
|
||||
local current_line = vim.api.nvim_win_get_cursor(M.state.outline_win)[1]
|
||||
local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1]
|
||||
local node = M.state.flattened_outline_items[current_line]
|
||||
vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character })
|
||||
if change_focus then
|
||||
@@ -76,9 +76,9 @@ end
|
||||
function M._highlight_current_item(winnr)
|
||||
local has_provider = providers.has_provider()
|
||||
|
||||
local is_current_buffer_the_outline = M.state.outline_buf == vim.api.nvim_get_current_buf()
|
||||
local is_current_buffer_the_outline = M.view.bufnr == vim.api.nvim_get_current_buf()
|
||||
|
||||
local doesnt_have_outline_buf = not M.state.outline_buf
|
||||
local doesnt_have_outline_buf = not M.view.bufnr
|
||||
|
||||
local should_exit = not has_provider or doesnt_have_outline_buf or is_current_buffer_the_outline
|
||||
|
||||
@@ -106,10 +106,10 @@ function M._highlight_current_item(winnr)
|
||||
end
|
||||
|
||||
-- clear old highlight
|
||||
ui.clear_hover_highlight(M.state.outline_buf)
|
||||
ui.clear_hover_highlight(M.view.bufnr)
|
||||
for _, value in ipairs(nodes) do
|
||||
ui.add_hover_highlight(M.state.outline_buf, value.line_in_outline - 1, value.depth * 2)
|
||||
vim.api.nvim_win_set_cursor(M.state.outline_win, { value.line_in_outline, 1 })
|
||||
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 })
|
||||
end
|
||||
end
|
||||
|
||||
@@ -142,14 +142,14 @@ local function handler(response)
|
||||
|
||||
M.state.code_win = vim.api.nvim_get_current_win()
|
||||
|
||||
M.state.outline_buf, M.state.outline_win = view.setup_view()
|
||||
M.view:setup_view()
|
||||
-- clear state when buffer is closed
|
||||
vim.api.nvim_buf_attach(M.state.outline_buf, false, {
|
||||
vim.api.nvim_buf_attach(M.view.bufnr, false, {
|
||||
on_detach = function(_, _)
|
||||
wipe_state()
|
||||
end,
|
||||
})
|
||||
setup_keymaps(M.state.outline_buf)
|
||||
setup_keymaps(M.view.bufnr)
|
||||
setup_buffer_autocmd()
|
||||
|
||||
local items = parser.parse(response)
|
||||
@@ -157,14 +157,14 @@ local function handler(response)
|
||||
M.state.outline_items = items
|
||||
M.state.flattened_outline_items = parser.flatten(items)
|
||||
|
||||
writer.parse_and_write(M.state.outline_buf, M.state.flattened_outline_items)
|
||||
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
|
||||
ui.setup_highlights()
|
||||
|
||||
M._highlight_current_item(M.state.code_win)
|
||||
end
|
||||
|
||||
function M.toggle_outline()
|
||||
if M.state.outline_buf == nil then
|
||||
if M.view.bufnr == nil then
|
||||
M.open_outline()
|
||||
else
|
||||
M.close_outline()
|
||||
@@ -172,19 +172,19 @@ function M.toggle_outline()
|
||||
end
|
||||
|
||||
function M.open_outline()
|
||||
if M.state.outline_buf == nil then
|
||||
if not M.view:is_open() then
|
||||
providers.request_symbols(handler)
|
||||
end
|
||||
end
|
||||
|
||||
function M.close_outline()
|
||||
if M.state.outline_buf then
|
||||
vim.api.nvim_win_close(M.state.outline_win, true)
|
||||
end
|
||||
M.view:close()
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
config.setup(opts)
|
||||
|
||||
M.view = View:new()
|
||||
setup_global_autocmd()
|
||||
end
|
||||
|
||||
|
||||
@@ -1,43 +1,53 @@
|
||||
local config = require 'symbols-outline.config'
|
||||
|
||||
local M = {}
|
||||
local View = {}
|
||||
|
||||
function View:new()
|
||||
return setmetatable({ bufnr = nil, winnr = nil }, { __index = View })
|
||||
end
|
||||
|
||||
---creates the outline window and sets it up
|
||||
---@return string bufnr
|
||||
---@return string bufnr
|
||||
function M.setup_view()
|
||||
function View:setup_view()
|
||||
-- create a scratch unlisted buffer
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
self.bufnr = vim.api.nvim_create_buf(false, true)
|
||||
|
||||
-- delete buffer when window is closed / buffer is hidden
|
||||
vim.api.nvim_buf_set_option(bufnr, 'bufhidden', 'delete')
|
||||
vim.api.nvim_buf_set_option(self.bufnr, 'bufhidden', 'delete')
|
||||
-- create a split
|
||||
vim.cmd(config.get_split_command())
|
||||
-- resize to a % of the current window size
|
||||
vim.cmd('vertical resize ' .. config.get_window_width())
|
||||
|
||||
-- get current (outline) window and attach our buffer to it
|
||||
local winnr = vim.api.nvim_get_current_win()
|
||||
vim.api.nvim_win_set_buf(winnr, bufnr)
|
||||
self.winnr = vim.api.nvim_get_current_win()
|
||||
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
|
||||
|
||||
-- window stuff
|
||||
vim.api.nvim_win_set_option(winnr, 'number', false)
|
||||
vim.api.nvim_win_set_option(winnr, 'relativenumber', false)
|
||||
vim.api.nvim_win_set_option(winnr, 'winfixwidth', true)
|
||||
vim.api.nvim_win_set_option(self.winnr, 'number', false)
|
||||
vim.api.nvim_win_set_option(self.winnr, 'relativenumber', false)
|
||||
vim.api.nvim_win_set_option(self.winnr, 'winfixwidth', true)
|
||||
-- buffer stuff
|
||||
vim.api.nvim_buf_set_name(bufnr, 'OUTLINE')
|
||||
vim.api.nvim_buf_set_option(bufnr, 'filetype', 'Outline')
|
||||
vim.api.nvim_buf_set_option(bufnr, 'modifiable', false)
|
||||
vim.api.nvim_buf_set_name(self.bufnr, 'OUTLINE')
|
||||
vim.api.nvim_buf_set_option(self.bufnr, 'filetype', 'Outline')
|
||||
vim.api.nvim_buf_set_option(self.bufnr, 'modifiable', false)
|
||||
|
||||
if config.options.show_numbers or config.options.show_relative_numbers then
|
||||
vim.api.nvim_win_set_option(winnr, 'nu', true)
|
||||
vim.api.nvim_win_set_option(self.winnr, 'nu', true)
|
||||
end
|
||||
|
||||
if config.options.show_relative_numbers then
|
||||
vim.api.nvim_win_set_option(winnr, 'rnu', true)
|
||||
vim.api.nvim_win_set_option(self.winnr, 'rnu', true)
|
||||
end
|
||||
|
||||
return bufnr, winnr
|
||||
end
|
||||
|
||||
return M
|
||||
function View:close()
|
||||
vim.api.nvim_win_close(self.winnr, true)
|
||||
self.winnr = nil
|
||||
self.bufnr = nil
|
||||
end
|
||||
|
||||
function View:is_open()
|
||||
return self.winnr and self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) and vim.api.nvim_win_is_valid(self.winnr)
|
||||
end
|
||||
|
||||
return View
|
||||
|
||||
Reference in New Issue
Block a user