Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(#3119): Expose filter functions to the api#3121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
gracepetryk wants to merge3 commits intonvim-tree:master
base:master
Choose a base branch
Loading
fromgracepetryk:filter-api

Conversation

gracepetryk
Copy link

@gracepetrykgracepetryk commentedMay 6, 2025
edited
Loading

Addresses#3119. Filter methods have all been normalized to accept a singlepath variable so that they may be easily called during a call tocustom. Results are cached based on the path argument during each render loop to avoid computing filters more than once if the filter is enabled and also called incustom.

This enables filter configurations that were not possible before, for example combining filters so that items are only filtered if they match both:

require('nvim-tree').setup({filters= {custom=function (path)localfilter_api=require('nvim-tree.api').filtersreturnfilter_api.git_clean(path)andfilter_api.no_buffer(path)end  }})

@alex-courtis
Copy link
Member

Nice work! I'll get to a proper review / test in the next couple of days.

gracepetryk reacted with heart emoji

@alex-courtis
Copy link
Member

Apologies for the delay, my time is limited right now.

This deserves considerable review and test time which I hope to have in the next few days.

@@ -23,7 +23,7 @@ local function search(search_dir, input_path)
local function iter(dir)
local realpath, path, name, stat, handle, _

local filter_status =explorer.filters:prepare()
explorer.filters:prepare()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Great clean up! All the state is in filters now.

---@field private explorer Explorer
---@field private exclude_list string[] filters.exclude
---@field private ignore_list table<string, boolean> filters.custom string table
---@field private custom_function (fun(absolute_path: string): boolean)|nil filters.custom function
---@field protected status FilterStatus
---@field private filter_cache table<string, FILTER_REASON>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
---@fieldprivatefilter_cache table<string,FILTER_REASON>
---@fieldprivatefilter_cache table<string,FILTER_REASON>
---@fieldprivatecustom fun(self:Filters,path:string):boolean
---@fieldprivatedotfile fun(self:Filters,path:string):boolean
---@fieldprivategit_ignored fun(self:Filters,path:string):boolean
---@fieldprivategit_clean fun(self:Filters,path:string):boolean
---@fieldprivatebuf fun(self:Filters,path:string):boolean
---@fieldprivatebookmark fun(self:Filters,path:string):boolean

This is annoying, but it will keep the language server from complaining about inject-field

You could even wrap them in the constructor, although I'm not sure how clean that would look.

@alex-courtis
Copy link
Member

It's working nicely with a variety of composed filters and tests onpath, withtoggle_custom_filter working as expected.

Unfortunately this one's not updating until we manually refresh:

filters= {enable=true,git_ignored=false,dotfiles=false,git_clean=false,no_buffer=false,no_bookmark=false,custom=function(path)returnpath:match(".*Makefile")andnotapi.filters.no_buffer(path)end,exclude= {},

no_buffer requires theBuf:filter_buffer_ event inExplorer to apply the filter, and it's only fired whenno_buffer = true.

Possible solution: fire the event when the user has a custom function. That's not the best for performance, happy for any other ideas.

@alex-courtis
Copy link
Member

This wasn't you, but we need to change the description of the default mapping Hidden->Custom

vim.keymap.set("n","U",api.tree.toggle_custom_filter,opts("Toggle Filter: Hidden"))

---@param self Filters
---@param path string
---@return FILTER_REASON
---@diagnostic disable:invisible
Copy link
Member

@alex-courtisalex-courtisMay 16, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I've had no success in resolving it. I'll keep at it...

Edit: we can change it to a method so that we may "legally" reference self. This keeps the language server happy:

--- Cache filter function results so subsequent calls to the same path in a loop--- iteration are as fast as possible---@private---@paramfnfun(self:Filters,path:string):boolean---@paramreasonFILTER_REASON---@returnfun(self:Filters,path:string):booleanfunctionFilters:cache_wrapper(fn,reason)---@parampathstring---@returnbooleanlocalfunctioninner(_,path)ifself.filter_cache[reason]==nilthenself.filter_cache[reason]= {}endifself.filter_cache[reason][path]==nilthenself.filter_cache[reason][path]=fn(self,path)endreturnself.filter_cache[reason][path]endreturninnerend

We'll need to move the assignments into the constructor:

self.bookmark=self:cache_wrapper(Filters.bookmark,FILTER_REASON.bookmark)self.custom=self:cache_wrapper(Filters.custom,FILTER_REASON.custom)self.dotfile=self:cache_wrapper(Filters.dotfile,FILTER_REASON.dotfile)self.git_ignored=self:cache_wrapper(Filters.git_ignored,FILTER_REASON.git_ignore)self.git_clean=self:cache_wrapper(Filters.git_clean,FILTER_REASON.git_clean)self.buf=self:cache_wrapper(Filters.buf,FILTER_REASON.buf)self.bookmark=self:cache_wrapper(Filters.bookmark,FILTER_REASON.bookmark)

---@field private explorer Explorer
---@field private exclude_list string[] filters.exclude
---@field private ignore_list table<string, boolean> filters.custom string table
---@field private custom_function (fun(absolute_path: string): boolean)|nil filters.custom function
---@field protected status FilterStatus
---@field private filter_cache table<string, FILTER_REASON>
Copy link
Member

@alex-courtisalex-courtisMay 16, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
---@fieldprivatefilter_cache table<string,FILTER_REASON>
---@fieldprivatefilter_cache table<FILTER_REASON,table<string,boolean>>

@alex-courtis
Copy link
Member

I think we can go one step further with the custom function: pass a node instead of a path, allowing the user to see the type, stat, children etc.

All of the filters could take a node, for consistency and ease of use, as well as aiding any future builtins.

There is extra complexity: we need to supply the user with a sanitised node rather than the actual, as perDecorator. See their cached creation:
https://github.com/gracepetryk/nvim-tree.lua/blob/ea5097a1e2702b4827cb7380e7fa0bd6da87699c/lua/nvim-tree/renderer/builder.lua#L236-L238

Copy link
Member

@alex-courtisalex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is looking fantastic, very straight-forward for the user.

Please

Apologies for the volume of comments; this is a significant change.

@gracepetryk
Copy link
Author

Thanks for the review! I'll have a chance to go over your comments and make changes within in the next few days.

@alex-courtis
Copy link
Member

Any progress on this one@gracepetryk ?

Would you like me to complete this and merge it?

No pressure...

@gracepetryk
Copy link
Author

Hi@alex-courtis, I haven't had the time to work on this I thought it would. If you'd like to complete + merge it go ahead :)

alex-courtis reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@alex-courtisalex-courtisalex-courtis requested changes

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@gracepetryk@alex-courtis

[8]ページ先頭

©2009-2025 Movatter.jp