feat(explorer): focus on first file when searching in the explorer

This commit is contained in:
Folke Lemaitre 2025-02-04 19:48:48 +01:00
parent 93d5abf133
commit 1d4bea4a9e
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 21 additions and 15 deletions

View file

@ -7,6 +7,7 @@ local Async = require("snacks.picker.util.async")
---@class snacks.picker.matcher.Config
---@field regex? boolean used internally for positions of sources that use regex
---@field on_match? fun(matcher: snacks.picker.Matcher, item: snacks.picker.Item)
---@field on_done? fun(matcher: snacks.picker.Matcher)
---@class snacks.picker.Matcher
---@field opts snacks.picker.matcher.Config
@ -91,6 +92,9 @@ function M:run(picker)
if not (self.sorting or picker.finder.task:running()) then
picker.list.items = picker.finder.items
picker:update({ force = true })
if self.opts.on_done then
self.opts.on_done(self)
end
return
end
@ -157,6 +161,11 @@ function M:run(picker)
until idx >= #picker.finder.items and not picker.finder.task:running()
picker:update({ force = true })
if self.opts.on_done then
vim.schedule(function()
self.opts.on_done(self)
end)
end
end)
end

View file

@ -163,6 +163,18 @@ function M.setup(opts)
end
end
end,
on_done = function()
local picker = ref.value
if not picker or picker.closed then
return
end
for item, idx in picker:iter() do
if not item.dir then
picker.list:view(idx)
return
end
end
end,
},
formatters = {
file = {
@ -265,21 +277,6 @@ function M.search(opts, ctx)
---@async
return function(cb)
cb(root)
-- focus the first non-internal item
ctx.picker.matcher.task:on(
"done",
vim.schedule_wrap(function()
if ctx.picker.closed then
return
end
for item, idx in ctx.picker:iter() do
if not item.internal then
ctx.picker.list:view(idx)
return
end
end
end)
)
---@param item snacks.picker.explorer.Item
local function add(item)