diff --git a/lua/cmp/utils/window.lua b/lua/cmp/utils/window.lua index fd0afee..6f87ce4 100644 --- a/lua/cmp/utils/window.lua +++ b/lua/cmp/utils/window.lua @@ -25,21 +25,28 @@ local window = {} ---@return cmp.Window window.new = function() local self = setmetatable({}, { __index = window }) - self.buf = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_option(self.buf, 'undolevels', -1) - vim.api.nvim_buf_set_option(self.buf, 'buftype', 'nofile') + self:ensure() self.win = nil - self.style = {} - self.sbuf1 = vim.api.nvim_create_buf(false, true) self.swin1 = nil - self.sbuf2 = vim.api.nvim_create_buf(false, true) self.swin2 = nil + self.style = {} self.cache = cache.new() self.opt = {} self.id = 0 return self end +---Ensure valid state. +window.ensure = function(self) + for _, name in ipairs({ 'buf', 'sbuf1', 'subf2' }) do + if not (self[name] and vim.api.nvim_buf_is_valid(self[name])) then + self[name] = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_option(self[name], 'undolevels', -1) + vim.api.nvim_buf_set_option(self[name], 'buftype', 'nofile') + end + end +end + ---Set window option. ---NOTE: If the window already visible, immediately applied to it. ---@param key string diff --git a/lua/cmp/view/custom_entries_view.lua b/lua/cmp/view/custom_entries_view.lua index 7f42c54..b371b35 100644 --- a/lua/cmp/view/custom_entries_view.lua +++ b/lua/cmp/view/custom_entries_view.lua @@ -89,6 +89,8 @@ custom_entries_view.redraw = function() end custom_entries_view.open = function(self, offset, entries) + self.entries_win:ensure() + self.offset = offset self.entries = {} self.column_bytes = { abbr = 0, kind = 0, menu = 0 } diff --git a/lua/cmp/view/docs_view.lua b/lua/cmp/view/docs_view.lua index aac0cf0..e005903 100644 --- a/lua/cmp/view/docs_view.lua +++ b/lua/cmp/view/docs_view.lua @@ -22,6 +22,8 @@ end ---@param e cmp.Entry ---@param view cmp.WindowStyle docs_view.open = function(self, e, view) + self.window:ensure() + local documentation = config.get().documentation if not documentation then return