feat(view): Turn View into a class and refactor closing
This commit is contained in:
@@ -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