Skip to content

Commit

Permalink
refactor: separate sorting logic to expand
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Nov 24, 2024
1 parent 872602f commit 28b4ac6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
12 changes: 11 additions & 1 deletion lua/frecency/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local os_util = require "frecency.os_util"
---@field scoring_function? fun(recency: integer, fzy_score: number): number default: see lua/frecency/config.lua
---@field max_timestamps? integer default: 10
---@field path_display? table default: nil
---@field preceding? "opened"|"same_repo" default: nil
---@field show_filter_column? boolean|string[] default: true
---@field show_scores? boolean default: false
---@field show_unindexed? boolean default: true
Expand Down Expand Up @@ -53,6 +54,7 @@ local Config = {}
---@field scoring_function fun(recency: integer, fzy_score: number): number default: see lua/frecency/config.lua
---@field max_timestamps integer default: 10
---@field path_display? table default: nil
---@field preceding? "opened"|"same_repo" default: nil
---@field show_filter_column boolean|string[] default: true
---@field show_scores boolean default: false
---@field show_unindexed boolean default: true
Expand Down Expand Up @@ -81,6 +83,7 @@ Config.new = function()
matcher = true,
max_timestamps = true,
path_display = true,
preceding = true,
scoring_function = true,
show_filter_column = true,
show_scores = true,
Expand Down Expand Up @@ -177,7 +180,7 @@ Config.setup = function(ext_config)
debug = { opts.debug, "b" },
default_workspace = { opts.default_workspace, "s", true },
disable_devicons = { opts.disable_devicons, "b" },
enable_prompt_mappings={opts.enable_prompt_mappings,'b'},
enable_prompt_mappings = { opts.enable_prompt_mappings, "b" },
filter_delimiter = { opts.filter_delimiter, "s" },
hide_current_buffer = { opts.hide_current_buffer, "b" },
ignore_patterns = { opts.ignore_patterns, "t" },
Expand All @@ -195,6 +198,13 @@ Config.setup = function(ext_config)
end,
"positive number",
},
preceding = {
opts.preceding,
function(v)
return v == "opened" or v == "same_repo" or v == nil
end,
'"opened" or "same_repo" or nil',
},
show_filter_column = { opts.show_filter_column, { "b", "t" }, true },
show_scores = { opts.show_scores, "b" },
show_unindexed = { opts.show_unindexed, "b" },
Expand Down
9 changes: 5 additions & 4 deletions lua/frecency/finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local recency = require "frecency.recency"
local log = require "frecency.log"
local timer = require "frecency.timer"
local lazy_require = require "frecency.lazy_require"
local Sorter = require "frecency.sorter"
local Job = lazy_require "plenary.job" --[[@as FrecencyPlenaryJob]]
local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]]

Expand All @@ -25,6 +26,7 @@ local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]]
---@field private seen table<string, boolean>
---@field private process table<string, { obj: VimSystemObj, done: boolean }>
---@field private state FrecencyState
---@field private sorter FrecencySorter
local Finder = {
---@type fun(): string[]?
cmd = (function()
Expand Down Expand Up @@ -81,6 +83,7 @@ Finder.new = function(database, entry_maker, need_scandir, paths, state, finder_
paths = paths,
process = {},
state = state,
sorter = Sorter.new(),

seen = {},
entries = {},
Expand Down Expand Up @@ -315,11 +318,9 @@ function Finder:get_results(workspaces, epoch)
end
timer.track "making results"

table.sort(files, function(a, b)
return a.score > b.score or (a.score == b.score and a.path > b.path)
end)
local sorted = self.sorter:sort(files)
timer.track "sorting finish"
return files
return sorted
end

function Finder:close()
Expand Down
18 changes: 18 additions & 0 deletions lua/frecency/sorter/default.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---@class FrecencySorterDefault: FrecencySorter
local Default = {}

---@return FrecencySorterDefault
Default.new = function()
return setmetatable({}, { __index = Default })
end

---@param files FrecencyDatabaseEntry[]
---@return FrecencyDatabaseEntry[]
function Default.sort(_, files)
table.sort(files, function(a, b)
return a.score > b.score or (a.score == b.score and a.path > b.path)
end)
return files
end

return Default
10 changes: 10 additions & 0 deletions lua/frecency/sorter/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local config = require "frecency.config"
local Default = require "frecency.sorter.default"
local Opened = require "frecency.sorter.opened"
local SameRepo = require "frecency.sorter.same_repo"

---@class FrecencySorter
---@field new fun(): FrecencySorter
---@field sort fun(self: FrecencySorter, files: FrecencyDatabaseEntry[]): FrecencyDatabaseEntry[]

return config.preceding == "opened" and Opened or config.preceding == "same_repo" and SameRepo or Default
11 changes: 11 additions & 0 deletions lua/frecency/sorter/opened.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local Default = require "frecency.sorter.default"

---@class FrecencySorterOpened: FrecencySorter
local Opened = setmetatable({}, { __index = Opened })

---@return FrecencySorterOpened
Opened.new = function()
return setmetatable({}, { __index = Opened })
end

return Opened
11 changes: 11 additions & 0 deletions lua/frecency/sorter/same_repo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local Default = require "frecency.sorter.default"

---@class FrecencySorterSameRepo: FrecencySorterDefault
local SameRepo = setmetatable({}, { __index = Default })

---@return FrecencySorterSameRepo
SameRepo.new = function()
return setmetatable({}, { __index = SameRepo })
end

return SameRepo

0 comments on commit 28b4ac6

Please sign in to comment.