Improved: ALL option settings do not fire the OptionSet event (#1417)

This commit is contained in:
tummetott
2023-02-03 17:09:00 +01:00
committed by GitHub
parent e7e2ef7031
commit cfafe0a1ca
2 changed files with 34 additions and 16 deletions

25
lua/cmp/utils/options.lua Normal file
View File

@@ -0,0 +1,25 @@
local M = {}
-- Set window option without triggering the OptionSet event
---@param window number
---@param name string
---@param value any
M.win_set_option = function(window, name, value)
local eventignore = vim.opt.eventignore:get()
vim.opt.eventignore:append('OptionSet')
vim.api.nvim_win_set_option(window, name, value)
vim.opt.eventignore = eventignore
end
-- Set buffer option without triggering the OptionSet event
---@param buffer number
---@param name string
---@param value any
M.buf_set_option = function(buffer, name, value)
local eventignore = vim.opt.eventignore:get()
vim.opt.eventignore:append('OptionSet')
vim.api.nvim_buf_set_option(buffer, name, value)
vim.opt.eventignore = eventignore
end
return M

View File

@@ -1,4 +1,5 @@
local misc = require('cmp.utils.misc') local misc = require('cmp.utils.misc')
local opt = require('cmp.utils.options')
local buffer = require('cmp.utils.buffer') local buffer = require('cmp.utils.buffer')
local api = require('cmp.utils.api') local api = require('cmp.utils.api')
local config = require('cmp.config') local config = require('cmp.config')
@@ -37,8 +38,7 @@ window.new = function()
end end
---Set window option. ---Set window option.
---NOTE: If the window already visible, immediately applied to it. The OptionSet ---NOTE: If the window already visible, immediately applied to it.
-- event is not triggered
---@param key string ---@param key string
---@param value any ---@param value any
window.option = function(self, key, value) window.option = function(self, key, value)
@@ -52,16 +52,12 @@ window.option = function(self, key, value)
self.opt[key] = value self.opt[key] = value
if self:visible() then if self:visible() then
local eventignore = vim.opt.eventignore:get() opt.win_set_option(self.win, key, value)
vim.opt.eventignore:append("OptionSet")
vim.api.nvim_win_set_option(self.win, key, value)
vim.opt.eventignore = eventignore
end end
end end
---Set buffer option. ---Set buffer option.
---NOTE: If the buffer already visible, immediately applied to it. The OptionSet ---NOTE: If the buffer already visible, immediately applied to it.
-- event is not triggered.
---@param key string ---@param key string
---@param value any ---@param value any
window.buffer_option = function(self, key, value) window.buffer_option = function(self, key, value)
@@ -76,10 +72,7 @@ window.buffer_option = function(self, key, value)
self.buffer_opt[key] = value self.buffer_opt[key] = value
local existing_buf = buffer.get(self.name) local existing_buf = buffer.get(self.name)
if existing_buf then if existing_buf then
local eventignore = vim.opt.eventignore:get() opt.buf_set_option(existing_buf, key, value)
vim.opt.eventignore:append("OptionSet")
vim.api.nvim_buf_set_option(existing_buf, key, value)
vim.opt.eventignore = eventignore
end end
end end
@@ -107,7 +100,7 @@ window.get_buffer = function(self)
local buf, created_new = buffer.ensure(self.name) local buf, created_new = buffer.ensure(self.name)
if created_new then if created_new then
for k, v in pairs(self.buffer_opt) do for k, v in pairs(self.buffer_opt) do
vim.api.nvim_buf_set_option(buf, k, v) opt.buf_set_option(buf, k, v)
end end
end end
return buf return buf
@@ -131,7 +124,7 @@ window.open = function(self, style)
s.noautocmd = true s.noautocmd = true
self.win = vim.api.nvim_open_win(self:get_buffer(), false, s) self.win = vim.api.nvim_open_win(self:get_buffer(), false, s)
for k, v in pairs(self.opt) do for k, v in pairs(self.opt) do
vim.api.nvim_win_set_option(self.win, k, v) opt.win_set_option(self.win, k, v)
end end
end end
self:update() self:update()
@@ -158,7 +151,7 @@ window.update = function(self)
else else
style.noautocmd = true style.noautocmd = true
self.sbar_win = vim.api.nvim_open_win(buffer.ensure(self.name .. 'sbar_buf'), false, style) self.sbar_win = vim.api.nvim_open_win(buffer.ensure(self.name .. 'sbar_buf'), false, style)
vim.api.nvim_win_set_option(self.sbar_win, 'winhighlight', 'EndOfBuffer:PmenuSbar,NormalFloat:PmenuSbar') opt.win_set_option(self.sbar_win, 'winhighlight', 'EndOfBuffer:PmenuSbar,NormalFloat:PmenuSbar')
end end
end end
@@ -180,7 +173,7 @@ window.update = function(self)
else else
style.noautocmd = true style.noautocmd = true
self.thumb_win = vim.api.nvim_open_win(buffer.ensure(self.name .. 'thumb_buf'), false, style) self.thumb_win = vim.api.nvim_open_win(buffer.ensure(self.name .. 'thumb_buf'), false, style)
vim.api.nvim_win_set_option(self.thumb_win, 'winhighlight', 'EndOfBuffer:PmenuThumb,NormalFloat:PmenuThumb') opt.win_set_option(self.thumb_win, 'winhighlight', 'EndOfBuffer:PmenuThumb,NormalFloat:PmenuThumb')
end end
else else
if self.sbar_win and vim.api.nvim_win_is_valid(self.sbar_win) then if self.sbar_win and vim.api.nvim_win_is_valid(self.sbar_win) then