Skip to content

Commit

Permalink
fix: fuzzy panic on too many items
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Oct 24, 2024
1 parent 5b39d83 commit 1e6dcbf
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 54 deletions.
142 changes: 130 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ lazy_static = "1.5.0"
frizbee = { git = "https://github.com/saghen/frizbee" }
serde = { version = "1.0.204", features = ["derive"] }
heed = "0.20.3"
mlua = { version = "0.9.9", features = ["module", "luajit"] }
mlua = { version = "0.10.0-rc.1", features = ["module", "luajit"] }
15 changes: 5 additions & 10 deletions lua/blink/cmp/fuzzy/fuzzy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub struct FuzzyOptions {
sorts: Vec<String>,
}

impl FromLua<'_> for FuzzyOptions {
fn from_lua(value: LuaValue<'_>, _lua: &'_ Lua) -> LuaResult<Self> {
impl FromLua for FuzzyOptions {
fn from_lua(value: LuaValue, _lua: &'_ Lua) -> LuaResult<Self> {
if let Some(tab) = value.as_table() {
let use_typo_resistance: bool = tab.get("use_typo_resistance").unwrap_or_default();
let use_frecency: bool = tab.get("use_frecency").unwrap_or_default();
Expand All @@ -43,7 +43,7 @@ impl FromLua<'_> for FuzzyOptions {
} else {
Err(mlua::Error::FromLuaConversionError {
from: "LuaValue",
to: "FuzzyOptions",
to: "FuzzyOptions".to_string(),
message: None,
})
}
Expand All @@ -52,17 +52,12 @@ impl FromLua<'_> for FuzzyOptions {

pub fn fuzzy(
needle: String,
haystack_labels: Vec<String>,
haystack: Vec<LuaTable>,
haystack: Vec<LspItem>,
frecency: &FrecencyTracker,
opts: FuzzyOptions,
) -> Vec<usize> {
let haystack = haystack
.into_iter()
.map(|item| LazyCell::new(move || -> LspItem { item.into() }))
.collect::<Vec<_>>();

let nearby_words: HashSet<String> = HashSet::from_iter(opts.nearby_words.unwrap_or_default());
let haystack_labels = haystack.iter().map(|s| s.label.clone()).collect::<Vec<_>>();

// Fuzzy match with fzrs
let options = frizbee::Options {
Expand Down
6 changes: 1 addition & 5 deletions lua/blink/cmp/fuzzy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ function fuzzy.filter_items(needle, haystack)
local nearby_words = #nearby_text < 10000 and fuzzy.rust.get_words(nearby_text) or {}

-- perform fuzzy search
local haystack_filter_text = {}
for _, item in ipairs(haystack) do
table.insert(haystack_filter_text, item.label)
end
local matched_indices = fuzzy.rust.fuzzy(needle, haystack_filter_text, haystack, {
local matched_indices = fuzzy.rust.fuzzy(needle, haystack, {
-- each matching char is worth 4 points and it receives a bonus for capitalization, delimiter and prefix
-- so this should generally be good
-- TODO: make this configurable
Expand Down
24 changes: 9 additions & 15 deletions lua/blink/cmp/fuzzy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ pub fn destroy_db(_: &Lua, _: ()) -> LuaResult<bool> {
Ok(true)
}

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

pub fn fuzzy(
_lua: &Lua,
(needle, haystack_filter_text, haystack, opts): (
String,
Vec<String>,
Vec<LuaTable>,
FuzzyOptions,
),
(needle, haystack, opts): (String, Vec<LspItem>, FuzzyOptions),
) -> LuaResult<Vec<u32>> {
let mut frecency_handle = FRECENCY.write().map_err(|_| {
mlua::Error::RuntimeError("Failed to acquire lock for frecency".to_string())
Expand All @@ -69,12 +63,10 @@ pub fn fuzzy(
mlua::Error::RuntimeError("Attempted to use frencecy before initialization".to_string())
})?;

Ok(
fuzzy::fuzzy(needle, haystack_filter_text, haystack, frecency, opts)
.into_iter()
.map(|i| i as u32)
.collect(),
)
Ok(fuzzy::fuzzy(needle, haystack, frecency, opts)
.into_iter()
.map(|i| i as u32)
.collect())
}

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

#[mlua::lua_module]
// NOTE: skip_memory_check greatly improves performance
// https://github.com/mlua-rs/mlua/issues/318
#[mlua::lua_module(skip_memory_check)]
fn blink_cmp_fuzzy(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("fuzzy", lua.create_function(fuzzy)?)?;
Expand Down
30 changes: 19 additions & 11 deletions lua/blink/cmp/fuzzy/lsp_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ pub struct LspItem {
pub source_id: String,
}

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

LspItem {
label,
kind,
score_offset,
source_id,
Ok(LspItem {
label,
kind,
score_offset,
source_id,
})
} else {
Err(mlua::Error::FromLuaConversionError {
from: "LuaValue",
to: "LspItem".to_string(),
message: None,
})
}
}
}
Expand Down

0 comments on commit 1e6dcbf

Please sign in to comment.