neo-tree.nvim icon indicating copy to clipboard operation
neo-tree.nvim copied to clipboard

Clear filter on file opened after `filter_on_submit`

Open esfox opened this issue 1 year ago • 3 comments

I assigned a mapping for filter_on_submit but I'd like it so that after opening a file in the filtered results tree, the filter will be cleared. I already have a file_opened event handler like so (closes the tree on open):

{
  event = "file_opened",
  handler = function()
    require("neo-tree.command").execute({ action = "close" })
  end,
}

I tried to add require("neo-tree.command").execute({ action = "clear_filter" }) on the same function but it doesn't seem to work.

esfox avatar Jan 12 '24 17:01 esfox

Here is an example of how to do that with a specific mapping:

https://github.com/nvim-neo-tree/neo-tree.nvim/wiki/Recipes#open-and-clear-search

You can also just extract the clear filter part out to put in your event handler:

    local state = require("neo-tree.sources.manager").get_state("filesystem")
    local cmds = require("neo-tree.sources.filesystem.commands")
    cmds.clear_filter(state)

cseickel avatar Jan 13 '24 15:01 cseickel

Hmm that seems to make it work, but it looks like calling cmds.clear_filter() also opens up the tree. So it doesn't work well in my file_opened event handler such that the tree closes on file open but instantly opens again (due to clear_filter() it seems).

esfox avatar Jan 13 '24 15:01 esfox

Hmm that seems to make it work, but it looks like calling cmds.clear_filter() also opens up the tree. So it doesn't work well in my file_opened event handler such that the tree closes on file open but instantly opens again (due to clear_filter() it seems).

cmds.clear_filter() calls fs.reset_search(state, true) where the second parameter is refresh. I changed the solution to the following and it won't open again:

local state = require("neo-tree.sources.manager").get_state("filesystem")
local fs = require("neo-tree.sources.filesystem")
fs.reset_search(state, false)

nerdroychan avatar Apr 22 '24 17:04 nerdroychan