Skip to content

Commit

Permalink
(mini.starter) Update hooks to be called with (content, buf_id) sig…
Browse files Browse the repository at this point in the history
…nature.
  • Loading branch information
echasnovski committed Oct 21, 2022
1 parent a57bd4b commit 98fa037
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Introduction of new module.
## mini.starter

- Item evaluation is now prepended with query reset, as it is rarely needed any more (#105).
- All hooks are now called with `(content, buf_id)` signature allowing them properly use current window layout.

## mini.surround

Expand Down
9 changes: 5 additions & 4 deletions doc/mini-starter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ Key design ideas:
normalized to an array of items. Read about how supplied items are
normalized in |MiniStarter.refresh|.
- Modify the final look by supplying content hooks: functions which take
buffer content as input (see |MiniStarter.get_content()| for more
information) and return buffer content as output. There are
pre-configured content hook generators in |MiniStarter.gen_hook|.
buffer content (see |MiniStarter.get_content()|) and identifier as input
while returning buffer content as output. There are pre-configured
content hook generators in |MiniStarter.gen_hook|.
- Choosing an item can be done in two ways:
- Type prefix query to filter item by matching its name (ignoring
case). Displayed information is updated after every typed character.
Expand Down Expand Up @@ -287,7 +287,8 @@ Refresh Starter buffer
by empty line.
- Last lines contain separate strings of normalized footer.
- Sequentially apply hooks from `MiniStarter.config.content_hooks` to
content. Output of one hook serves as input to the next.
content. All hooks are applied with `(content, buf_id)` signature. Output
of one hook serves as first argument to the next.
- Gather final items from content with |MiniStarter.content_to_items|.
- Convert content to buffer lines with |MiniStarter.content_to_lines| and
add them to buffer.
Expand Down
19 changes: 10 additions & 9 deletions lua/mini/starter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
--- normalized to an array of items. Read about how supplied items are
--- normalized in |MiniStarter.refresh|.
--- - Modify the final look by supplying content hooks: functions which take
--- buffer content as input (see |MiniStarter.get_content()| for more
--- information) and return buffer content as output. There are
--- pre-configured content hook generators in |MiniStarter.gen_hook|.
--- buffer content (see |MiniStarter.get_content()|) and identifier as input
--- while returning buffer content as output. There are pre-configured
--- content hook generators in |MiniStarter.gen_hook|.
--- - Choosing an item can be done in two ways:
--- - Type prefix query to filter item by matching its name (ignoring
--- case). Displayed information is updated after every typed character.
Expand Down Expand Up @@ -359,7 +359,8 @@ end
--- by empty line.
--- - Last lines contain separate strings of normalized footer.
--- - Sequentially apply hooks from `MiniStarter.config.content_hooks` to
--- content. Output of one hook serves as input to the next.
--- content. All hooks are applied with `(content, buf_id)` signature. Output
--- of one hook serves as first argument to the next.
--- - Gather final items from content with |MiniStarter.content_to_items|.
--- - Convert content to buffer lines with |MiniStarter.content_to_lines| and
--- add them to buffer.
Expand Down Expand Up @@ -388,7 +389,7 @@ MiniStarter.refresh = function(buf_id)
local content = H.make_initial_content(data.header, items, data.footer)
local hooks = config.content_hooks or H.default_content_hooks
for _, f in ipairs(hooks) do
content = f(content)
content = f(content, buf_id)
end
data.content = content

Expand Down Expand Up @@ -593,7 +594,7 @@ MiniStarter.gen_hook = {}
MiniStarter.gen_hook.padding = function(left, top)
left = math.max(left or 0, 0)
top = math.max(top or 0, 0)
return function(content)
return function(content, _)
-- Add left padding
local left_pad = string.rep(' ', left)
for _, line in ipairs(content) do
Expand Down Expand Up @@ -663,7 +664,7 @@ MiniStarter.gen_hook.indexing = function(grouping, exclude_sections)
exclude_sections = exclude_sections or {}
local per_section = grouping == 'section'

return function(content)
return function(content, _)
local cur_section, n_section, n_item = nil, 0, 0
local coords = MiniStarter.content_coords(content, 'item')

Expand Down Expand Up @@ -707,7 +708,7 @@ MiniStarter.gen_hook.aligning = function(horizontal, vertical)
local horiz_coef = ({ left = 0, center = 0.5, right = 1.0 })[horizontal]
local vert_coef = ({ top = 0, center = 0.5, bottom = 1.0 })[vertical]

return function(content)
return function(content, _)
local line_strings = MiniStarter.content_to_lines(content)

-- Align horizontally
Expand Down Expand Up @@ -772,7 +773,7 @@ end
--- `c`, use `content[c.line][c.unit]`.
MiniStarter.content_coords = function(content, predicate)
content = content or MiniStarter.get_content()
if predicate == nil then predicate = function(unit) return true end end
if predicate == nil then predicate = function(_) return true end end
if type(predicate) == 'string' then
local pred_type = predicate
predicate = function(unit) return unit.type == pred_type end
Expand Down
16 changes: 16 additions & 0 deletions tests/test_starter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,22 @@ T['refresh()']['calls `content_hooks` on every call'] = function()
eq(child.lua_get('_G.hooks_history'), { 'a', 'b', 'a', 'b' })
end

T['refresh()']['calls `content_hooks` with proper signature'] = function()
child.lua([[MiniStarter.config.content_hooks = {
function(...) local dots = {...}; _G.args = dots; return dots[1] end
}]])
child.lua('MiniStarter.open()')
local buf_id = child.api.nvim_get_current_buf()

-- Hooks should be called with `(content, buf_id)` signature
child.lua('MiniStarter.refresh()')
local args = child.lua_get('_G.args')

eq(#args, 2)
eq(child.lua_get('MiniStarter.get_content()'), args[1])
eq(buf_id, args[2])
end

T['refresh()']['respects `vim.b.ministarter_config`'] = function()
child.lua('MiniStarter.open()')
child.expect_screenshot()
Expand Down

0 comments on commit 98fa037

Please sign in to comment.