telescope-frecency.nvim/lua/frecency/recency.lua
JINNOUCHI Yasushi 1f32091e2b
refactor!: use OO & add tests (#100)
* I did an overhall for all codes and added typing by Lua-language-server and tests. It also works on CI.
* Now it searches files on the workspace completely asynchronously. It does not block your text input. (Fix #106)
Make count = 1 when you open a file you've never opened (Fix #107)
2023-08-06 16:02:37 +09:00

43 lines
1.1 KiB
Lua

---@class FrecencyRecency
---@field config FrecencyRecencyConfig
---@field private modifier table<integer, { age: integer, value: integer }>
local Recency = {}
---@class FrecencyRecencyConfig
---@field max_count integer default: 10
---@param config FrecencyRecencyConfig?
---@return FrecencyRecency
Recency.new = function(config)
return setmetatable({
config = vim.tbl_extend("force", { max_count = 10 }, config or {}),
modifier = {
{ age = 240, value = 100 }, -- past 4 hours
{ age = 1440, value = 80 }, -- past day
{ age = 4320, value = 60 }, -- past 3 days
{ age = 10080, value = 40 }, -- past week
{ age = 43200, value = 20 }, -- past month
{ age = 129600, value = 10 }, -- past 90 days
},
}, { __index = Recency })
end
---@param count integer
---@param ages number[]
---@return number
function Recency:calculate(count, ages)
local score = 0
for _, age in ipairs(ages) do
for _, rank in ipairs(self.modifier) do
if age <= rank.age then
score = score + rank.value
goto continue
end
end
::continue::
end
return count * score / self.config.max_count
end
return Recency