Skip to content

Commit

Permalink
refactor(bar)!: make callback indexing more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
bekaboo committed Dec 21, 2023
1 parent 053f7f3 commit 3dd2c28
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ should be self-explanatory:
│ │ └─┬─▲──┘ └──────┘ │
│ │ __tostring() │ │ return string cache │
│ │ ┌───▼─┴───┐ ┌──────────────▼──────────────┐
│ │ │dropbar_t├────────────────────▶_G.dropbar.on_click_callbacks
│ │ │dropbar_t├────────────────────▶ _G.dropbar.callbacks
│ │ On update events └───┬─▲───┘ register symbol └──────────────┬──────────────┘
│ │ get_symbols(1, 1000, <cursor>) │ │ on_click() callbacks │
│ └─────────────────────────────────┘ │ ┌──────────┬────▼─────┬─────────┐
Expand Down Expand Up @@ -1844,7 +1844,7 @@ Declared and defined in [`lua/dropbar/bar.lua`](https://github.com/Bekaboo/dropb
It gets symbols<sub>[`dropbar_symbol_t[]`](#dropbar_symbol_t)</sub> from
sources<sub>[`dropbar_source_t`](#dropbar_source_t)</sub> and renders them to a
string. It is also responsible for registering `on_click` callbacks of each
symbol in the global table `_G.dropbar.on_click_callbacks` so that nvim knows
symbol in the global table `_G.dropbar.callbacks` so that nvim knows
which function to call when a symbol is clicked.


Expand Down
4 changes: 2 additions & 2 deletions doc/dropbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ around in their window or clicks at a symbol in the winbar:
│ │ └─┬─▲──┘ └──────┘ │
│ │ __tostring() │ │ return string cache │
│ │ ┌───▼─┴───┐ ┌──────────────▼──────────────┐
│ │ │dropbar_t├────────────────────▶_G.dropbar.on_click_callbacks
│ │ │dropbar_t├────────────────────▶_G.dropbar.callbacks
│ │ On update events └───┬─▲───┘ register symbol └──────────────┬──────────────┘
│ │ get_symbols(1, 1000, <cursor>) │ │ on_click() callbacks │
│ └─────────────────────────────────┘ │ ┌──────────┬────▼─────┬─────────┐
Expand Down Expand Up @@ -1266,7 +1266,7 @@ Declared and defined in `lua/dropbar/bar.lua`.
It gets symbols (|dropbar-developers-classes-dropbar_symbol_t|) from sources
(|dropbar-developers-classes-dropbar_source_t|) and renders them to a string.
It is also responsible for registering `on_click` callbacks of each symbol in
the global table `_G.dropbar.on_click_callbacks` so that nvim knows which
the global table `_G.dropbar.callbacks` so that nvim knows which
function to call when a symbol is clicked.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions lua/dropbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local utils = require('dropbar.utils')
---(v:lua) only support calling global lua functions
---@type table<string, table<string, function>>
---@see dropbar_t:update
_G.dropbar.on_click_callbacks = setmetatable({}, {
_G.dropbar.callbacks = setmetatable({}, {
__index = function(self, buf)
self[buf] = setmetatable({}, {
__index = function(this, win)
Expand Down Expand Up @@ -80,7 +80,7 @@ local function setup(opts)
callback = function(info)
utils.bar.exec('del', { buf = info.buf })
_G.dropbar.bars[info.buf] = nil
_G.dropbar.on_click_callbacks['buf' .. info.buf] = nil
_G.dropbar.callbacks['buf' .. info.buf] = nil
end,
desc = 'Remove dropbar from cache on buffer delete/unload/wipeout.',
})
Expand Down
12 changes: 7 additions & 5 deletions lua/dropbar/bar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ end
---@field sibling_idx integer? index of the symbol in its siblings
---@field range dropbar_symbol_range_t?
---@field on_click fun(this: dropbar_symbol_t, min_width: integer?, n_clicks: integer?, button: string?, modifiers: string?)|false|nil force disable on_click when false
---@field callback_idx integer? idx of the on_click callback in `_G.dropbar.callbacks[buf][win]`, use this to index callback function because `bar_idx` could change after truncate
---@field opts dropbar_symbol_opts_t? options passed to `dropbar_symbol_t:new()` when the symbols is created
---@field data table? any data associated with the symbol
---@field cache table caches string representation, length, etc. for the symbol
Expand Down Expand Up @@ -244,10 +245,10 @@ function dropbar_symbol_t:cat(plain)
self.cache.decorated_str = make_clickable(
icon_highlighted .. name_highlighted,
string.format(
'v:lua.dropbar.on_click_callbacks.buf%s.win%s.fn%s',
'v:lua.dropbar.callbacks.buf%s.win%s.fn%s',
self.bar.buf,
self.bar.win,
self.bar_idx
self.callback_idx
)
)
return self.cache.decorated_str
Expand Down Expand Up @@ -409,7 +410,7 @@ end
---@return nil
function dropbar_t:del()
_G.dropbar.bars[self.buf][self.win] = nil
_G.dropbar.on_click_callbacks['buf' .. self.buf]['win' .. self.win] = nil
_G.dropbar.callbacks['buf' .. self.buf]['win' .. self.win] = nil
for _, component in ipairs(self.components) do
component:del()
end
Expand Down Expand Up @@ -566,11 +567,12 @@ function dropbar_t:update()
component:del()
end
self.components = {}
_G.dropbar.on_click_callbacks['buf' .. self.buf]['win' .. self.win] = {}
_G.dropbar.callbacks['buf' .. self.buf]['win' .. self.win] = {}
for _, source in ipairs(self.sources) do
local symbols = source.get_symbols(self.buf, self.win, cursor)
for _, symbol in ipairs(symbols) do
symbol.bar_idx = #self.components + 1
symbol.callback_idx = symbol.bar_idx
symbol.bar = self
table.insert(self.components, symbol)
-- Register on_click callback for each symbol
Expand All @@ -580,7 +582,7 @@ function dropbar_t:update()
---@param button string mouse button used
---@param modifiers string modifiers used
---@return nil
_G.dropbar.on_click_callbacks['buf' .. self.buf]['win' .. self.win]['fn' .. symbol.bar_idx] = function(
_G.dropbar.callbacks['buf' .. self.buf]['win' .. self.win]['fn' .. symbol.callback_idx] = function(
min_width,
n_clicks,
button,
Expand Down
12 changes: 5 additions & 7 deletions tests/bar_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ describe('[bar]', function()
local start_str = '%##'
local end_str = ' %*'
local sep_str = '%#DropBarIconUISeparator# | %*'
local sym2_str = '%@v:lua.dropbar.on_click_callbacks.buf1.win1000.fn2@%#DropBarIconTest#󰅩 %*%#DropBarNameTest#sym2%*%X'
local sym3_str = '%@v:lua.dropbar.on_click_callbacks.buf1.win1000.fn3@sym3%X'
local sym4_str = '%@v:lua.dropbar.on_click_callbacks.buf1.win1000.fn4@sym4%X'
local sym2_str = '%@v:lua.dropbar.callbacks.buf1.win1000.fn2@%#DropBarIconTest#󰅩 %*%#DropBarNameTest#sym2%*%X'
local sym3_str = '%@v:lua.dropbar.callbacks.buf1.win1000.fn3@sym3%X'
local sym4_str = '%@v:lua.dropbar.callbacks.buf1.win1000.fn4@sym4%X'
-- stylua: ignore end
local representation_str = start_str
.. sep_str
Expand Down Expand Up @@ -222,9 +222,7 @@ describe('[bar]', function()
end, winbar.components)
winbar:del()
assert.is_nil(rawget(_G.dropbar.bars[winbar.buf], winbar.win))
assert.is_nil(
rawget(_G.dropbar.on_click_callbacks[winbar.buf], winbar.win)
)
assert.is_nil(rawget(_G.dropbar.callbacks[winbar.buf], winbar.win))
for _, agent in ipairs(agents) do
assert.spy(agent).was_called()
end
Expand Down Expand Up @@ -270,7 +268,7 @@ describe('[bar]', function()
it('concatenates', function()
assert.are.same('󰅩 sym2', sym2:cat(true))
assert.are.same(
'%@v:lua.dropbar.on_click_callbacks.buf1.win1000.fn2@%#DropBarIconTest#󰅩 %*%#DropBarNameTest#sym2%*%X',
'%@v:lua.dropbar.callbacks.buf1.win1000.fn2@%#DropBarIconTest#󰅩 %*%#DropBarNameTest#sym2%*%X',
sym2:cat()
)
end)
Expand Down

0 comments on commit 3dd2c28

Please sign in to comment.