-
-
Notifications
You must be signed in to change notification settings - Fork 624
feat(#2654): filters.custom may be a function #2655
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
b754082
e942899
be1e6e4
d65092d
0e46599
1304c03
89653f3
7978157
7339881
0e880dc
2e4a1d1
5e416f3
b447d69
38ded48
8363ac5
c4b4aae
1e054f3
340c581
8385e89
e771143
c3336b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,30 @@ local function dotfile(path) | |
return M.config.filter_dotfiles and utils.path_basename(path):sub(1, 1) == "." | ||
end | ||
|
||
---@param path string | ||
---@return boolean | ||
local function binary(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, we can't include this OS specific functionality. It will also be a big performance overhead. Fortunately we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not know of this function, thanks! :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One downside to this option is that executable text files, such as shell scripts, will be hidden with this filter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. That is a good use case, however as you say it is a good case for a freely defined user function. |
||
if not M.config.filter_binaries then | ||
return false | ||
end | ||
|
||
-- 4-byte 'magic number' for ELF | ||
local magic_number = string.char(0x7f) .. "ELF" | ||
|
||
local fd = vim.loop.fs_open(path, "r", 438) | ||
if not fd then | ||
return false | ||
end | ||
local stat = vim.loop.fs_fstat(fd) | ||
if not stat then | ||
return false | ||
end | ||
local data = vim.loop.fs_read(fd, 4, 0) | ||
vim.loop.fs_close(fd) | ||
|
||
return data == magic_number | ||
end | ||
|
||
---@param path string | ||
---@param bookmarks table<string, boolean> absolute paths bookmarked | ||
local function bookmark(path, bookmarks) | ||
|
@@ -87,8 +111,14 @@ local function custom(path) | |
-- filter custom regexes | ||
local relpath = utils.path_relative(path, vim.loop.cwd()) | ||
for pat, _ in pairs(M.ignore_list) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With some hacking
I was able to pass the custom function through, however it's never executed as I reckon you'll need to directly call the custom function outside of this loop. |
||
if vim.fn.match(relpath, pat) ~= -1 or vim.fn.match(basename, pat) ~= -1 then | ||
return true | ||
if type(pat) == "function" then | ||
if pat(path) then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's give the user all the information here to allow creation of any sort of filter. We can use Yes, that's a performance hit, however only for users that use a custom filter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to get the node from the path, at the stage the filter is ran? I am not very familiar with this codebase, so apologies if I am misunderstanding something. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will often be null; we just have to test for that and ignore it. |
||
return true | ||
end | ||
else | ||
if vim.fn.match(relpath, pat) ~= -1 or vim.fn.match(basename, pat) ~= -1 then | ||
return true | ||
end | ||
end | ||
end | ||
|
||
|
@@ -136,13 +166,19 @@ function M.should_filter(path, status) | |
return false | ||
end | ||
|
||
return git(path, status.git_status) or buf(path, status.bufinfo) or dotfile(path) or custom(path) or bookmark(path, status.bookmarks) | ||
return git(path, status.git_status) | ||
or buf(path, status.bufinfo) | ||
or dotfile(path) | ||
or binary(path) | ||
or custom(path) | ||
or bookmark(path, status.bookmarks) | ||
end | ||
|
||
function M.setup(opts) | ||
M.config = { | ||
filter_custom = true, | ||
filter_dotfiles = opts.filters.dotfiles, | ||
filter_binaries = opts.filters.binaries, | ||
filter_git_ignored = opts.filters.git_ignored, | ||
filter_git_clean = opts.filters.git_clean, | ||
filter_no_buffer = opts.filters.no_buffer, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be added to
ACCEPTED_TYPES