Skip to content

Commit 5e479c3

Browse files
committed
feat(pick): allow source.preview to set another buffer in main window
1 parent 6977af0 commit 5e479c3

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ There are following change types:
1616

1717
The previous "reset by explicitly setting initial background color" behavior is available by setting the new `opts.explicit_reset` option to `true`.
1818

19+
## mini.pick
20+
21+
### Expand
22+
23+
- Allow `source.preview` to directly set another buffer into picker's main window. The recommended way is still to adjust the provided `buf_id` buffer, but there is now a workaround if this is not reasonably possible.
24+
1925

2026
# Version 0.17.0
2127

doc/mini-pick.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ It will be called with the following arguments:
393393
of item previewing new scratch buffer is be created.
394394
- `item` - item to preview.
395395

396-
It should update buffer `buf_id` to visually represent `item`.
396+
It should update buffer `buf_id` to visually represent `item`. It can also
397+
directly set another buffer in picker's main window, but usually it is more
398+
robust to update given `buf_id` directly.
397399

398400
Example: >lua
399401

lua/mini/pick.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,9 @@
388388
--- of item previewing new scratch buffer is be created.
389389
--- - `item` - item to preview.
390390
---
391-
--- It should update buffer `buf_id` to visually represent `item`.
391+
--- It should update buffer `buf_id` to visually represent `item`. It can also
392+
--- directly set another buffer in picker's main window, but usually it is more
393+
--- robust to update given `buf_id` directly.
392394
---
393395
--- Example: >lua
394396
---
@@ -2959,7 +2961,7 @@ H.picker_show_preview = function(picker)
29592961
vim.bo[buf_id].bufhidden = 'wipe'
29602962
H.set_winbuf(win_id, buf_id)
29612963
preview(buf_id, item)
2962-
picker.buffers.preview = buf_id
2964+
picker.buffers.preview = vim.api.nvim_win_get_buf(win_id)
29632965
picker.view_state = 'preview'
29642966
end
29652967

tests/test_pick.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,13 @@ T['builtin.help()']['works when help window is already opened'] = function()
33503350
eq(#child.api.nvim_list_wins(), 2)
33513351
end
33523352

3353+
T['builtin.help()']['keeps correct picker state'] = function()
3354+
builtin_help()
3355+
type_keys('<Tab>')
3356+
local state = get_picker_state()
3357+
eq(child.api.nvim_win_get_buf(state.windows.main), state.buffers.preview)
3358+
end
3359+
33533360
T['builtin.help()']['can be properly aborted'] = function()
33543361
builtin_help()
33553362
type_keys('<C-c>')
@@ -5514,6 +5521,26 @@ T['Preview']['supports vertical and horizontal scroll'] = function()
55145521
validate('<C-b>')
55155522
end
55165523

5524+
T['Preview']['handles `source.preview` setting buffer directly'] = function()
5525+
local buf_id_other = child.lua([[
5526+
_G.buf_id_other = vim.api.nvim_create_buf(false, true)
5527+
return _G.buf_id_other
5528+
]])
5529+
child.api.nvim_buf_set_lines(buf_id_other, 0, -1, false, { 'Other preview buffer' })
5530+
child.lua_notify([[
5531+
local preview = function()
5532+
local state = MiniPick.get_picker_state()
5533+
vim.api.nvim_win_set_buf(state.windows.main, buf_id_other)
5534+
end
5535+
MiniPick.start({ source = { items = { 'a' }, name = 'Preview other', preview = preview } })
5536+
]])
5537+
type_keys('<Tab>')
5538+
5539+
local state = get_picker_state()
5540+
eq(state.buffers.preview, buf_id_other)
5541+
eq(child.api.nvim_win_get_buf(state.windows.main), buf_id_other)
5542+
end
5543+
55175544
T['Matching'] = new_set()
55185545

55195546
local start_with_items_matchlog = function(items)

0 commit comments

Comments
 (0)