Skip to content

Commit fae55a0

Browse files
committed
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
1 parent 33f0789 commit fae55a0

File tree

3 files changed

+74
-13
lines changed

3 files changed

+74
-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: 61 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
@@ -28,16 +34,21 @@
2834
--- @alias blink.cmp.SortFunction fun(a: blink.cmp.CompletionItem, b: blink.cmp.CompletionItem): boolean | nil
2935
--- @alias blink.cmp.Sort ("label" | "sort_text" | "kind" | "score" | "exact" | blink.cmp.SortFunction)
3036

37+
local utils = require('blink.cmp.lib.utils')
3138
local validate = require('blink.cmp.config.utils').validate
39+
3240
local fuzzy = {
3341
--- @type blink.cmp.FuzzyConfig
3442
default = {
3543
implementation = 'prefer_rust_with_warning',
3644
max_typos = function(keyword) return math.floor(#keyword / 4) end,
37-
use_frecency = true,
3845
use_proximity = true,
39-
use_unsafe_no_lock = false,
4046
sorts = { 'score', 'sort_text' },
47+
frecency = {
48+
enabled = true,
49+
path = vim.fn.stdpath('state') .. '/blink/cmp/fuzzy.db',
50+
unsafe_no_lock = false,
51+
},
4152
prebuilt_binaries = {
4253
download = true,
4354
ignore_version_mismatch = false,
@@ -53,6 +64,45 @@ local fuzzy = {
5364
}
5465

5566
function fuzzy.validate(config)
67+
-- TODO: Deprecations to be removed in v2.0
68+
if config.use_frecency ~= nil then
69+
utils.notify({
70+
{ 'fuzzy.use_frecency', 'DiagnosticWarn' },
71+
{ ' is deprecated and has been replaced by ' },
72+
{ 'fuzzy.frecency.enabled', 'DiagnosticInfo' },
73+
{ '. Please update your config before version 2.0' },
74+
})
75+
config.frecency.enabled = config.use_frecency
76+
end
77+
if config.use_unsafe_no_lock ~= nil then
78+
utils.notify({
79+
{ 'fuzzy.use_unsafe_no_lock', 'DiagnosticWarn' },
80+
{ ' is deprecated and has been replaced by ' },
81+
{ 'fuzzy.frecency.unsafe_no_lock', 'DiagnosticInfo' },
82+
{ '. Please update your config before version 2.0' },
83+
})
84+
config.frecency.unsafe_no_lock = config.use_unsafe_no_lock
85+
end
86+
-- Migrate the frecency database to its new location if needed
87+
local deprecated_path = vim.fn.stdpath('data') .. '/blink/cmp/fuzzy.db'
88+
if
89+
config.frecency.enabled
90+
and config.frecency.path ~= deprecated_path
91+
and vim.fn.isdirectory(deprecated_path) == 1
92+
then
93+
local fromto_msg = ('from %s to %s'):format(deprecated_path, config.frecency.path)
94+
local has_migrated = false
95+
if vim.fn.mkdir(vim.fn.fnamemodify(config.frecency.path, ':h'), 'p') then
96+
if vim.fn.rename(deprecated_path, config.frecency.path) == 0 then
97+
has_migrated = true
98+
utils.notify({ { 'Successfully migrated frecency database ' .. fromto_msg } })
99+
end
100+
end
101+
if not has_migrated then
102+
utils.notify({ { 'An error occured while migrating frecency database ' .. fromto_msg } })
103+
end
104+
end
105+
56106
validate('fuzzy', {
57107
implementation = {
58108
config.implementation,
@@ -62,9 +112,7 @@ function fuzzy.validate(config)
62112
'one of: "prefer_rust", "prefer_rust_with_warning", "rust", "lua"',
63113
},
64114
max_typos = { config.max_typos, { 'number', 'function' } },
65-
use_frecency = { config.use_frecency, 'boolean' },
66115
use_proximity = { config.use_proximity, 'boolean' },
67-
use_unsafe_no_lock = { config.use_unsafe_no_lock, 'boolean' },
68116
sorts = {
69117
config.sorts,
70118
function(sorts)
@@ -80,9 +128,16 @@ function fuzzy.validate(config)
80128
end,
81129
'one of: "label", "sort_text", "kind", "score", "exact" or a function',
82130
},
131+
frecency = { config.frecency, 'table' },
83132
prebuilt_binaries = { config.prebuilt_binaries, 'table' },
84133
}, config)
85134

135+
validate('fuzzy.frecency', {
136+
enabled = { config.frecency.enabled, 'boolean' },
137+
path = { config.frecency.path, 'string' },
138+
unsafe_no_lock = { config.frecency.unsafe_no_lock, 'boolean' },
139+
}, config.frecency)
140+
86141
validate('fuzzy.prebuilt_binaries', {
87142
download = { config.prebuilt_binaries.download, 'boolean' },
88143
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)