Skip to content

Commit a3fc66e

Browse files
committed
filter out deleted file
1 parent 03a0efd commit a3fc66e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lua/frecency/config.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local os_util = require "frecency.os_util"
1919
---@field ignore_patterns? string[] default: { "*.git/*", "*/tmp/*", "term://*" }
2020
---@field ignore_register? fun(bufnr: integer): boolean
2121
---@field matcher? "default"|"fuzzy" default: "default"
22+
---@field remove_non_existent_files? boolean default: true
2223
---@field scoring_function? fun(recency: integer, fzy_score: number): number default: see lua/frecency/config.lua
2324
---@field max_timestamps? integer default: 10
2425
---@field path_display? table default: nil
@@ -54,6 +55,7 @@ local Config = {}
5455
---@field ignore_patterns string[] default: { "*.git/*", "*/tmp/*", "term://*" }
5556
---@field ignore_register? fun(bufnr: integer): boolean default: nil
5657
---@field matcher "default"|"fuzzy" default: "default"
58+
---@field remove_non_existent_files boolean default: true
5759
---@field scoring_function fun(recency: integer, fzy_score: number): number default: see lua/frecency/config.lua
5860
---@field max_timestamps integer default: 10
5961
---@field path_display? table default: nil
@@ -89,6 +91,7 @@ Config.new = function()
8991
max_timestamps = true,
9092
path_display = true,
9193
preceding = true,
94+
remove_non_existent_files = true,
9295
scoring_function = true,
9396
show_filter_column = true,
9497
show_scores = true,
@@ -132,6 +135,7 @@ Config.default_values = {
132135
or { "*.git/*", "*/tmp/*", "term://*" },
133136
matcher = "default",
134137
max_timestamps = 10,
138+
remove_non_existent_files = true,
135139
recency_values = {
136140
{ age = 240, value = 100 }, -- past 4 hours
137141
{ age = 1440, value = 80 }, -- past day
@@ -208,6 +212,7 @@ Config.setup = function(ext_config)
208212
vim.validate("preceding", opts.preceding, function(v)
209213
return v == "opened" or v == "same_repo" or v == nil
210214
end, '"opened" or "same_repo" or nil')
215+
vim.validate("remove_non_existent_files", opts.remove_non_existent_files, "boolean", true)
211216
vim.validate("show_filter_column", opts.show_filter_column, { "boolean", "table" }, true)
212217
vim.validate("show_scores", opts.show_scores, "boolean")
213218
vim.validate("show_unindexed", opts.show_unindexed, "boolean")
@@ -261,6 +266,7 @@ Config.setup = function(ext_config)
261266
end,
262267
'"opened" or "same_repo" or nil',
263268
},
269+
remove_non_existent_files = { opts.remove_non_existent_files, "b", true },
264270
show_filter_column = { opts.show_filter_column, { "b", "t" }, true },
265271
show_scores = { opts.show_scores, "b" },
266272
show_unindexed = { opts.show_unindexed, "b" },

lua/frecency/finder.lua

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,34 @@ function Finder:start(epoch)
114114
async.void(function()
115115
-- NOTE: return to the main loop to show the main window
116116
async.util.scheduler()
117+
118+
-- Collect deleted files to remove from database if enabled
119+
local deleted_files = {}
120+
local check_existence = config.remove_non_existent_files
121+
122+
-- Process database entries
117123
for _, file in ipairs(self:get_results(self.paths, epoch)) do
118124
file.path = os_util.normalize_sep(file.path)
119-
local entry = self.entry_maker(file)
120-
self.tx.send(entry)
125+
126+
if not check_existence or fs.exists(file.path) then
127+
local entry = self.entry_maker(file)
128+
self.tx.send(entry)
129+
else
130+
-- File doesn't exist, schedule it for removal from database
131+
table.insert(deleted_files, file.path)
132+
end
121133
end
134+
122135
self.tx.send(nil)
136+
137+
-- Remove deleted files from database in the background after telescope display is complete
138+
if check_existence and #deleted_files > 0 then
139+
log.debug("Removing " .. #deleted_files .. " deleted files from database")
140+
async.void(function()
141+
self.database:remove_files(deleted_files)
142+
end)()
143+
end
144+
123145
if self.need_scan_dir then
124146
vim
125147
.iter(self.paths)

0 commit comments

Comments
 (0)