Skip to content

Commit

Permalink
fix: reset workspace and reload with the default
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Aug 3, 2023
1 parent 93130ed commit d888275
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
34 changes: 25 additions & 9 deletions lua/frecency/frecency.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ local WebDevicons = require "frecency.web_devicons"

---@class Frecency
---@field config FrecencyConfig
---@field picker FrecencyPicker
---@field private buf_registered table<integer, boolean> 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 = {}

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/frecency/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
28 changes: 14 additions & 14 deletions lua/frecency/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>

Expand All @@ -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[]
Expand All @@ -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 = {},
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
12 changes: 1 addition & 11 deletions lua/frecency/tests/finder_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions lua/frecency/tests/frecency_spec.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -10,6 +11,13 @@ 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 = Picker.new(
frecency.database,
frecency.finder,
frecency.fs,
frecency.recency,
{ editing_bufnr = 0, filter_delimiter = ":", show_unindexed = false, workspaces = {} }
)
callback(frecency, dir)
close()
end
Expand Down

0 comments on commit d888275

Please sign in to comment.