Skip to content

Commit 05a4af9

Browse files
phanenibhagwan
authored andcommitted
refactor: always setlocal on window options
chore: remove useless autocmd refactor: move logic to win:resized, win:save_size refactor: simplify utils.eventignore
1 parent d6e899e commit 05a4af9

6 files changed

Lines changed: 108 additions & 110 deletions

File tree

lua/fzf-lua/actions.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ M.vimcmd_entry = function(_vimcmd, selected, opts, pcall_vimcmd)
201201
return
202202
end
203203
end
204-
if will_replace_curbuf
205-
and vim.fn.exists("&winfixbuf") == 1
206-
and vim.wo.winfixbuf
204+
if will_replace_curbuf and utils.wo.winfixbuf
207205
then
208206
utils.warn("'winfixbuf' is set for current window, will open in a split.")
209207
vimcmd = "split | " .. vimcmd

lua/fzf-lua/previewer/builtin.lua

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ function TSContext.update(winid, bufnr, opts)
107107
if win and api.nvim_win_is_valid(win) and api.nvim_win_get_config(win).zindex ~= zindex then
108108
api.nvim_win_set_config(win, { zindex = zindex })
109109
-- noautocmd don't ignore WinResized/WinScrolled
110-
if fn.exists("+eventignorewin") == 1 and vim.wo[win][0].eventignorewin == "" then
111-
vim.wo[win][0].eventignorewin = "WinResized"
112-
end
110+
utils.wo[win].eventignorewin = "WinResized"
113111
end
114112
end
115113
api.nvim_win_call(winid, function()
@@ -1053,11 +1051,11 @@ function Previewer.base:attach_snacks_image_inline()
10531051
if not ft then return end
10541052
_G._fzf_lua_snacks_langs = _G._fzf_lua_snacks_langs or simg.langs()
10551053
if not vim.tbl_contains(_G._fzf_lua_snacks_langs, vim.treesitter.language.get_lang(ft)) then
1056-
vim.wo[preview_winid].winblend = self.winblend
1054+
utils.wo[preview_winid].winblend = self.winblend
10571055
return
10581056
end
10591057

1060-
vim.wo[preview_winid].winblend = 0 -- https://github.com/folke/snacks.nvim/pull/1615
1058+
utils.wo[preview_winid].winblend = 0 -- https://github.com/folke/snacks.nvim/pull/1615
10611059
vim.b[bufnr].snacks_image_attached = simg.inline.new(bufnr)
10621060
vim.defer_fn(function()
10631061
self.win:update_preview_scrollbar()
@@ -1121,7 +1119,7 @@ function Previewer.buffer_or_file:do_syntax(entry)
11211119
-- nvim_buf_call has less side-effects than window switch
11221120
-- doautocmd filetypedetect BufRead (vim.filetype.match + ftdetect) + do_modeline
11231121
local ok, _ = pcall(api.nvim_buf_call, bufnr, function()
1124-
utils.eventignore(function() vim.cmd("filetype detect") end, preview_winid, "FileType")
1122+
utils.eventignore(function() vim.cmd("filetype detect") end, "FileType")
11251123
end)
11261124
if not ok then
11271125
utils.warn(("':filetype detect' failed for '%s'"):format(entry.path or "<null>"))
@@ -1172,9 +1170,7 @@ function Previewer.base:maybe_set_cursorline(win, pos)
11721170
vim.api.nvim_win_set_cursor(win, pos)
11731171
cursorline = self.winopts.cursorline
11741172
end
1175-
if cursorline ~= vim.wo[win].cursorline then
1176-
vim.wo[win].cursorline = cursorline
1177-
end
1173+
utils.wo[win].cursorline = cursorline
11781174
end
11791175

11801176
function Previewer.buffer_or_file:set_cursor_hl(entry)
@@ -1211,7 +1207,7 @@ function Previewer.buffer_or_file:set_cursor_hl(entry)
12111207
local lnum, col = tonumber(entry.line), tonumber(entry.col) or 0
12121208
if not lnum or lnum < 1 then
12131209
-- set win option is slow with bigfile
1214-
if vim.wo.cursorline then vim.wo.cursorline = false end
1210+
utils.wo.cursorline = false
12151211
self.orig_pos = { 1, 0 }
12161212
api.nvim_win_set_cursor(self.win.preview_winid, cached_pos or self.orig_pos)
12171213
return

lua/fzf-lua/providers/nvim.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ M.nvim_options = function(opts)
529529
end
530530

531531
M.spell_suggest = function(opts)
532-
-- if not vim.wo.spell then return false end
533532
---@type fzf-lua.config.SpellSuggest
534533
opts = config.normalize_opts(opts, "spell_suggest")
535534
if not opts then return end

lua/fzf-lua/utils.lua

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ M.__HAS_NVIM_0102 = vim.fn.has("nvim-0.10.2") == 1
1515
M.__HAS_NVIM_011 = vim.fn.has("nvim-0.11") == 1
1616
M.__IS_WINDOWS = vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1
1717
-- `:help shellslash` (for more info see #1055)
18-
M.__WIN_HAS_SHELLSLASH = M.__IS_WINDOWS and vim.fn.exists("+shellslash")
18+
M.__WIN_HAS_SHELLSLASH = M.__IS_WINDOWS and vim.fn.exists("+shellslash") == 1
1919

2020
function M.__FILE__() return debug.getinfo(2, "S").source end
2121

@@ -133,10 +133,6 @@ function M.round(num, limit)
133133
return math.floor(num)
134134
end
135135

136-
function M.nvim_has_option(option)
137-
return vim.fn.exists("&" .. option) == 1
138-
end
139-
140136
M._notify_header = "LineNr"
141137

142138
--- Fancy notification wrapper, idea borrowed from blink.nvim
@@ -1153,16 +1149,8 @@ end
11531149

11541150
---@param func function
11551151
---@param scope string?
1156-
---@param win integer?
11571152
---@return ... any
1158-
function M.eventignore(func, win, scope)
1159-
if win and vim.fn.exists("+eventignorewin") == 1 then
1160-
local save_ei = vim.wo[win][0].eventignorewin
1161-
vim.wo[win][0].eventignorewin = scope or "all"
1162-
local ret = { func() }
1163-
vim.wo[win][0].eventignorewin = save_ei
1164-
return unpack(ret)
1165-
end
1153+
function M.eventignore(func, scope)
11661154
local save_ei = vim.o.eventignore
11671155
vim.o.eventignore = scope or "all"
11681156
local ret = { func() }
@@ -1560,4 +1548,28 @@ function M.pid_object(key, opts)
15601548
return Pid:new(key, opts)
15611549
end
15621550

1551+
-- modified version of vim.wo
1552+
-- 1. always setlocal (to avoid potential pollute on win split)
1553+
-- 2. nop on non-exist option
1554+
-- 3. nop if unchange (set win option can be slow #2018)
1555+
local function new_win_opt_accessor(winid)
1556+
return setmetatable({}, {
1557+
__index = function(_, k)
1558+
if type(k) == "number" then return new_win_opt_accessor(k) end
1559+
if vim.fn.exists("+" .. k) == 0 then return end
1560+
return vim.api.nvim_get_option_value(k, { scope = "local", win = winid or 0 })
1561+
end,
1562+
__newindex = function(_, k, v)
1563+
if vim.fn.exists("+" .. k) == 0
1564+
or vim.api.nvim_get_option_value(k, { scope = "local", win = winid or 0 }) == v then
1565+
return
1566+
end
1567+
vim.api.nvim_set_option_value(k, v, { scope = "local", win = winid or 0 })
1568+
end,
1569+
})
1570+
end
1571+
---@type {}|vim.wo
1572+
M.wo = new_win_opt_accessor()
1573+
1574+
15631575
return M

0 commit comments

Comments
 (0)