From be5add7248d32262716a0078b5798e6729b8b7c3 Mon Sep 17 00:00:00 2001 From: JINNOUCHI Yasushi Date: Sun, 14 Jul 2024 23:11:14 +0900 Subject: [PATCH] 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` --- cspell.json | 2 +- doc/telescope-frecency.txt | 31 ++++++++++++++++++++++++++-- lua/frecency/config.lua | 3 +++ lua/frecency/fs.lua | 5 ++--- lua/frecency/init.lua | 3 +++ lua/frecency/tests/frecency_spec.lua | 23 +++++++++++++++++++++ 6 files changed, 61 insertions(+), 6 deletions(-) diff --git a/cspell.json b/cspell.json index 2fd95b9..3496821 100644 --- a/cspell.json +++ b/cspell.json @@ -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"} \ No newline at end of file +{"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"} \ No newline at end of file diff --git a/doc/telescope-frecency.txt b/doc/telescope-frecency.txt index 7b057b2..c25ebb3 100644 --- a/doc/telescope-frecency.txt +++ b/doc/telescope-frecency.txt @@ -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 ~ diff --git a/lua/frecency/config.lua b/lua/frecency/config.lua index 60dfa05..22bd729 100644 --- a/lua/frecency/config.lua +++ b/lua/frecency/config.lua @@ -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, diff --git a/lua/frecency/fs.lua b/lua/frecency/fs.lua index 7b9e1cf..6868839 100644 --- a/lua/frecency/fs.lua +++ b/lua/frecency/fs.lua @@ -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 diff --git a/lua/frecency/init.lua b/lua/frecency/init.lua index e2cbd5a..795d6cf 100644 --- a/lua/frecency/init.lua +++ b/lua/frecency/init.lua @@ -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 diff --git a/lua/frecency/tests/frecency_spec.lua b/lua/frecency/tests/frecency_spec.lua index 1d08198..8aaffa4 100644 --- a/lua/frecency/tests/frecency_spec.lua +++ b/lua/frecency/tests/frecency_spec.lua @@ -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()