feat!: set the default DB path to XDG_STATE_HOME (#204)

* feat: access user opts from config.ext_config

* feat!: set the default DB path to XDG_STATE_HOME

* feat: add fallback logic to detect old DB path

* docs: add note for this change
This commit is contained in:
JINNOUCHI Yasushi 2024-05-25 17:27:08 +09:00 committed by GitHub
parent 08e3ad80fc
commit 865f51a611
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 5 deletions

View File

@ -241,11 +241,17 @@ If `true`, it removes stale entries count over than
*telescope-frecency-configuration-db_root* *telescope-frecency-configuration-db_root*
db_root ~ db_root ~
Default: `vim.fn.stdpath "data"` Default: `vim.fn.stdpath "state"`
Type: `string` Type: `string`
Path to the parent directory of custom database location. Defaults to Path to the parent directory of custom database location. Defaults to
`$XDG_DATA_HOME/nvim` if unset. |$XDG_STATE_HOME|/nvim (see |stdpath()|) if unset.
NOTE: The default value was `vim.fn.stdpath "data"`. If you doesn't set this
option and you have DB in `vim.fn.stdpath "data"` instead of
`vim.fn.stdpath "state"`, it uses the one in the old path for backward
compatibility. See the detail in the issue below.
https://github.com/nvim-telescope/telescope-frecency.nvim/issues/200
*telescope-frecency-configuration-db_safe_mode* *telescope-frecency-configuration-db_safe_mode*
db_safe_mode ~ db_safe_mode ~

View File

@ -1,13 +1,14 @@
local os_util = require "frecency.os_util" local os_util = require "frecency.os_util"
---@class FrecencyConfig: FrecencyRawConfig ---@class FrecencyConfig: FrecencyRawConfig
---@field ext_config FrecencyRawConfig
---@field private values FrecencyRawConfig ---@field private values FrecencyRawConfig
local Config = {} local Config = {}
---@class FrecencyRawConfig ---@class FrecencyRawConfig
---@field recency_values { age: integer, value: integer }[] default: see lua/frecency/config.lua ---@field recency_values { age: integer, value: integer }[] default: see lua/frecency/config.lua
---@field auto_validate boolean default: true ---@field auto_validate boolean default: true
---@field db_root string default: vim.fn.stdpath "data" ---@field db_root string default: vim.fn.stdpath "state"
---@field db_safe_mode boolean default: true ---@field db_safe_mode boolean default: true
---@field db_validate_threshold integer default: 10 ---@field db_validate_threshold integer default: 10
---@field default_workspace? string default: nil ---@field default_workspace? string default: nil
@ -29,7 +30,7 @@ local Config = {}
Config.new = function() Config.new = function()
local default_values = { local default_values = {
auto_validate = true, auto_validate = true,
db_root = vim.fn.stdpath "data", db_root = vim.fn.stdpath "state",
db_safe_mode = true, db_safe_mode = true,
db_validate_threshold = 10, db_validate_threshold = 10,
default_workspace = nil, default_workspace = nil,
@ -85,6 +86,7 @@ Config.new = function()
workspaces = true, workspaces = true,
} }
return setmetatable({ return setmetatable({
ext_config = {},
values = default_values, values = default_values,
}, { }, {
__index = function(self, key) __index = function(self, key)
@ -140,6 +142,7 @@ Config.setup = function(ext_config)
workspace_scan_cmd = { opts.workspace_scan_cmd, { "s", "t" }, true }, workspace_scan_cmd = { opts.workspace_scan_cmd, { "s", "t" }, true },
workspaces = { opts.workspaces, "t" }, workspaces = { opts.workspaces, "t" },
} }
config.ext_config = ext_config
config.values = opts config.values = opts
end end

View File

@ -30,7 +30,20 @@ Database.new = function(fs)
tbl = Table.new(version), tbl = Table.new(version),
version = version, version = version,
}, { __index = Database }) }, { __index = Database })
self.filename = Path.new(config.db_root, "file_frecency.bin").filename self.filename = (function()
-- NOTE: for backward compatibility
-- If the user does not set db_root specifically, search DB in
-- $XDG_DATA_HOME/nvim in addition to $XDG_STATE_HOME/nvim (default value).
local file = "file_frecency.bin"
local db = Path.new(config.db_root, file)
if not config.ext_config.db_root and not db:exists() then
local old_location = Path.new(vim.fn.stdpath "data", file)
if old_location:exists() then
return old_location.filename
end
end
return db.filename
end)()
self.file_lock = FileLock.new(self.filename) self.file_lock = FileLock.new(self.filename)
local rx local rx
self.tx, rx = async.control.channel.mpsc() self.tx, rx = async.control.channel.mpsc()

View File

@ -5,6 +5,7 @@
---@class FrecencyPlenaryPath ---@class FrecencyPlenaryPath
---@field new fun(self: FrecencyPlenaryPath|string, path?: string): FrecencyPlenaryPath ---@field new fun(self: FrecencyPlenaryPath|string, path?: string): FrecencyPlenaryPath
---@field absolute fun(): string ---@field absolute fun(): string
---@field exists fun(self: FrecencyPlenaryPath): boolean
---@field is_file fun(self: FrecencyPlenaryPath): boolean ---@field is_file fun(self: FrecencyPlenaryPath): boolean
---@field filename string ---@field filename string
---@field joinpath fun(self: FrecencyPlenaryPath, ...): FrecencyPlenaryPath ---@field joinpath fun(self: FrecencyPlenaryPath, ...): FrecencyPlenaryPath