refactor: Move buffer/window creation to its own module

For now we have some stuff out of that module to prevent infinite
require loops (set_keymaps/clear_state), but once state goes into its
own module as well then we can work it all into view.lua
This commit is contained in:
simrat39
2021-08-22 17:22:16 -07:00
parent c646d68fe8
commit fae3393873
2 changed files with 54 additions and 37 deletions

View File

@@ -7,6 +7,7 @@ local config = require('symbols-outline.config')
local lsp_utils = require('symbols-outline.utils.lsp_utils')
local utils = require('symbols-outline.utils.init')
local markdown = require('symbols-outline.markdown')
local view = require('symbols-outline.view')
local M = {}
@@ -162,48 +163,16 @@ local function setup_keymaps(bufnr)
map(config.options.keymaps.close, ":bw!<Cr>")
end
----------------------------
-- WINDOW AND BUFFER STUFF
----------------------------
local function setup_buffer()
M.state.outline_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_attach(M.state.outline_buf, false,
{on_detach = function(_, _) wipe_state() end})
vim.api.nvim_buf_set_option(M.state.outline_buf, "bufhidden", "delete")
local current_win = vim.api.nvim_get_current_win()
local current_win_width = vim.api.nvim_win_get_width(current_win)
vim.cmd(config.get_split_command())
vim.cmd("vertical resize " ..
math.ceil(current_win_width * config.get_width_percentage()))
M.state.outline_win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(M.state.outline_win, M.state.outline_buf)
setup_keymaps(M.state.outline_buf)
vim.api.nvim_win_set_option(M.state.outline_win, "number", false)
vim.api.nvim_win_set_option(M.state.outline_win, "relativenumber", false)
vim.api.nvim_win_set_option(M.state.outline_win, "winfixwidth", true)
vim.api.nvim_buf_set_name(M.state.outline_buf, "OUTLINE")
vim.api.nvim_buf_set_option(M.state.outline_buf, "filetype", "Outline")
vim.api.nvim_buf_set_option(M.state.outline_buf, "modifiable", false)
if config.options.show_numbers or config.options.show_relative_numbers then
vim.api.nvim_win_set_option(M.state.outline_win, "nu", true)
end
if config.options.show_relative_numbers then
vim.api.nvim_win_set_option(M.state.outline_win, "rnu", true)
end
end
local function handler(response)
if response == nil or type(response) ~= 'table' then return end
M.state.code_win = vim.api.nvim_get_current_win()
setup_buffer()
M.state.outline_buf, M.state.outline_win = view.setup_view()
-- clear state when buffer is closed
vim.api.nvim_buf_attach(M.state.outline_buf, false,
{on_detach = function(_, _) wipe_state() end})
setup_keymaps(M.state.outline_buf)
setup_buffer_autocmd()
local items = parser.parse(response)

View File

@@ -0,0 +1,48 @@
local config = require('symbols-outline.config')
local M = {}
---creates the outline window and sets it up
---@return string bufnr
---@return string bufnr
function M.setup_view()
-- create a scratch unlisted buffer
local 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")
local current_win = vim.api.nvim_get_current_win()
local current_win_width = vim.api.nvim_win_get_width(current_win)
-- create a split
vim.cmd(config.get_split_command())
-- resize to a % of the current window size
vim.cmd("vertical resize " ..
math.ceil(current_win_width * config.get_width_percentage()))
-- 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)
-- 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)
-- 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)
if config.options.show_numbers or config.options.show_relative_numbers then
vim.api.nvim_win_set_option(winnr, "nu", true)
end
if config.options.show_relative_numbers then
vim.api.nvim_win_set_option(winnr, "rnu", true)
end
return bufnr, winnr
end
return M