Skip to content

Commit

Permalink
feat(store): introduce nougat store
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Dec 14, 2023
1 parent 2f6820b commit 3f23d3e
Show file tree
Hide file tree
Showing 33 changed files with 661 additions and 415 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ Source: [slanty.lua](examples/slanty.lua)

---

## :art: Color Palette

**[Check Detailed Documentation for `nougat.color`](lua/nougat/color)**

## :gear: NougatBar

The sweet `NougatBar` represents the `statusline` / `tabline` / `winbar`.
Expand All @@ -186,9 +190,9 @@ Separator that goes between two `NougatItem`s.

**[Check Detailed Documentation for `nougat.cache`](lua/nougat/cache)**

## :art: Color Palette
## :gear: Store

**[Check Detailed Documentation for `nougat.color`](lua/nougat/color)**
**[Check Detailed Documentation for `nougat.store`](lua/nougat/store)**

## :peanuts: Nuts

Expand Down
18 changes: 10 additions & 8 deletions lua/nougat/bar/store.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
local register_store = require("nougat.util.store").register
local Store = require("nougat.store").Store

local store = register_store("nougat.bar.store", {
local store = Store("nougat.bar.store", {
statusline = {},
tabline = {},
winbar = {},
}, function(store)
for _, value in pairs(store) do
for key in pairs(value) do
value[key] = nil
}, {
clear = function(store)
for _, val in pairs(store) do
for k in pairs(val) do
val[k] = nil
end
end
end
end)
end,
})

return store
4 changes: 2 additions & 2 deletions lua/nougat/bar/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ end
--luacov: enable

local core = require("nougat.core")
local create_cache_store = require("nougat.cache").create_store
local WinStore = require("nougat.store").WinStore
local store = require("nougat.bar.store")
local on_event = require("nougat.util.on_event")

Expand Down Expand Up @@ -170,7 +170,7 @@ end
---@param bar NougatBar|nougat_bar_selector
local function set_winbar_for_winid(winid, bar)
if not winbar.by_winid then
winbar.by_winid = create_cache_store("win", "nougat.wo.winbar.by_winid")
winbar.by_winid = WinStore("nougat.wo.winbar.by_winid")
end

if winid == 0 then
Expand Down
49 changes: 1 addition & 48 deletions lua/nougat/cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,10 @@ The complex calculations can be done in multiple places:
function. Evaluation time will be affected only when cached value is missing or if
cache value is invalidated.

## `cache.create_store`

_Signature:_ `(type: 'buf'|'win'|'tab', name: string, default_value?: table) -> NougatCacheStore`

The returned `table` is the cache store.

If `type` is `buf`, cache store needs to be indexed with buffer number.

If `type` is `win`, cache store needs to be indexed with window id.

If `type` is `tab`, cache store needs to be indexed with tab id.

The second paramter `name` is the identifier for the cache store. It is usually the
module name of the item for which the cache store is used.

**Example**

```lua
local create_cache_store = require("nougat.cache").create_store

local cache_store = create_store("buf", "nut.dummy", {
modified = false,
})

vim.api.nvim_create_autocmd("BufModifiedSet", {
group = vim.api.nvim_create_augroup("nougat.nut.dummy", { clear = true }),
callback = function(params)
local bufnr = params.buf
vim.schedule(function ()
-- calculate the value (this is just an example)
local modified = vim.api.nvim_buf_get_option(bufnr, 'modified')
-- cache the calculated value
cache_store[bufnr].modified = modified
end)
end,
})

local dummy_item = Item({
content = function(item, ctx)
local cache = cache_store[ctx.bufnr]
if cache.modified then
return "+"
end
end,
})
```
Nougat provides some built-in cache store.

## Buffer Cache

Nougat provides some built-in cache store.

### filetype

