Skip to content

Commit b8154b4

Browse files
soifouSaghen
andauthored
feat(fuzzy): make frecency database path configurable (#2061)
* feat(fuzzy): make frecency database path configurable This adds a new frecency config table with options to enable it, set the database path, and tweak locking. It also deprecates the old boolean flags `fuzzy.use_frecency` and `fuzzy.use_unsafe_no_lock`, showing a heads-up notice when used. ```lua fuzzy = { -- OLD use_frecency = true, use_unsafe_no_lock = false, -- NEW frecency = { enabled = true, unsafe_no_lock = false, } } ``` If an old frecency database directory exists, it will be automatically migrated to the new configurable location, no manual steps needed. Closes #2006 * fix(fuzzy): use `vim.deprecate` for deprecated fields * feat: drop migration, clear deprecated fields --------- Co-authored-by: Liam Dyer <[email protected]>
1 parent c2f25fd commit b8154b4

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

doc/configuration/reference.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,16 +406,22 @@ fuzzy = {
406406

407407
-- Frecency tracks the most recently/frequently used items and boosts the score of the item
408408
-- Note, this does not apply when using the Lua implementation.
409-
use_frecency = true,
409+
frecency = {
410+
-- Whether to enable the frecency feature
411+
enabled = true,
412+
-- Location of the frecency database
413+
path = vim.fn.stdpath('state') .. '/blink/cmp/fuzzy.db',
414+
-- UNSAFE!! When enabled, disables the lock and fsync when writing to the frecency database.
415+
-- This should only be used on unsupported platforms (i.e. alpine termux)
416+
unsafe_no_lock = false,
417+
},
418+
use_frecency = true, -- deprecated alias for frecency.enabled, will be removed in v2.0
419+
use_unsafe_no_lock = false, -- deprecated alias for frecency.unsafe_no_lock, will be removed in v2.0
410420

411421
-- Proximity bonus boosts the score of items matching nearby words
412422
-- Note, this does not apply when using the Lua implementation.
413423
use_proximity = true,
414424

415-
-- UNSAFE!! When enabled, disables the lock and fsync when writing to the frecency database. This should only be used on unsupported platforms (i.e. alpine termux)
416-
-- Note, this does not apply when using the Lua implementation.
417-
use_unsafe_no_lock = false,
418-
419425
-- Controls which sorts to use and in which order, falling back to the next sort if the first one returns nil
420426
-- You may pass a function instead of a string to customize the sorting
421427
sorts = {

lua/blink/cmp/config/fuzzy.lua

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
--- @class (exact) blink.cmp.FuzzyConfig
22
--- @field implementation blink.cmp.FuzzyImplementationType Controls which implementation to use for the fuzzy matcher. See the documentation for the available values for more information.
33
--- @field max_typos number | fun(keyword: string): number Allows for a number of typos relative to the length of the query. Set this to 0 to match the behavior of fzf. Note, this does not apply when using the Lua implementation.
4-
--- @field use_frecency boolean Tracks the most recently/frequently used items and boosts the score of the item. Note, this does not apply when using the Lua implementation.
4+
--- @field use_frecency boolean (deprecated) alias for frecency.enabled, will be removed in v2.0
5+
--- @field use_unsafe_no_lock boolean (deprecated) alias for frecency.unsafe_no_lock, will be removed in v2.0
56
--- @field use_proximity boolean Boosts the score of items matching nearby words. Note, this does not apply when using the Lua implementation.
6-
--- @field use_unsafe_no_lock boolean UNSAFE!! When enabled, disables the lock and fsync when writing to the frecency database. This should only be used on unsupported platforms (i.e. alpine termux). Note, this does not apply when using the Lua implementation.
77
--- @field sorts blink.cmp.Sort[] Controls which sorts to use and in which order.
8+
--- @field frecency blink.cmp.FuzzyFrecencyConfig Tracks the most recently/frequently used items and boosts the score of the item. Note, this does not apply when using the Lua implementation.
89
--- @field prebuilt_binaries blink.cmp.PrebuiltBinariesConfig
910

11+
--- @class (exact) blink.cmp.FuzzyFrecencyConfig
12+
--- @field enabled boolean Whether to enable the frecency feature
13+
--- @field path string Location of the frecency database
14+
--- @field unsafe_no_lock boolean UNSAFE!! When enabled, disables the lock and fsync when writing to the frecency database. This should only be used on unsupported platforms (i.e. alpine termux).
15+
1016
--- @class (exact) blink.cmp.PrebuiltBinariesConfig
1117
--- @field download boolean Whenther or not to automatically download a prebuilt binary from github. If this is set to `false`, you will need to manually build the fuzzy binary dependencies by running `cargo build --release`. Disabled by default when `fuzzy.implementation = 'lua'`
1218
--- @field ignore_version_mismatch boolean Ignores mismatched version between the built binary and the current git sha, when building locally
@@ -29,15 +35,19 @@
2935
--- @alias blink.cmp.Sort ("label" | "sort_text" | "kind" | "score" | "exact" | blink.cmp.SortFunction)
3036

3137
local validate = require('blink.cmp.config.utils').validate
38+
3239
local fuzzy = {
3340
--- @type blink.cmp.FuzzyConfig
3441
default = {
3542
implementation = 'prefer_rust_with_warning',
3643
max_typos = function(keyword) return math.floor(#keyword / 4) end,
37-
use_frecency = true,
3844
use_proximity = true,
39-
use_unsafe_no_lock = false,
4045
sorts = { 'score', 'sort_text' },
46+
frecency = {
47+
enabled = true,
48+
path = vim.fn.stdpath('state') .. '/blink/cmp/fuzzy.db',
49+
unsafe_no_lock = false,
50+
},
4151
prebuilt_binaries = {
4252
download = true,
4353
ignore_version_mismatch = false,
@@ -53,6 +63,18 @@ local fuzzy = {
5363
}
5464

5565
function fuzzy.validate(config)
66+
-- TODO: Deprecations to be removed in v2.0
67+
if config.use_frecency ~= nil then
68+
vim.deprecate('fuzzy.use_frecency', 'fuzzy.frecency.enabled', 'v2.0.0', 'blink-cmp')
69+
config.frecency.enabled = config.use_frecency
70+
config.use_frecency = nil
71+
end
72+
if config.use_unsafe_no_lock ~= nil then
73+
vim.deprecate('fuzzy.use_unsafe_no_lock', 'fuzzy.frecency.unsafe_no_lock', 'v2.0.0', 'blink-cmp')
74+
config.frecency.unsafe_no_lock = config.use_unsafe_no_lock
75+
config.use_unsafe_no_lock = nil
76+
end
77+
5678
validate('fuzzy', {
5779
implementation = {
5880
config.implementation,
@@ -62,9 +84,7 @@ function fuzzy.validate(config)
6284
'one of: "prefer_rust", "prefer_rust_with_warning", "rust", "lua"',
6385
},
6486
max_typos = { config.max_typos, { 'number', 'function' } },
65-
use_frecency = { config.use_frecency, 'boolean' },
6687
use_proximity = { config.use_proximity, 'boolean' },
67-
use_unsafe_no_lock = { config.use_unsafe_no_lock, 'boolean' },
6888
sorts = {
6989
config.sorts,
7090
function(sorts)
@@ -80,9 +100,16 @@ function fuzzy.validate(config)
80100
end,
81101
'one of: "label", "sort_text", "kind", "score", "exact" or a function',
82102
},
103+
frecency = { config.frecency, 'table' },
83104
prebuilt_binaries = { config.prebuilt_binaries, 'table' },
84105
}, config)
85106

107+
validate('fuzzy.frecency', {
108+
enabled = { config.frecency.enabled, 'boolean' },
109+
path = { config.frecency.path, 'string' },
110+
unsafe_no_lock = { config.frecency.unsafe_no_lock, 'boolean' },
111+
}, config.frecency)
112+
86113
validate('fuzzy.prebuilt_binaries', {
87114
download = { config.prebuilt_binaries.download, 'boolean' },
88115
ignore_version_mismatch = { config.prebuilt_binaries.ignore_version_mismatch, 'boolean' },

lua/blink/cmp/fuzzy/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ end
2020
function fuzzy.init_db()
2121
if fuzzy.has_init_db then return end
2222

23-
fuzzy.implementation.init_db(vim.fn.stdpath('data') .. '/blink/cmp/fuzzy.db', config.use_unsafe_no_lock)
23+
fuzzy.implementation.init_db(config.fuzzy.frecency.path, config.fuzzy.frecency.unsafe_no_lock)
2424

2525
vim.api.nvim_create_autocmd('VimLeavePre', {
2626
callback = fuzzy.implementation.destroy_db,
@@ -106,7 +106,7 @@ function fuzzy.fuzzy(line, cursor_col, haystacks_by_provider, range)
106106
local provider_ids = vim.tbl_keys(haystacks_by_provider)
107107
local provider_idxs, matched_indices, scores, exacts = fuzzy.implementation.fuzzy(line, cursor_col, provider_ids, {
108108
max_typos = max_typos,
109-
use_frecency = config.fuzzy.use_frecency and keyword_length > 0,
109+
use_frecency = config.fuzzy.frecency.enabled and keyword_length > 0,
110110
use_proximity = config.fuzzy.use_proximity and keyword_length > 0,
111111
nearby_words = nearby_words,
112112
match_suffix = range == 'full',

0 commit comments

Comments
 (0)