Skip to content

Commit

Permalink
refactor: use 0.10 feature
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Dec 10, 2024
1 parent 56dea67 commit 9e3300e
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 284 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ jobs:
- macos-latest
- windows-latest
version:
- v0.9.5
- v0.10.0
- v0.10.1
- v0.10.2
- nightly
runs-on: ${{ matrix.os }}
timeout-minutes: 15
Expand Down
3 changes: 3 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
globals = {
"a",
}
98 changes: 53 additions & 45 deletions lua/frecency/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,13 @@ end
---@return string[]
Config.ignore_regexes = function()
if not config.cached_ignore_regexes then
config.cached_ignore_regexes = vim.tbl_map(function(pattern)
local regex = vim.pesc(pattern):gsub("%%%*", ".*"):gsub("%%%?", ".")
return "^" .. regex .. "$"
end, config.ignore_patterns)
config.cached_ignore_regexes = vim
.iter(config.ignore_patterns)
:map(function(pattern)
local regex = vim.pesc(pattern):gsub("%%%*", ".*"):gsub("%%%?", ".")
return "^" .. regex .. "$"
end)
:totable()
end
return config.cached_ignore_regexes
end
Expand All @@ -170,47 +173,52 @@ end
---@return nil
Config.setup = function(ext_config)
local opts = vim.tbl_extend("force", Config.default_values, ext_config or {})
vim.validate {
recency_values = { opts.recency_values, "t" },
auto_validate = { opts.auto_validate, "b" },
bootstrap = { opts.bootstrap, "b" },
db_root = { opts.db_root, "s" },
db_safe_mode = { opts.db_safe_mode, "b" },
db_validate_threshold = { opts.db_validate_threshold, "n" },
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" },
filter_delimiter = { opts.filter_delimiter, "s" },
hide_current_buffer = { opts.hide_current_buffer, "b" },
ignore_patterns = { opts.ignore_patterns, "t" },
matcher = {
opts.matcher,
function(v)
return type(v) == "string" and (v == "default" or v == "fuzzy")
end,
'"default" or "fuzzy"',
},
max_timestamps = {
opts.max_timestamps,
function(v)
return type(v) == "number" and v > 0
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" },
workspace_scan_cmd = { opts.workspace_scan_cmd, { "s", "t" }, true },
workspaces = { opts.workspaces, "t" },
}
if vim.version().minor >= 11 then
vim.validate("recency_values", opts.recency_values, "table")
vim.validate("auto_validate", opts.auto_validate, "boolean")
vim.validate("bootstrap", opts.bootstrap, "boolean")
vim.validate("db_root", opts.db_root, "string")
vim.validate("db_safe_mode", opts.db_safe_mode, "boolean")
vim.validate("db_validate_threshold", opts.db_validate_threshold, "number")
vim.validate("debug", opts.debug, "boolean")
vim.validate("default_workspace", opts.default_workspace, "string", true)
vim.validate("disable_devicons", opts.disable_devicons, "boolean")
vim.validate("enable_prompt_mappings", opts.enable_prompt_mappings, "boolean")
vim.validate("filter_delimiter", opts.filter_delimiter, "string")
vim.validate("hide_current_buffer", opts.hide_current_buffer, "boolean")
vim.validate("ignore_patterns", opts.ignore_patterns, "table")
vim.validate("matcher", opts.matcher, function(v)
return type(v) == "string" and (v == "default" or v == "fuzzy")
end, '"default" or "fuzzy"')
vim.validate("max_timestamps", opts.max_timestamps, function(v)
return type(v) == "number" and v > 0
end, "positive number")
vim.validate("preceding", opts.preceding, function(v)
return v == "opened" or v == "same_repo" or v == nil
end, '"opened" or "same_repo" or nil')
vim.validate("show_filter_column", opts.show_filter_column, { "boolean", "table" }, true)
vim.validate("show_scores", opts.show_scores, "boolean")
vim.validate("show_unindexed", opts.show_unindexed, "boolean")
vim.validate("workspace_scan_cmd", opts.workspace_scan_cmd, { "string", "table" }, true)
vim.validate("workspaces", opts.workspaces, "table")
else
-- TODO: remove this for deprecating 0.10 in the future
vim.validate {
recency_values = { opts.recency_values, "t" },
auto_validate = { opts.auto_validate, "b" },
bootstrap = { opts.bootstrap, "b" },
db_root = { opts.db_root, "s" },
db_safe_mode = { opts.db_safe_mode, "b" },
db_validate_threshold = { opts.db_validate_threshold, "n" },
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" },
filter_delimiter = { opts.filter_delimiter, "s" },
hide_current_buffer = { opts.hide_current_buffer, "b" },
ignore_patterns = { opts.ignore_patterns, "t" },
}
end
config.cached_ignore_regexes = nil
config.ext_config = ext_config
config.values = opts
Expand Down
70 changes: 30 additions & 40 deletions lua/frecency/database.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,35 @@ function Database:insert_files(paths)
if #paths == 0 then
return
end
for _, path in ipairs(paths) do
vim.iter(paths):each(function(path)
self.tbl.records[path] = { count = 1, timestamps = { 0 } }
end
end)
self.watcher_tx.send "save"
end

---@async
---@return string[]
function Database:unlinked_entries()
-- HACK: async.util.join() does not work with empty table. So when the table
-- has no entries, return early.
-- TODO: This is fixed in https://github.com/nvim-lua/plenary.nvim/pull/616
local paths = vim.tbl_keys(self.tbl.records)
return #paths == 0 and {}
or vim.tbl_flatten(async.util.join(vim.tbl_map(function(path)
local threads = vim
.iter(self.tbl.records)
:map(function(path, _)
return function()
local err, realpath = async.uv.fs_realpath(path)
if err or not realpath or realpath ~= path or fs.is_ignored(realpath) then
return path
end
end
end, paths)))
end)
:totable()
return vim.iter(async.util.join(threads)):flatten():totable()
end

---@async
---@param paths string[]
function Database:remove_files(paths)
for _, file in ipairs(paths) do
vim.iter(paths):each(function(file)
self.tbl.records[file] = nil
end
end)
self.watcher_tx.send "save"
end

Expand All @@ -159,11 +158,7 @@ function Database:update(path, epoch)
local now = epoch or os.time()
table.insert(record.timestamps, now)
if #record.timestamps > config.max_timestamps then
local new_table = {}
for i = #record.timestamps - config.max_timestamps + 1, #record.timestamps do
table.insert(new_table, record.timestamps[i])
end
record.timestamps = new_table
record.timestamps = vim.iter(record.timestamps):skip(#record.timestamps - config.max_timestamps):totable()
end
self.tbl.records[path] = record
self.watcher_tx.send "save"
Expand All @@ -175,33 +170,28 @@ end
---@return FrecencyDatabaseEntry[]
function Database:get_entries(workspaces, epoch)
local now = epoch or os.time()
---@param path string
---@return boolean
local function in_workspace(path)
if not workspaces then
return true
end
for _, workspace in ipairs(workspaces) do
if fs.starts_with(path, workspace) then
return true
end
end
return false
end
local items = {}
for path, record in pairs(self.tbl.records) do
if in_workspace(path) then
table.insert(items, {
return vim
.iter(self.tbl.records)
:filter(function(path, _)
return not workspaces
or vim.iter(workspaces):any(function(workspace)
return fs.starts_with(path, workspace)
end)
end)
:map(function(path, record)
return {
path = path,
count = record.count,
ages = vim.tbl_map(function(v)
return (now - v) / 60
end, record.timestamps),
ages = vim
.iter(record.timestamps)
:map(function(timestamp)
return (now - timestamp) / 60
end)
:totable(),
timestamps = record.timestamps,
})
end
end
return items
}
end)
:totable()
end

---@async
Expand Down
33 changes: 12 additions & 21 deletions lua/frecency/entry_maker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ function EntryMaker:create(filepath_formatter, workspaces, workspace_tag)
}

-- set loaded buffers for highlight
self.loaded = {}
local loaded_bufnrs = vim.tbl_filter(function(v)
return vim.api.nvim_buf_is_loaded(v)
end, vim.api.nvim_list_bufs())
for _, bufnr in ipairs(loaded_bufnrs) do
local bufname = vim.api.nvim_buf_get_name(bufnr)
self.loaded = vim.iter(vim.api.nvim_list_bufs()):filter(vim.api.nvim_buf_is_loaded):fold({}, function(a, b)
local bufname = vim.api.nvim_buf_get_name(b)
if bufname then
self.loaded[bufname] = true
a[bufname] = true
end
end
return a
end)

return function(file)
return {
Expand All @@ -69,12 +66,9 @@ function EntryMaker:create(filepath_formatter, workspaces, workspace_tag)
---@type string
local matched
if workspaces then
for _, workspace in ipairs(workspaces) do
if entry.name:find(workspace, 1, true) then
matched = workspace
break
end
end
matched = vim.iter(workspaces):find(function(workspace)
return not not entry.name:find(workspace, 1, true)
end)
end
local items = self:items(entry, matched, workspace_tag, filepath_formatter(matched))
return displayer(items)
Expand Down Expand Up @@ -160,12 +154,9 @@ end
---@param workspace_tag string
---@return integer
function EntryMaker:calculate_filter_column_width(workspaces, workspace_tag)
local longest = ""
for _, workspace in ipairs(workspaces) do
if not longest or #workspace > #longest then
longest = workspace
end
end
local longest = vim.iter(workspaces):fold("", function(a, b)
return #a > #b and a or b
end)
return self:should_show_tail(workspace_tag) and #(utils.path_tail(longest)) + 1
or #(fs.relative_from_home(longest)) + 1
end
Expand All @@ -176,7 +167,7 @@ end
function EntryMaker.should_show_tail(_, workspace_tag)
local show_filter_column = config.show_filter_column
local filters = type(show_filter_column) == "table" and show_filter_column or { "LSP", "CWD" }
return vim.tbl_contains(filters, workspace_tag)
return vim.list_contains(filters, workspace_tag)
end

return EntryMaker
Loading

0 comments on commit 9e3300e

Please sign in to comment.