feat: add an option for a function to ignore files (#223)

* refactor: use vim.pesc instead of implementing

* feat: add optional func to ignore in registering

* test: add tests for ignore_register function

* docs: add note for `ignore_register` option

* docs: add note for use case of `ignore_patterns`
This commit is contained in:
JINNOUCHI Yasushi 2024-07-14 23:11:14 +09:00 committed by GitHub
parent 03d43510d0
commit be5add7248
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 61 additions and 6 deletions

View File

@ -1 +1 @@
{"words":["frecency","nvim","frecently","frecent","devicons","Neovim","shada","oldfiles","keymap","smartcase","frecencydelete","frecencyvalidate","frecencyopts","stdpath","substr","unindexed","fdfind","telescopebufferloaded","telescopepathseparator","telescopefrecencyscores","telescopequeryfilter","norl","noet","compl"],"flagWords":[],"language":"en","version":"0.2"}
{"flagWords":[],"words":["frecency","nvim","frecently","frecent","devicons","Neovim","shada","oldfiles","keymap","smartcase","frecencydelete","frecencyvalidate","frecencyopts","stdpath","substr","unindexed","fdfind","telescopebufferloaded","telescopepathseparator","telescopefrecencyscores","telescopequeryfilter","norl","noet","compl","bufnr","nobuflisted","buflisted"],"language":"en","version":"0.2"}

View File

@ -443,9 +443,31 @@ Default:
for Windows: `{ [[*.git\*]], [[*\tmp\*]], "term://*" }`
Type: `string`
Patterns in this table control which files are indexed (and subsequently which
you'll see in the finder results).
Glob patterns in this table control which files are indexed (and subsequently
which you'll see in the finder results).
*telescope-frecency-configuration-ignore_register*
ignore_register ~
Default: `nil`
Type: `fun(bufnr: integer): boolean`
In opening buffers, it registers the filename and timestamps into DB to
calculate frecency scores later. `ignore_register` can take a buffer number
and you can control whether the file should be registered or not.
For example, you can ignore |'nobuflisted'| buffers to register with this below.
>lua
telescope.setup {
extensions = {
frecency = {
ignore_register = function(bufnr)
return not vim.bo[bufnr].buflisted
end,
}
},
}
<
*telescope-frecency-configuration-matcher*
matcher ~
@ -592,6 +614,11 @@ workspace files. It tries in order until it works successfully.
If you like another commands, set them to this option, like
`workspace_scan_cmd = { "find", ".", "-type", "f" }`.
NOTE: When the plugin uses “4. Native Lua code”, it uses
|telescope-frecency-configuration-ignore_patterns| to ignore candidates from
listed entries. With other ways, such as `rg` and `fd`, it does not use the option
because of performance consideration.
*telescope-frecency-configuration-workspaces*
workspaces ~

View File

@ -13,6 +13,7 @@ local os_util = require "frecency.os_util"
---@field filter_delimiter? string default: ":"
---@field hide_current_buffer? boolean default: false
---@field ignore_patterns? string[] default: { "*.git/*", "*/tmp/*", "term://*" }
---@field ignore_register? fun(bufnr: integer): boolean
---@field matcher? "default"|"fuzzy" default: "default"
---@field scoring_function? fun(recency: integer, fzy_score: number): number default: see lua/frecency/config.lua
---@field max_timestamps? integer default: 10
@ -40,6 +41,7 @@ local Config = {}
---@field filter_delimiter string default: ":"
---@field hide_current_buffer boolean default: false
---@field ignore_patterns string[] default: { "*.git/*", "*/tmp/*", "term://*" }
---@field ignore_register? fun(bufnr: integer): boolean default: nil
---@field matcher "default"|"fuzzy" default: "default"
---@field scoring_function fun(recency: integer, fzy_score: number): number default: see lua/frecency/config.lua
---@field max_timestamps integer default: 10
@ -65,6 +67,7 @@ Config.new = function()
filter_delimiter = true,
hide_current_buffer = true,
ignore_patterns = true,
ignore_register = true,
matcher = true,
max_timestamps = true,
path_display = true,

View File

@ -17,14 +17,13 @@ local FS = {}
---@param fs_config? FrecencyFSConfig
---@return FrecencyFS
FS.new = function(fs_config)
local self = setmetatable(
local self= setmetatable(
{ config = vim.tbl_extend("force", { scan_depth = 100 }, fs_config or {}), os_homedir = assert(uv.os_homedir()) },
{ __index = FS }
)
---@param pattern string
self.ignore_regexes = vim.tbl_map(function(pattern)
local escaped = pattern:gsub("[%-%.%+%[%]%(%)%$%^%%%?%*]", "%%%1")
local regex = escaped:gsub("%%%*", ".*"):gsub("%%%?", ".")
local regex = vim.pesc(pattern):gsub("%%%*", ".*"):gsub("%%%?", ".")
return "^" .. regex .. "$"
end, config.ignore_patterns)
return self

View File

@ -113,6 +113,9 @@ end
---@param bufnr integer
---@param epoch? integer
function Frecency:register(bufnr, epoch)
if config.ignore_register and config.ignore_register(bufnr) then
return
end
local path = vim.api.nvim_buf_get_name(bufnr)
if self.buf_registered[bufnr] or not self.fs:is_valid_path(path) then
return

View File

@ -232,6 +232,29 @@ describe("frecency", function()
end)
end)
end)
describe("when ignore_register is set", function()
with_files({ "hoge1.txt", "hoge2.txt" }, {
ignore_register = function(bufnr)
local _, bufname = pcall(vim.api.nvim_buf_get_name, bufnr)
local should_ignore = not not (bufname and bufname:find "hoge2%.txt$")
log.debug { bufnr = bufnr, bufname = bufname, should_ignore = should_ignore }
return should_ignore
end,
}, function(frecency, finder, dir)
local register = make_register(frecency, dir)
local epoch1 = make_epoch "2023-07-29T00:00:00+09:00"
register("hoge1.txt", epoch1)
local epoch2 = make_epoch "2023-07-29T01:00:00+09:00"
register("hoge2.txt", epoch2)
it("ignores the file the func returns true", function()
local results = finder:get_results(nil, make_epoch "2023-07-29T02:00:00+09:00")
assert.are.same({
{ count = 1, path = filepath(dir, "hoge1.txt"), score = 10, timestamps = { epoch1 } },
}, results)
end)
end)
end)
end)
describe("benchmark", function()