Skip to content

Commit 29d824d

Browse files
committed
feat(core): use nougat.store
1 parent 1401b52 commit 29d824d

File tree

3 files changed

+85
-74
lines changed

3 files changed

+85
-74
lines changed

lua/nougat/bar/util.lua

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ local statusline = store.statusline
1313
local tabline = store.tabline
1414
local winbar = store.winbar
1515

16+
---@param opts { content: string|nougat_core_expression_fn, id?: string, context?: nougat_core_item_options_context }
17+
local function generator(opts)
18+
return core.generator(opts.content, opts)
19+
end
20+
1621
---@alias nougat_bar_selector (fun(ctx:nougat_core_expression_context):NougatBar)
1722

1823
local option_value_global_opts = { scope = "global" }
@@ -23,89 +28,95 @@ local function get_statusline_width(winid)
2328
or vim.api.nvim_win_get_width(winid)
2429
end
2530

26-
local statusline_generator = core.generator(function(ctx)
27-
ctx.width = get_statusline_width(ctx.winid)
31+
local statusline_generator = {
32+
id = "nougat.go.statusline",
33+
content = function(ctx)
34+
ctx.width = get_statusline_width(ctx.winid)
2835

29-
local select = statusline.select
36+
local select = statusline.select
3037

31-
return vim.api.nvim_win_call(ctx.winid, function()
32-
local stl = type(select) == "function" and select(ctx) or select
33-
return stl and stl:generate(ctx) or ""
34-
end)
35-
end, {
36-
id = "nougat.go.statusline",
38+
return vim.api.nvim_win_call(ctx.winid, function()
39+
local stl = type(select) == "function" and select(ctx) or select
40+
return stl and stl:generate(ctx) or ""
41+
end)
42+
end,
3743
context = {},
38-
})
44+
}
3945

40-
local statusline_by_filetype_generator = core.generator(function(ctx)
41-
ctx.width = get_statusline_width(ctx.winid)
46+
local statusline_by_filetype_generator = {
47+
id = "nougat.wo.statusline.by_filetype",
48+
content = function(ctx)
49+
ctx.width = get_statusline_width(ctx.winid)
4250

43-
local select = statusline.by_filetype[vim.api.nvim_buf_get_option(ctx.bufnr, "filetype")]
51+
local select = statusline.by_filetype[vim.api.nvim_buf_get_option(ctx.bufnr, "filetype")]
4452

45-
return vim.api.nvim_win_call(ctx.winid, function()
46-
local stl = type(select) == "function" and select(ctx) or select
47-
return stl and stl:generate(ctx) or ""
48-
end)
49-
end, {
50-
id = "nougat.wo.statusline.by_filetype",
53+
return vim.api.nvim_win_call(ctx.winid, function()
54+
local stl = type(select) == "function" and select(ctx) or select
55+
return stl and stl:generate(ctx) or ""
56+
end)
57+
end,
5158
context = {},
52-
})
59+
}
5360

54-
local tabline_generator = core.generator(function(ctx)
55-
ctx.width = vim.api.nvim_get_option_value("columns", option_value_global_opts)
61+
local tabline_generator = {
62+
id = "nougat.go.tabline",
63+
content = function(ctx)
64+
ctx.width = vim.api.nvim_get_option_value("columns", option_value_global_opts)
5665

57-
local select = tabline.select
66+
local select = tabline.select
5867

59-
return vim.api.nvim_win_call(ctx.winid, function()
60-
local tal = type(select) == "function" and select(ctx) or select
61-
return tal and tal:generate(ctx) or ""
62-
end)
63-
end, {
64-
id = "nougat.go.tabline",
68+
return vim.api.nvim_win_call(ctx.winid, function()
69+
local tal = type(select) == "function" and select(ctx) or select
70+
return tal and tal:generate(ctx) or ""
71+
end)
72+
end,
6573
context = {},
66-
})
74+
}
6775

