snacks.nvim icon indicating copy to clipboard operation
snacks.nvim copied to clipboard

feature: folder collapse argument when calling explorer.open

Open t1gu1 opened this issue 7 months ago • 5 comments

Did you check the docs?

  • [x] I have read all the snacks.nvim docs

Is your feature request related to a problem? Please describe.

I want open the explorer at the root of the project sometime, but there isn't an easy way. There is already an option when pressing Z key when the Explorer is already open called explorer_close_all.. but i want to open it already collapse with one key instead of doing 2.

Describe the solution you'd like

Be able to open the explorer with a collapse argument set to false by default.

Snacks.explorer.open({collapse = true})

Describe alternatives you've considered

Here what i've done as my workaround. But I hate using nvim_feedkeys like that and I see a little delay/shift because folders collapse after the explorer appear.

-- Explorer
map("n", "<leader>e", function()
  Snacks.explorer.open()
end, { desc = "Explorer - Reveal active file" })

map("n", "<leader>E", function()
  Snacks.explorer.open()

  local timer = vim.loop.new_timer()
  local start_up_func = function()
    vim.api.nvim_feedkeys("Z", "m", false)
  end
  if timer ~= nil then
    timer:start(1, 0, vim.schedule_wrap(start_up_func))
  end
end, { desc = "Explorer - Reveal root project" })

Additional context

Here is a link to the code source where the existing explorer_close_all already kinda exist https://github.com/folke/snacks.nvim/blob/bc0630e43be5699bb94dadc302c0d21615421d93/lua/snacks/explorer/actions.lua#L124

t1gu1 avatar May 22 '25 05:05 t1gu1

I though for a moment that I could do something like that

Snacks.explorer.open({
    on_show = function(picker)
      picker:action("explorer_close_all")
    end,
})

But the action "explorer_close_all" doesn't exist. Anyways, it im not sure if doing it that way would still make it "delay/shift" the ui.

t1gu1 avatar May 22 '25 06:05 t1gu1

You can try

    {
      "<leader>fe",
      function()
        Snacks.explorer({
          follow_file = false,
          on_show = function(picker)
            require("snacks.explorer.actions").actions.explorer_close_all(picker)
          end,
        })
      end,
      desc = "Explorer Snacks (root dir)",
    },

I believe this should do what you want. follow_file = false is needed so that it doesn't expand the tree.

dpetka2001 avatar May 22 '25 07:05 dpetka2001

Yay, that works fine on my side! Thx!

Even my previous example works now. As you say, follow_file = false, is required.

Here what I got now:

Snacks.explorer.open({
    follow_file = false,
    on_show = function(picker)
      picker:action("explorer_close_all")
    end,
})

I still see a little delay/shift when the explorer open. (because folders collapse after the explorer appear.) But it already feels faster compared with what I had before and the code looks already better.

t1gu1 avatar May 22 '25 08:05 t1gu1

I could close the feature request, but I still think it could be way more intuitive to simply have something like {collapse = true}. Also it would fix the delay/shift when the explorer open. (because folders collapse after the explorer appear.)

I would understand that is not a priority since it works at the end.

t1gu1 avatar May 22 '25 08:05 t1gu1

I'm just a simple user like you and not making any decisions. Leave this open until maintainer gives feedback. I just replied for how you could do that already.

dpetka2001 avatar May 22 '25 08:05 dpetka2001

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jun 22 '25 02:06 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar Jun 29 '25 02:06 github-actions[bot]