Skip to content

Commit

Permalink
hoge
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Jul 23, 2023
1 parent 5fb8c49 commit f8279ad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
4 changes: 3 additions & 1 deletion lua/frecency/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local algo = require "frecency.algo"
local sqlite = require "sqlite"
local p = require "plenary.path"
local s = sqlite.lib
local log = require "frecency.log"

---@class FrecencyDBConfig
---@field db_root string: default "${stdpath.data}"
Expand Down Expand Up @@ -98,7 +99,8 @@ function db.get_files(opts)
end

if opts.ws_path and opts.show_unindexed then
util.include_unindexed(files, opts.ws_path)
--util.include_unindexed(files, opts.ws_path)
log:debug("%s", opts)
end

return files
Expand Down
19 changes: 17 additions & 2 deletions lua/frecency/log.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
---@class FrecencyLog
---@field count integer
---@field dev boolean
local Log = {}

Log.new = function()
return setmetatable({}, { __index = Log })
return setmetatable({ count = 0, dev = false }, { __index = Log })
end

---@param fmt string
---@param args any[]
---@param level integer
function Log:log(fmt, args, level)
vim.notify(("[Telescope-Frecency]: " .. fmt):format(unpack(args)), level)
args = vim.tbl_map(function(v)
return type(v) == "table" and vim.inspect(v, { indent = " ", newline = "" }) or v
end, args)
self.count = self.count + 1
vim.notify(("[Telescope-Frecency: %d]: " .. fmt):format(self.count, unpack(args)), level)
end

---@param fmt string
Expand All @@ -17,4 +24,12 @@ function Log:info(fmt, ...)
self:log(fmt, { ... }, vim.log.levels.INFO)
end

---@param fmt string
---@param ... any
function Log:debug(fmt, ...)
if self.dev then
self:log(fmt, { ... }, vim.log.levels.DEBUG)
end
end

return Log.new()
2 changes: 2 additions & 0 deletions lua/frecency/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local pickers = require "telescope.pickers"
local sorters = require "telescope.sorters"
local ts_util = require "telescope.utils"
local db = require "frecency.db"
local log = require "frecency.log"

local m = {
results = {},
Expand Down Expand Up @@ -66,6 +67,7 @@ end
---@param filter string
---@return boolean
m.update = function(filter)
log:debug("filter: %s", filter or "NONE")
local filter_updated = false
local ws_dir = filter and m.config.workspaces[filter] or nil

Expand Down
22 changes: 12 additions & 10 deletions lua/frecency/pickers.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local EntryMaker = require "frecency.picker.entry_maker"
local log = require "frecency.log"
local actions = require "telescope.actions"
local config_values = require("telescope.config").values
local finders = require "telescope.finders"
Expand Down Expand Up @@ -85,8 +86,9 @@ function Picker:start(opts)
self.editing_bufnr = vim.api.nvim_get_current_buf()
self.lsp_workspaces = {}
self.workspace = self:get_workspace(opts.cwd, opts.workspace)
log:debug("%s", opts)
if vim.tbl_isempty(self.results) then
self.results = self:fetch_results()
self.results = self:fetch_results(self.workspace)
end

local picker = pickers.new(opts, {
Expand Down Expand Up @@ -180,9 +182,11 @@ function Picker:get_workspace(cwd, tag)
end

---@private
---@param workspace string?
---@return FrecencyFile[]
function Picker:fetch_results()
local files = self.database:get_files(self.workspace)
function Picker:fetch_results(workspace)
log:debug("workspace: %s", workspace or "NONE")
local files = self.database:get_files(workspace)
-- NOTE: this might get slower with big db, it might be better to query with db.get_timestamp.
-- TODO: test the above assumption
local timestamps = self.database:get_timestamps()
Expand All @@ -197,14 +201,11 @@ function Picker:fetch_results()
return timestamp.age
end, file_timestamps)
file.score = self.recency:calculate(file.count, ages)
if file.path == vim.fs.normalize "~/git/github.com/delphinus/dotfiles/.config/nvim/lua/lazies/opt.lua" then
vim.print { count = file.count, ages = ages, score = file.score }
end
end

if self.workspace and self.config.show_unindexed then
for name in self.config.fs:scan_dir(self.workspace) do
table.insert(files, { path = vim.fs.joinpath(self.workspace, name), score = 0 })
if workspace and self.config.show_unindexed then
for name in self.config.fs:scan_dir(workspace) do
table.insert(files, { path = vim.fs.joinpath(workspace, name), score = 0 })
end
end

Expand All @@ -231,10 +232,11 @@ function Picker:on_input_filter_cb(prompt, cwd)
local matched, tag = prompt:match(self.workspace_tag_regex)
local opts = { prompt = matched and prompt:sub(matched:len() + 1) or prompt }
local workspace = self:get_workspace(cwd, tag) or self.workspace or self.config.default_workspace
log:debug("%s", { workspace = workspace, ["self.workspace"] = self.workspace })
if self.workspace ~= workspace then
self.workspace = workspace
opts.updated_finder = finders.new_table {
results = self:fetch_results(),
results = self:fetch_results(workspace),
entry_maker = self.entry_maker:create(workspace),
}
end
Expand Down

0 comments on commit f8279ad

Please sign in to comment.