```lua
Expand Down
42 changes: 18 additions & 24 deletions lua/nougat/cache/buffer.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
local create_store = require("nougat.cache").create_store
local register_store = require("nougat.util.store").register
local BufStore = require("nougat.store").BufStore
local Store = require("nougat.store").Store
local on_event = require("nougat.util").on_event

local store = register_store("nougat.cache.buffer", {
local store = Store("nougat.cache.buffer", {
---@type table<string, any>
default_value = {},
---@type table<string, boolean>
enabled_key = {},
---@type table<string, (fun(value: string, cache: table, bufnr: integer):nil)[]>
hooks = {},
}, function(store)
for _, value in pairs(store) do
for key in pairs(value) do
value[key] = nil
end
end
end)
})

local default_value = store.default_value

local cache_store = create_store("buf", "nougat.cache.buffer", default_value)
local buf_store = BufStore("nougat.cache.buffer", default_value)

local hooks = store.hooks

Expand All @@ -31,29 +25,29 @@ end

local function get_option_getter(name)
return function(bufnr)
local value = cache_store[bufnr][name]
local value = buf_store[bufnr][name]
if value == nil then
value = vim.bo[bufnr][name]
cache_store[bufnr][name] = value
buf_store[bufnr][name] = value
end
return value
end
end

local get = {
filename = function(bufnr)
local filename = cache_store[bufnr].filename
local filename = buf_store[bufnr].filename
if not filename then
filename = vim.api.nvim_buf_get_name(bufnr)
cache_store[bufnr].filename = filename
buf_store[bufnr].filename = filename
end
return filename
end,
filetype = function(bufnr)
local filetype = cache_store[bufnr].filetype
local filetype = buf_store[bufnr].filetype
if not filetype then
filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
cache_store[bufnr].filetype = filetype
buf_store[bufnr].filetype = filetype
end
return filetype
end,
Expand All @@ -68,7 +62,7 @@ local subscribe = {

on_event({ "BufReadPost", "BufFilePost" }, function(params)
local bufnr, filename = params.buf, params.match
local cache = cache_store[bufnr]
local cache = buf_store[bufnr]

cache.filename = filename

Expand All @@ -80,7 +74,7 @@ local subscribe = {

on_event("FileType", function(params)
local bufnr, filetype = params.buf, params.match
local cache = cache_store[bufnr]
local cache = buf_store[bufnr]

cache.filetype = filetype

Expand All @@ -92,7 +86,7 @@ local subscribe = {

on_event("OptionSet modifiable", function()
local bufnr, modifiable = vim.api.nvim_get_current_buf(), vim.v.option_new
local cache = cache_store[bufnr]
local cache = buf_store[bufnr]

cache.modifiable = modifiable

Expand All @@ -106,7 +100,7 @@ local subscribe = {

on_event("BufModifiedSet", function(params)
local bufnr = params.buf
local cache = cache_store[bufnr]
local cache = buf_store[bufnr]

cache.modified = vim.api.nvim_buf_get_option(bufnr, "modified")

Expand All @@ -118,7 +112,7 @@ local subscribe = {

on_event("OptionSet readonly", function()
local bufnr, readonly = vim.api.nvim_get_current_buf(), vim.v.option_new
local cache = cache_store[bufnr]
local cache = buf_store[bufnr]

cache.readonly = readonly

Expand All @@ -140,7 +134,7 @@ local subscribe = {
if provider == "gitsigns" then
on_event("User GitSignsUpdate", function(params)
local bufnr = params.buf
local cache = cache_store[bufnr]
local cache = buf_store[bufnr]

vim.schedule(function()
local status = vim.fn.getbufvar(bufnr, "gitsigns_status_dict", false)
Expand Down Expand Up @@ -168,7 +162,7 @@ local subscribe = {
}

local mod = {
store = cache_store,
store = buf_store,
}

local enabled_key = store.enabled_key
Expand Down
20 changes: 7 additions & 13 deletions lua/nougat/cache/diagnostic.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
local create_cache_store = require("nougat.cache").create_store
local register_store = require("nougat.util.store").register
local BufStore = require("nougat.store").BufStore
local Store = require("nougat.store").Store
local on_event = require("nougat.util").on_event

local severity = vim.deepcopy(vim.diagnostic.severity)
---@cast severity -nil|unknown
severity.COMBINED = severity.ERROR + severity.WARN + severity.INFO + severity.HINT

local store = register_store("nougat.cache.diagnostic", {
local store = Store("nougat.cache.diagnostic", {
---@type (fun(cache: table, bufnr: integer):nil)[]
on_update_cbs = {},
}, function(store)
for _, value in pairs(store) do
for key in pairs(value) do
value[key] = nil
end
end
end)
})

local cache_store = create_cache_store("buf", "nougat.cache.diagnostic", {
local buf_store = BufStore("nougat.cache.diagnostic", {
[severity.ERROR] = 0,
[severity.WARN] = 0,
[severity.INFO] = 0,
Expand Down Expand Up @@ -52,7 +46,7 @@ local function handle_diagnostic_changed(params)
end
end

local cache = cache_store[bufnr]
local cache = buf_store[bufnr]

if cache[severity.ERROR] ~= error then
cache[severity.ERROR] = error
Expand All @@ -76,7 +70,7 @@ end

local mod = {
severity = severity,
store = cache_store,
store = buf_store,
}

function mod.enable()
Expand Down
Loading

0 comments on commit 3f23d3e

Please sign in to comment.