68-
local winbar_generator = core.generator(function(ctx)
69-
ctx.width = vim.api.nvim_win_get_width(ctx.winid)
76+
local winbar_generator = {
77+
id = "nougat.go.winbar",
78+
content = function(ctx)
79+
ctx.width = vim.api.nvim_win_get_width(ctx.winid)
7080

71-
local select = winbar.select
81+
local select = winbar.select
7282

73-
return vim.api.nvim_win_call(ctx.winid, function()
74-
local wbr = type(select) == "function" and select(ctx) or select
75-
return wbr and wbr:generate(ctx) or ""
76-
end)
77-
end, {
78-
id = "nougat.go.winbar",
83+
return vim.api.nvim_win_call(ctx.winid, function()
84+
local wbr = type(select) == "function" and select(ctx) or select
85+
return wbr and wbr:generate(ctx) or ""
86+
end)
87+
end,
7988
context = {},
80-
})
89+
}
8190

82-
local winbar_by_filetype_generator = core.generator(function(ctx)
83-
ctx.width = vim.api.nvim_win_get_width(ctx.winid)
91+
local winbar_by_filetype_generator = {
92+
id = "nougat.wo.winbar.by_filetype",
93+
content = function(ctx)
94+
ctx.width = vim.api.nvim_win_get_width(ctx.winid)
8495

85-
local select = winbar.by_filetype[vim.api.nvim_buf_get_option(ctx.bufnr, "filetype")]
96+
local select = winbar.by_filetype[vim.api.nvim_buf_get_option(ctx.bufnr, "filetype")]
8697

87-
return vim.api.nvim_win_call(ctx.winid, function()
88-
local wbr = type(select) == "function" and select(ctx) or select
89-
return wbr and wbr:generate(ctx) or ""
90-
end)
91-
end, {
92-
id = "nougat.wo.winbar.by_filetype",
98+
return vim.api.nvim_win_call(ctx.winid, function()
99+
local wbr = type(select) == "function" and select(ctx) or select
100+
return wbr and wbr:generate(ctx) or ""
101+
end)
102+
end,
93103
context = {},
94-
})
104+
}
95105

96-
local winbar_by_winid_generator = core.generator(function(ctx)
97-
ctx.width = vim.api.nvim_win_get_width(ctx.winid)
106+
local winbar_by_winid_generator = {
107+
id = "nougat.wo.winbar.by_winid",
108+
content = function(ctx)
109+
ctx.width = vim.api.nvim_win_get_width(ctx.winid)
98110

99-
local select = winbar.by_winid[ctx.winid].select
111+
local select = winbar.by_winid[ctx.winid].select
100112

101-
return vim.api.nvim_win_call(ctx.winid, function()
102-
local wbr = type(select) == "function" and select(ctx) or select
103-
return wbr and wbr:generate(ctx) or ""
104-
end)
105-
end, {
106-
id = "nougat.wo.winbar.by_winid",
113+
return vim.api.nvim_win_call(ctx.winid, function()
114+
local wbr = type(select) == "function" and select(ctx) or select
115+
return wbr and wbr:generate(ctx) or ""
116+
end)
117+
end,
107118
context = {},
108-
})
119+
}
109120

110121
---@param filetype string
111122
---@param bar NougatBar|nougat_bar_selector
@@ -119,7 +130,7 @@ local function set_statusline_for_filetype(filetype, bar)
119130
vim.schedule(function()
120131
if vim.api.nvim_buf_is_valid(bufnr) then
121132
vim.api.nvim_buf_call(bufnr, function()
122-
vim.api.nvim_win_set_option(0, "statusline", statusline_by_filetype_generator)
133+
vim.api.nvim_win_set_option(0, "statusline", generator(statusline_by_filetype_generator))
123134
end)
124135
end
125136
end)
@@ -139,7 +150,7 @@ local function set_winbar_local(bar)
139150
return
140151
end
141152

142-
vim.api.nvim_win_set_option(0, "winbar", winbar_generator)
153+
vim.api.nvim_win_set_option(0, "winbar", generator(winbar_generator))
143154
end)
144155
end
145156

