Skip to content

Commit 7e78236

Browse files
committed
fix: make ffi behave with plugin reloaders
1 parent 79a5262 commit 7e78236

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

lua/noice/util/ffi.lua

+18-21
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
local M = {}
22

3-
---@type ffi.namespace*
4-
local C = nil
5-
6-
function M.setup()
7-
local ffi = require("ffi")
8-
local ok, err = pcall(
9-
ffi.cdef,
10-
[[typedef int32_t RgbValue;
3+
---@return ffi.namespace*
4+
function M.load()
5+
-- Put in a global var to make sure we dont reload
6+
-- when plugin reloaders do their thing
7+
if not _G.noice_C then
8+
local ffi = require("ffi")
9+
local ok, err = pcall(
10+
ffi.cdef,
11+
[[typedef int32_t RgbValue;
1112
typedef struct attr_entry {
1213
int16_t rgb_ae_attr, cterm_ae_attr;
1314
RgbValue rgb_fg_color, rgb_bg_color, rgb_sp_color;
@@ -18,25 +19,21 @@ function M.setup()
1819
void update_screen();
1920
bool cmdpreview;
2021
]]
21-
)
22-
---@diagnostic disable-next-line: need-check-nil
23-
if not ok and not err:find("redefine") then
24-
error(err)
22+
)
23+
---@diagnostic disable-next-line: need-check-nil
24+
if not ok then
25+
error(err)
26+
end
27+
_G.noice_C = ffi.C
2528
end
26-
C = ffi.C
29+
return _G.noice_C
2730
end
2831

2932
return setmetatable(M, {
3033
__index = function(_, key)
31-
if not C then
32-
M.setup()
33-
end
34-
return C[key]
34+
return M.load()[key]
3535
end,
3636
__newindex = function(_, k, v)
37-
if not C then
38-
M.setup()
39-
end
40-
C[k] = v
37+
M.load()[k] = v
4138
end,
4239
})

tests/util/ffi_spec.lua

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local lazy = require("noice.util.lazy")
2+
3+
describe("ffi", function()
4+
it("cmdpreview is false", function()
5+
assert(lazy("noice.util.ffi").cmdpreview == false)
6+
end)
7+
end)

0 commit comments

Comments
 (0)