Skip to content

Commit

Permalink
feat: use lossy string for lua -> rust conversion
Browse files Browse the repository at this point in the history
Closes #1000
Closes #1051
  • Loading branch information
Saghen committed Jan 22, 2025
1 parent 0632884 commit 3b4fa80
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
35 changes: 24 additions & 11 deletions lua/blink/cmp/fuzzy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn set_provider_items(

pub fn fuzzy(
_lua: &Lua,
(line, cursor_col, provider_id, opts): (String, usize, String, FuzzyOptions),
(line, cursor_col, provider_id, opts): (mlua::String, usize, String, FuzzyOptions),
) -> LuaResult<(Vec<i32>, Vec<u32>)> {
let mut frecency_handle = FRECENCY.write().map_err(|_| {
mlua::Error::RuntimeError("Failed to acquire lock for frecency".to_string())
Expand All @@ -87,44 +87,57 @@ pub fn fuzzy(
))
})?;

Ok(fuzzy::fuzzy(&line, cursor_col, haystack, frecency, opts))
Ok(fuzzy::fuzzy(
&line.to_string_lossy(),
cursor_col,
haystack,
frecency,
opts,
))
}

pub fn fuzzy_matched_indices(
_lua: &Lua,
(line, cursor_col, haystack, match_suffix): (String, usize, Vec<String>, bool),
(line, cursor_col, haystack, match_suffix): (mlua::String, usize, Vec<mlua::String>, bool),
) -> LuaResult<Vec<Vec<usize>>> {
Ok(fuzzy::fuzzy_matched_indices(
&line,
&line.to_string_lossy(),
cursor_col,
&haystack,
&haystack
.iter()
.map(|s| s.to_string_lossy())
.collect::<Vec<_>>(),
match_suffix,
))
}

pub fn get_keyword_range(
_lua: &Lua,
(line, col, match_suffix): (String, usize, bool),
(line, col, match_suffix): (mlua::String, usize, bool),
) -> LuaResult<(usize, usize)> {
Ok(keyword::get_keyword_range(&line, col, match_suffix))
Ok(keyword::get_keyword_range(
&line.to_string_lossy(),
col,
match_suffix,
))
}

pub fn guess_edit_range(
_lua: &Lua,
(item, line, cursor_col, match_suffix): (LspItem, String, usize, bool),
(item, line, cursor_col, match_suffix): (LspItem, mlua::String, usize, bool),
) -> LuaResult<(usize, usize)> {
// TODO: take the max range from insert_text and filter_text
Ok(keyword::guess_keyword_range_from_item(
item.insert_text.as_ref().unwrap_or(&item.label),
&line,
&line.to_string_lossy(),
cursor_col,
match_suffix,
))
}

pub fn get_words(_: &Lua, text: String) -> LuaResult<Vec<String>> {
pub fn get_words(_: &Lua, text: mlua::String) -> LuaResult<Vec<String>> {
Ok(REGEX
.find_iter(&text)
.find_iter(&text.to_string_lossy())
.map(|m| m.as_str().to_string())
.filter(|s| s.len() < 512)
.collect::<HashSet<String>>()
Expand Down
20 changes: 15 additions & 5 deletions lua/blink/cmp/fuzzy/lsp_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ pub struct LspItem {
impl FromLua for LspItem {
fn from_lua(value: LuaValue, _: &Lua) -> LuaResult<Self> {
if let Some(tab) = value.as_table() {
let label = tab.get("label").unwrap_or_default();
let filter_text = tab.get("filterText").ok();
let sort_text = tab.get("sortText").ok();
let label = tab
.get::<mlua::String>("label")
.map(|s| s.to_string_lossy())
.unwrap_or_default();
let filter_text = tab
.get::<mlua::String>("filterText")
.ok()
.map(|s| s.to_string_lossy());
let sort_text = tab
.get::<mlua::String>("sortText")
.ok()
.map(|s| s.to_string_lossy());
let insert_text = tab
.get::<LuaTable>("textEdit")
.and_then(|text_edit| text_edit.get("newText"))
.and_then(|text_edit| text_edit.get::<mlua::String>("newText"))
.ok()
.or_else(|| tab.get("insertText").ok());
.or_else(|| tab.get::<mlua::String>("insertText").ok())
.map(|s| s.to_string_lossy());
let kind = tab.get("kind").unwrap_or_default();
let score_offset = tab.get("score_offset").unwrap_or(0);
let source_id = tab.get("source_id").unwrap_or_default();
Expand Down

0 comments on commit 3b4fa80

Please sign in to comment.