Replies: 1 comment 4 replies
-
|
That's an interesting idea! The -- Make window config that chooses height based on the view in main window
local win_config = function()
local state = MiniPick.get_picker_state()
local is_preview = state ~= nil and state.buffers.preview == vim.api.nvim_win_get_buf(state.windows.main)
return { height = is_preview and 50 or 20 }
end
require('mini.pick').setup({ window = { config = win_config } })
-- Ensure that window is updated every time a new buffer is shown in it.
-- Schedule since state data is not yet updated when the buffer is shown.
local refresh_picker = vim.schedule_wrap(function()
if not MiniPick.is_picker_active() then return end
MiniPick.refresh()
end)
vim.api.nvim_create_autocmd('BufWinEnter', { callback = refresh_picker })Couple of notes:
The On a side note, I am very surprised by the high quality of your approaches and this write-up itself. Very nice 👍 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Contributing guidelines
Module(s)
mini.pick
Question
What I'm trying to accomplish: I want to create a custom action that previews the current match when
view_state ~= "preview"(i.e. not a toggle) and increase window size of the picker, then decrease back to normal after exiting previewI eventually got this work, but it involved modifying some code in 'mini.pick' to allow custom action callbacks to accept a
pickerargument the same way the builtins do. (see dropdown 4)first iteration looks like this:
minipick_preview_grow.mp4
Is there an extremely simple solution to this that I'm missing?
first try - set callable `window.config` in config
The idea here was to see if a
MiniFiles.refresh()is called after the preview is shown in the window. Didn't worksecond try - autocmd that detects `BufWinEnter` for preview buf
The idea here was to call
MiniPick.refresh()after the preview buffer is loaded into the window, but that didn't work either. After some testing it seems thatpicker.view_stateandpicker.buffers.previeware not set until after the event has fired. I tried to show what I mean in the video below:minipick_bufwinenter.mp4
You can see that the value stored in
pickers.buffers.previewis behind what's currently in the windowthird try - create custom action and manually set win config
So here, I looked at the source code for the builtin
toggle_previewaction and basically lsp goto definition'd my way through the call stack and created a custom action that did all the same things:Of course, as opposed to the
H.*helpers in the actual 'mini.pick', thepickerargument isn't actually a reference to the picker, but just a copy of what's returned byMiniPick.get_picker_state(). So while I could modify the size of the window in this custom action, the core issue was that, when callingMiniPick.default_preview(),picker.buffers.previewandpicker.view_stateare not actually set in the internal picker object, meaning there's some behavior that's not quite right for example the input prompt does not switch to the filename as it does for the builtin preview action, whereas the builtintoggle_previewhas access to a reference to the realpicker.This leads to the issue that, even though I
MiniPick.default_preview(), the picker is not in the normal "preview" state where it has a validpicker.buffers.previewfieldcustom_action_test.mp4
fourth try - solved, but had to modify `H.normalize_mappings`
So, after coming to the conclusion that what I want to happen is for my custom action to also be able to set
view_state = "preview"and set a buffer number inpicker.buffers.previewjust like the builtin action.I added an additional argument
pickertoH.normalize_mappingsso that custom actions may also have a reference to thepickerpassed. As in, I changed line 2527 toand line 2544 to
Then, everywhere
H.normalize_mappingswas called, I passed the picker as an additional argumentThis allowed me to write the following custom action (note that the
pickerpassed to custom actions is now a true reference):where now I can write the window size logic directly in the
window.configcallable and just callMiniPick.refresh()in my custom actions. So it finally works as I wanted :)This all leads me to a question: why is
pickernot passed as an argument to custom action callbacks the same way it is to the builtin actions?Also, would you be open to accepting this change (passing
pickerreference to custom action callbacks) officially in 'mini.pick'? I think it's a nice feature and allows a lot more customizabilityBeta Was this translation helpful? Give feedback.
All reactions