Skip to content

Commit 1e6dcbf

Browse files
committed
fix: fuzzy panic on too many items
1 parent 5b39d83 commit 1e6dcbf

File tree

6 files changed

+165
-54
lines changed

6 files changed

+165
-54
lines changed

Cargo.lock

+130-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ lazy_static = "1.5.0"
1313
frizbee = { git = "https://github.com/saghen/frizbee" }
1414
serde = { version = "1.0.204", features = ["derive"] }
1515
heed = "0.20.3"
16-
mlua = { version = "0.9.9", features = ["module", "luajit"] }
16+
mlua = { version = "0.10.0-rc.1", features = ["module", "luajit"] }

lua/blink/cmp/fuzzy/fuzzy.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub struct FuzzyOptions {
2020
sorts: Vec<String>,
2121
}
2222

23-
impl FromLua<'_> for FuzzyOptions {
24-
fn from_lua(value: LuaValue<'_>, _lua: &'_ Lua) -> LuaResult<Self> {
23+
impl FromLua for FuzzyOptions {
24+
fn from_lua(value: LuaValue, _lua: &'_ Lua) -> LuaResult<Self> {
2525
if let Some(tab) = value.as_table() {
2626
let use_typo_resistance: bool = tab.get("use_typo_resistance").unwrap_or_default();
2727
let use_frecency: bool = tab.get("use_frecency").unwrap_or_default();
@@ -43,7 +43,7 @@ impl FromLua<'_> for FuzzyOptions {
4343
} else {
4444
Err(mlua::Error::FromLuaConversionError {
4545
from: "LuaValue",
46-
to: "FuzzyOptions",
46+
to: "FuzzyOptions".to_string(),
4747
message: None,
4848
})
4949
}
@@ -52,17 +52,12 @@ impl FromLua<'_> for FuzzyOptions {
5252

5353
pub fn fuzzy(
5454
needle: String,
55-
haystack_labels: Vec<String>,
56-
haystack: Vec<LuaTable>,
55+
haystack: Vec<LspItem>,
5756
frecency: &FrecencyTracker,
5857
opts: FuzzyOptions,
5958
) -> Vec<usize> {
60-
let haystack = haystack
61-
.into_iter()
62-
.map(|item| LazyCell::new(move || -> LspItem { item.into() }))
63-
.collect::<Vec<_>>();
64-
6559
let nearby_words: HashSet<String> = HashSet::from_iter(opts.nearby_words.unwrap_or_default());
60+
let haystack_labels = haystack.iter().map(|s| s.label.clone()).collect::<Vec<_>>();
6661

6762
// Fuzzy match with fzrs
6863
let options = frizbee::Options {

lua/blink/cmp/fuzzy/init.lua

+1-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ function fuzzy.filter_items(needle, haystack)
3535
local nearby_words = #nearby_text < 10000 and fuzzy.rust.get_words(nearby_text) or {}
3636

3737
-- perform fuzzy search
38-
local haystack_filter_text = {}
39-
for _, item in ipairs(haystack) do
40-
table.insert(haystack_filter_text, item.label)
41-
end
42-
local matched_indices = fuzzy.rust.fuzzy(needle, haystack_filter_text, haystack, {
38+
local matched_indices = fuzzy.rust.fuzzy(needle, haystack, {
4339
-- each matching char is worth 4 points and it receives a bonus for capitalization, delimiter and prefix
4440
-- so this should generally be good
4541
-- TODO: make this configurable

lua/blink/cmp/fuzzy/lib.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ pub fn destroy_db(_: &Lua, _: ()) -> LuaResult<bool> {
4141
Ok(true)
4242
}
4343

44-
pub fn access(_: &Lua, item: LuaTable) -> LuaResult<bool> {
45-
let item: LspItem = item.into();
44+
pub fn access(_: &Lua, item: LspItem) -> LuaResult<bool> {
4645
let mut frecency_handle = FRECENCY.write().map_err(|_| {
4746
mlua::Error::RuntimeError("Failed to acquire lock for frecency".to_string())
4847
})?;
@@ -55,12 +54,7 @@ pub fn access(_: &Lua, item: LuaTable) -> LuaResult<bool> {
5554

5655
pub fn fuzzy(
5756
_lua: &Lua,
58-
(needle, haystack_filter_text, haystack, opts): (
59-
String,
60-
Vec<String>,
61-
Vec<LuaTable>,
62-
FuzzyOptions,
63-
),
57+
(needle, haystack, opts): (String, Vec<LspItem>, FuzzyOptions),
6458
) -> LuaResult<Vec<u32>> {
6559
let mut frecency_handle = FRECENCY.write().map_err(|_| {
6660
mlua::Error::RuntimeError("Failed to acquire lock for frecency".to_string())
@@ -69,12 +63,10 @@ pub fn fuzzy(
6963
mlua::Error::RuntimeError("Attempted to use frencecy before initialization".to_string())
7064
})?;
7165

72-
Ok(
73-
fuzzy::fuzzy(needle, haystack_filter_text, haystack, frecency, opts)
74-
.into_iter()
75-
.map(|i| i as u32)
76-
.collect(),
77-
)
66+
Ok(fuzzy::fuzzy(needle, haystack, frecency, opts)
67+
.into_iter()
68+
.map(|i| i as u32)
69+
.collect())
7870
}
7971

8072
pub fn get_words(_: &Lua, text: String) -> LuaResult<Vec<String>> {
@@ -86,7 +78,9 @@ pub fn get_words(_: &Lua, text: String) -> LuaResult<Vec<String>> {
8678
.collect())
8779
}
8880

89-
#[mlua::lua_module]
81+
// NOTE: skip_memory_check greatly improves performance
82+
// https://github.com/mlua-rs/mlua/issues/318
83+
#[mlua::lua_module(skip_memory_check)]
9084
fn blink_cmp_fuzzy(lua: &Lua) -> LuaResult<LuaTable> {
9185
let exports = lua.create_table()?;
9286
exports.set("fuzzy", lua.create_function(fuzzy)?)?;

lua/blink/cmp/fuzzy/lsp_item.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@ pub struct LspItem {
88
pub source_id: String,
99
}
1010

11-
impl From<LuaTable<'_>> for LspItem {
12-
fn from(tab: LuaTable) -> Self {
13-
let label: String = tab.get("label").unwrap_or_default();
14-
let kind: u32 = tab.get("kind").unwrap_or_default();
15-
let score_offset: i32 = tab.get("score_offset").unwrap_or(0);
16-
let source_id: String = tab.get("source_id").unwrap_or_default();
11+
impl FromLua for LspItem {
12+
fn from_lua(value: LuaValue, _: &Lua) -> LuaResult<Self> {
13+
if let Some(tab) = value.as_table() {
14+
let label: String = tab.get("label").unwrap_or_default();
15+
let kind: u32 = tab.get("kind").unwrap_or_default();
16+
let score_offset: i32 = tab.get("score_offset").unwrap_or(0);
17+
let source_id: String = tab.get("source_id").unwrap_or_default();
1718

18-
LspItem {
19-
label,
20-
kind,
21-
score_offset,
22-
source_id,
19+
Ok(LspItem {
20+
label,
21+
kind,
22+
score_offset,
23+
source_id,
24+
})
25+
} else {
26+
Err(mlua::Error::FromLuaConversionError {
27+
from: "LuaValue",
28+
to: "LspItem".to_string(),
29+
message: None,
30+
})
2331
}
2432
}
2533
}

0 commit comments

Comments
 (0)