@@ -155,7 +166,7 @@ local function set_winbar_for_filetype(filetype, bar)
155166
vim.schedule(function()
156167
if vim.api.nvim_buf_is_valid(bufnr) then
157168
vim.api.nvim_buf_call(bufnr, function()
158-
vim.api.nvim_win_set_option(0, "winbar", winbar_by_filetype_generator)
169+
vim.api.nvim_win_set_option(0, "winbar", generator(winbar_by_filetype_generator))
159170
end)
160171
end
161172
end)
@@ -179,7 +190,7 @@ local function set_winbar_for_winid(winid, bar)
179190

180191
winbar.by_winid[winid].select = bar
181192

182-
vim.api.nvim_set_option_value("winbar", winbar_by_winid_generator, { win = winid, scope = "local" })
193+
vim.api.nvim_set_option_value("winbar", generator(winbar_by_winid_generator), { win = winid, scope = "local" })
183194
end
184195

185196
local mod = {}
@@ -194,7 +205,7 @@ function mod.set_statusline(bar, opts)
194205
else
195206
statusline.select = bar
196207

197-
vim.api.nvim_set_option_value("statusline", statusline_generator, option_value_global_opts)
208+
vim.api.nvim_set_option_value("statusline", generator(statusline_generator), option_value_global_opts)
198209
end
199210
end
200211

@@ -212,7 +223,7 @@ end
212223
function mod.set_tabline(bar)
213224
tabline.select = bar
214225

215-
vim.api.nvim_set_option_value("tabline", tabline_generator, option_value_global_opts)
226+
vim.api.nvim_set_option_value("tabline", generator(tabline_generator), option_value_global_opts)
216227
end
217228

218229
function mod.refresh_tabline()
@@ -230,7 +241,7 @@ function mod.set_winbar(bar, opts)
230241
set_winbar_for_filetype(opts.filetype, bar)
231242
elseif opts.global then
232243
winbar.select = bar
233-
vim.api.nvim_set_option_value("winbar", winbar_generator, option_value_global_opts)
244+
vim.api.nvim_set_option_value("winbar", generator(winbar_generator), option_value_global_opts)
234245
else
235246
set_winbar_local(bar)
236247
end

lua/nougat/core/init.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local Store = require("nougat.store").Store
2+
13
local mod = {}
24

35
--luacheck: push no max line length
@@ -14,7 +16,7 @@ local mod = {}
1416

1517
--luacheck: pop
1618

17-
local fn_storage = {
19+
local fn_storage = Store("nougat.core.fn", {
1820
_next_id = 1,
1921
---@type table<function, integer>
2022
id_by_ref = {},
@@ -24,7 +26,7 @@ local fn_storage = {
2426
ctx_by_id = {},
2527
---@type table<string, integer>
2628
id_by_external_id = {},
27-
}
29+
})
2830

2931
---@param external_id? string
3032
---@return integer fn_id
@@ -33,7 +35,7 @@ local function get_next_fn_id(external_id)
3335
if fn_storage.id_by_external_id[external_id] then
3436
return fn_storage.id_by_external_id[external_id], false
3537
end
36-
local fn_id = fn_storage._next_id
38+
local fn_id = fn_storage._next_id or 1
3739
fn_storage._next_id = fn_id + 1
3840
if external_id then
3941
fn_storage.id_by_external_id[external_id] = fn_id

tests/nougat/core/init_spec.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
pcall(require, "luacov")
22

3+
local core = require("nougat.core")
34
local t = require("tests.util")
45

56
describe("core", function()
6-
---@module 'nougat.core'
7-
local core
87
local ctx
98

109
before_each(function()
11-
package.loaded["nougat.core"] = nil
12-
core = require("nougat.core")
10+
require("nougat.store")._clear()
1311

1412
ctx = t.make_ctx(0)
1513
end)

0 commit comments

Comments
 (0)