diff --git a/lua/frecency/frecency.lua b/lua/frecency/frecency.lua index e12f887b..f9df3861 100644 --- a/lua/frecency/frecency.lua +++ b/lua/frecency/frecency.lua @@ -8,10 +8,11 @@ local WebDevicons = require "frecency.web_devicons" ---@class Frecency ---@field config FrecencyConfig ----@field picker FrecencyPicker ---@field private buf_registered table flag to indicate the buffer is registered to the database. ---@field private database FrecencyDatabase +---@field private finder FrecencyFinder ---@field private fs FrecencyFS +---@field private picker FrecencyPicker ---@field private recency FrecencyRecency local Frecency = {} @@ -55,14 +56,8 @@ Frecency.new = function(opts) show_filter_column = config.show_filter_column, show_scores = config.show_scores, }) - local finder = Finder.new(entry_maker, self.fs) + self.finder = Finder.new(entry_maker, self.fs) self.recency = Recency.new() - self.picker = Picker.new(self.database, finder, self.fs, self.recency, { - default_workspace = config.default_workspace, - filter_delimiter = config.filter_delimiter, - show_unindexed = config.show_unindexed, - workspaces = config.workspaces, - }) return self end @@ -94,6 +89,27 @@ function Frecency:setup() }) end +---@param opts FrecencyPickerOptions +---@return nil +function Frecency:start(opts) + self.picker = Picker.new(self.database, self.finder, self.fs, self.recency, { + default_workspace_tag = self.config.default_workspace, + editing_bufnr = vim.api.nvim_get_current_buf(), + filter_delimiter = self.config.filter_delimiter, + initial_workspace_tag = opts.workspace, + show_unindexed = self.config.show_unindexed, + workspaces = self.config.workspaces, + }) + self.picker:start(opts) +end + +---@param findstart 1|0 +---@param base string +---@return integer|''|string[] +function Frecency:complete(findstart, base) + return self.picker:complete(findstart, base) +end + ---@param force boolean? ---@return nil function Frecency:validate_database(force) @@ -136,7 +152,7 @@ function Frecency:register(bufnr, datetime) local id, inserted = self.database:upsert_files(path) self.database:insert_timestamps(id, datetime) self.database:trim_timestamps(id, self.recency.config.max_count) - if inserted then + if inserted and self.picker then self.picker:discard_results() end self.buf_registered[bufnr] = true diff --git a/lua/frecency/init.lua b/lua/frecency/init.lua index d09da20d..ff0944c0 100644 --- a/lua/frecency/init.lua +++ b/lua/frecency/init.lua @@ -8,12 +8,12 @@ return { end, start = function(opts) if frecency then - frecency.picker:start(opts) + frecency:start(opts) end end, complete = function(findstart, base) if frecency then - return frecency.picker:complete(findstart, base) + return frecency:complete(findstart, base) end end, frecency = function() diff --git a/lua/frecency/picker.lua b/lua/frecency/picker.lua index 8df052e2..0505b0ad 100644 --- a/lua/frecency/picker.lua +++ b/lua/frecency/picker.lua @@ -10,8 +10,10 @@ local uv = vim.loop or vim.uv local Path = require "plenary.path" ---@class FrecencyPickerConfig ----@field default_workspace string +---@field default_workspace_tag string +---@field editing_bufnr integer ---@field filter_delimiter string +---@field initial_workspace_tag string? ---@field show_unindexed boolean ---@field workspaces table @@ -37,7 +39,6 @@ local Path = require "plenary.path" ---@class FrecencyPicker ---@field private config FrecencyPickerConfig ---@field private database FrecencyDatabase ----@field private editing_bufnr integer ---@field private finder FrecencyFinder ---@field private fs FrecencyFS ---@field private lsp_workspaces string[] @@ -57,7 +58,6 @@ Picker.new = function(database, finder, fs, recency, config) local self = setmetatable({ config = config, database = database, - editing_bufnr = 0, finder = finder, fs = fs, lsp_workspaces = {}, @@ -77,9 +77,8 @@ function Picker:start(opts) return self:default_path_display(picker_opts, path) end, }, opts or {}) --[[@as FrecencyPickerOptions]] - self.editing_bufnr = vim.api.nvim_get_current_buf() self.lsp_workspaces = {} - local workspace = self:get_workspace(opts.cwd, opts.workspace) + local workspace = self:get_workspace(opts.cwd, self.config.initial_workspace_tag) log.debug { workspace = workspace, ["self.workspace"] = self.workspace } if vim.tbl_isempty(self.results) or workspace ~= self.workspace then self.workspace = workspace @@ -221,7 +220,7 @@ end ---@return string? function Picker:get_lsp_workspace() if vim.tbl_isempty(self.lsp_workspaces) then - self.lsp_workspaces = vim.api.nvim_buf_call(self.editing_bufnr, vim.lsp.buf.list_workspace_folders) + self.lsp_workspaces = vim.api.nvim_buf_call(self.config.editing_bufnr, vim.lsp.buf.list_workspace_folders) end return self.lsp_workspaces[1] end @@ -232,23 +231,24 @@ end function Picker:on_input_filter_cb(picker_opts) local filepath_formatter = self:filepath_formatter(picker_opts) return function(prompt) - local workspace = self.workspace - if prompt ~= "" then - local matched, tag = prompt:match(self.workspace_tag_regex) - picker_opts.prompt = matched and prompt:sub(matched:len() + 1) or prompt - workspace = self:get_workspace(picker_opts.cwd, tag) or self.workspace or self.config.default_workspace + local workspace + local matched, tag = prompt:match(self.workspace_tag_regex) + local opts = { prompt = matched and prompt:sub(matched:len() + 1) or prompt } + if prompt == "" then + workspace = self:get_workspace(picker_opts.cwd, self.config.initial_workspace_tag) + else + workspace = self:get_workspace(picker_opts.cwd, tag or self.config.default_workspace_tag) or self.workspace end - log.debug { workspace = workspace, ["self.workspace"] = self.workspace } if self.workspace ~= workspace then self.workspace = workspace self.results = self:fetch_results(workspace) - picker_opts.updated_finder = self.finder:start(filepath_formatter, self.results, { + opts.updated_finder = self.finder:start(filepath_formatter, self.results, { initial_results = self.results, need_scandir = self.workspace and self.config.show_unindexed and true or false, workspace = self.workspace, }) end - return picker_opts + return opts end end diff --git a/lua/frecency/tests/finder_spec.lua b/lua/frecency/tests/finder_spec.lua index 69d0992b..868b7436 100644 --- a/lua/frecency/tests/finder_spec.lua +++ b/lua/frecency/tests/finder_spec.lua @@ -14,19 +14,9 @@ local util = require "frecency.tests.util" local function with_files(files, opts, callback) opts = vim.tbl_extend("force", { ignore_patterns = {}, - __files = { - "lua/hoge/fuga.lua", - "lua/hoge/hoho.lua", - "lua/hoge/fufu.lua", - "lua/hogehoge.lua", - "lua/fugafuga.lua", - }, - __clear_db = true, + filter_delimiter = ":", }, opts or {}) local dir, close = util.make_tree(files) - if opts.__clear_db then - dir:joinpath("file_frecency.sqlite3"):rm() - end opts.root = dir local fs = FS.new(opts) local database = Database.new(fs, opts) diff --git a/lua/frecency/tests/frecency_spec.lua b/lua/frecency/tests/frecency_spec.lua index 9f269099..23121441 100644 --- a/lua/frecency/tests/frecency_spec.lua +++ b/lua/frecency/tests/frecency_spec.lua @@ -1,5 +1,6 @@ ---@diagnostic disable: invisible local Frecency = require "frecency.frecency" +local Picker = require "frecency.picker" local util = require "frecency.tests.util" local Path = require "plenary.path" local log = require "plenary.log" @@ -10,6 +11,9 @@ local log = require "plenary.log" local function with_files(files, callback) local dir, close = util.make_tree(files) local frecency = Frecency.new { db_root = dir.filename } + frecency.picker = + ---@diagnostic disable-next-line: missing-fields + Picker.new(frecency.database, frecency.finder, frecency.fs, frecency.recency, { filter_delimiter = ":" }) callback(frecency, dir) close() end