From f8279adbccd4dbf21a4289cd3bcdc79ee07eae80 Mon Sep 17 00:00:00 2001 From: delphinus Date: Sun, 23 Jul 2023 20:38:32 +0900 Subject: [PATCH] hoge --- lua/frecency/db.lua | 4 +++- lua/frecency/log.lua | 19 +++++++++++++++++-- lua/frecency/picker.lua | 2 ++ lua/frecency/pickers.lua | 22 ++++++++++++---------- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lua/frecency/db.lua b/lua/frecency/db.lua index 68e94b39..2cf401b4 100644 --- a/lua/frecency/db.lua +++ b/lua/frecency/db.lua @@ -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}" @@ -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 diff --git a/lua/frecency/log.lua b/lua/frecency/log.lua index cac01cf7..eb133e3e 100644 --- a/lua/frecency/log.lua +++ b/lua/frecency/log.lua @@ -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 @@ -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() diff --git a/lua/frecency/picker.lua b/lua/frecency/picker.lua index e8813121..40e48343 100644 --- a/lua/frecency/picker.lua +++ b/lua/frecency/picker.lua @@ -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 = {}, @@ -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 diff --git a/lua/frecency/pickers.lua b/lua/frecency/pickers.lua index 1c7dc92f..1a014f1b 100644 --- a/lua/frecency/pickers.lua +++ b/lua/frecency/pickers.lua @@ -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" @@ -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, { @@ -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() @@ -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 @@ -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