feat(view): Turn View into a class and refactor closing

This commit is contained in:
Simrat Grewal
2022-08-10 14:46:01 -07:00
parent 13e89b6349
commit 50dd2e9275
2 changed files with 47 additions and 37 deletions

View File

@@ -6,7 +6,7 @@ local ui = require 'symbols-outline.ui'
local writer = require 'symbols-outline.writer' local writer = require 'symbols-outline.writer'
local config = require 'symbols-outline.config' local config = require 'symbols-outline.config'
local utils = require 'symbols-outline.utils.init' local utils = require 'symbols-outline.utils.init'
local view = require 'symbols-outline.view' local View = require 'symbols-outline.view'
local M = {} local M = {}
@@ -40,7 +40,7 @@ local function wipe_state()
end end
local function __refresh() local function __refresh()
if M.state.outline_buf ~= nil then if M.view.bufnr ~= nil then
local function refresh_handler(response) local function refresh_handler(response)
if response == nil or type(response) ~= 'table' then if response == nil or type(response) ~= 'table' then
return return
@@ -52,7 +52,7 @@ local function __refresh()
M.state.outline_items = items M.state.outline_items = items
M.state.flattened_outline_items = parser.flatten(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 end
providers.request_symbols(refresh_handler) providers.request_symbols(refresh_handler)
@@ -62,7 +62,7 @@ end
M._refresh = utils.debounce(__refresh, 100) M._refresh = utils.debounce(__refresh, 100)
function M._goto_location(change_focus) 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] local node = M.state.flattened_outline_items[current_line]
vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character }) vim.api.nvim_win_set_cursor(M.state.code_win, { node.line + 1, node.character })
if change_focus then if change_focus then
@@ -76,9 +76,9 @@ end
function M._highlight_current_item(winnr) function M._highlight_current_item(winnr)
local has_provider = providers.has_provider() 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 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 end
-- clear old highlight -- clear old highlight
ui.clear_hover_highlight(M.state.outline_buf) ui.clear_hover_highlight(M.view.bufnr)
for _, value in ipairs(nodes) do for _, value in ipairs(nodes) do
ui.add_hover_highlight(M.state.outline_buf, value.line_in_outline - 1, value.depth * 2) ui.add_hover_highlight(M.view.bufnr, value.line_in_outline - 1, value.depth * 2)
vim.api.nvim_win_set_cursor(M.state.outline_win, { value.line_in_outline, 1 }) vim.api.nvim_win_set_cursor(M.view.winnr, { value.line_in_outline, 1 })
end end
end end
@@ -142,14 +142,14 @@ local function handler(response)
M.state.code_win = vim.api.nvim_get_current_win() 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 -- 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(_, _) on_detach = function(_, _)
wipe_state() wipe_state()
end, end,
}) })
setup_keymaps(M.state.outline_buf) setup_keymaps(M.view.bufnr)
setup_buffer_autocmd() setup_buffer_autocmd()
local items = parser.parse(response) local items = parser.parse(response)
@@ -157,14 +157,14 @@ local function handler(response)
M.state.outline_items = items M.state.outline_items = items
M.state.flattened_outline_items = parser.flatten(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() ui.setup_highlights()
M._highlight_current_item(M.state.code_win) M._highlight_current_item(M.state.code_win)
end end
function M.toggle_outline() function M.toggle_outline()
if M.state.outline_buf == nil then if M.view.bufnr == nil then
M.open_outline() M.open_outline()
else else
M.close_outline() M.close_outline()
@@ -172,19 +172,19 @@ function M.toggle_outline()
end end
function M.open_outline() function M.open_outline()
if M.state.outline_buf == nil then if not M.view:is_open() then
providers.request_symbols(handler) providers.request_symbols(handler)
end end
end end
function M.close_outline() function M.close_outline()
if M.state.outline_buf then M.view:close()
vim.api.nvim_win_close(M.state.outline_win, true)
end
end end
function M.setup(opts) function M.setup(opts)
config.setup(opts) config.setup(opts)
M.view = View:new()
setup_global_autocmd() setup_global_autocmd()
end end

View File

@@ -1,43 +1,53 @@
local config = require 'symbols-outline.config' 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 ---creates the outline window and sets it up
---@return string bufnr function View:setup_view()
---@return string bufnr
function M.setup_view()
-- create a scratch unlisted buffer -- 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 -- 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 -- create a split
vim.cmd(config.get_split_command()) vim.cmd(config.get_split_command())
-- resize to a % of the current window size -- resize to a % of the current window size
vim.cmd('vertical resize ' .. config.get_window_width()) vim.cmd('vertical resize ' .. config.get_window_width())
-- get current (outline) window and attach our buffer to it -- get current (outline) window and attach our buffer to it
local winnr = vim.api.nvim_get_current_win() self.winnr = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(winnr, bufnr) vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
-- window stuff -- window stuff
vim.api.nvim_win_set_option(winnr, 'number', false) vim.api.nvim_win_set_option(self.winnr, 'number', false)
vim.api.nvim_win_set_option(winnr, 'relativenumber', false) vim.api.nvim_win_set_option(self.winnr, 'relativenumber', false)
vim.api.nvim_win_set_option(winnr, 'winfixwidth', true) vim.api.nvim_win_set_option(self.winnr, 'winfixwidth', true)
-- buffer stuff -- buffer stuff
vim.api.nvim_buf_set_name(bufnr, 'OUTLINE') vim.api.nvim_buf_set_name(self.bufnr, 'OUTLINE')
vim.api.nvim_buf_set_option(bufnr, 'filetype', 'Outline') vim.api.nvim_buf_set_option(self.bufnr, 'filetype', 'Outline')
vim.api.nvim_buf_set_option(bufnr, 'modifiable', false) vim.api.nvim_buf_set_option(self.bufnr, 'modifiable', false)
if config.options.show_numbers or config.options.show_relative_numbers then 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 end
if config.options.show_relative_numbers then 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
end end
return bufnr, winnr function View:close()
vim.api.nvim_win_close(self.winnr, true)
self.winnr = nil
self.bufnr = nil
end end
return M 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