Skip to content

Commit bd5e476

Browse files
committed
saving code
1 parent f44328e commit bd5e476

File tree

6 files changed

+83
-51
lines changed

6 files changed

+83
-51
lines changed

autoload/easycomplete/util.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,8 @@ function! easycomplete#util#CompleteMenuFilter(all_menu, word, maxlength)
12291229
" 和 word 不是一一对应的,通过word fuzzy 匹配的位置无法正确应用在 abbr 上
12301230
" 这里只 hack 了 vim,其他类型的文件未测试
12311231
let key_name = (&filetype == "vim") ? "abbr" : "word"
1232-
let matching_res = all_items->matchfuzzypos(word, {'key': key_name, 'matchseq': 1, "limit": a:maxlength})
1232+
" let matching_res = all_items->matchfuzzypos(word, {'key': key_name, 'matchseq': 1, "limit": a:maxlength})
1233+
let matching_res = s:util_toolkit.matchfuzzypos(all_items, word, {'key': key_name, 'matchseq': 1, "limit": a:maxlength})
12331234
if g:env_is_nvim
12341235
" 350 个元素,7ms
12351236
let l:ret = s:util_toolkit.complete_menu_filter(matching_res, word)

build.sh

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
#!/usr/bin/env sh
22

3+
name="debug"
4+
#name="release"
5+
36
rm -rf ./target/release
47

5-
# cargo build --target x86_64-apple-darwin --release
6-
# cargo build --target aarch64-apple-darwin --release
7-
8-
# mkdir -p target/universal/release
9-
10-
# lipo -create \
11-
# target/x86_64-apple-darwin/release/libeasycomplete_util.dylib \
12-
# target/aarch64-apple-darwin/release/libeasycomplete_util.dylib \
13-
# -output target/debug/libeasycomplete_util.dylib
14-
15-
####################### dev #######################
16-
17-
# cargo rustc -- \
18-
# -C link-arg=-undefined \
19-
# -C link-arg=dynamic_lookup \
20-
# -C opt-level=3 \
21-
# --target x86_64-apple-darwin
22-
23-
# cd ./target
24-
# ln -s debug release
25-
26-
####################### release #######################
27-
28-
rm -rf ./target/debug
29-
cargo rustc --release -- \
30-
-C link-arg=-undefined \
31-
-C link-arg=dynamic_lookup \
32-
-C opt-level=3 \
33-
-C debuginfo=0 \
34-
--target x86_64-apple-darwin
35-
rm ./target/CACHEDIR.TAG
36-
rm ./target/.rustc_info.json
37-
rm ./target/release/libeasycomplete_rust_speed.d
38-
rm -rf ./target/release/incremental
39-
rm -rf ./target/release/examples
40-
rm -rf ./target/release/deps
41-
rm -rf ./target/release/build
42-
rm -rf ./target/release/.fingerprint
43-
rm ./target/release/.cargo-lock
44-
#
45-
# -C profile-generate \
8+
if [ "$name" = "debug" ]; then
9+
####################### dev #######################
10+
echo "debug build"
11+
12+
cargo rustc -- \
13+
-C link-arg=-undefined \
14+
-C link-arg=dynamic_lookup \
15+
-C opt-level=3 \
16+
--target x86_64-apple-darwin
17+
18+
cd ./target
19+
ln -s debug release
20+
21+
else
22+
####################### release #######################
23+
echo "release build"
24+
25+
rm -rf ./target/debug
26+
cargo rustc --release -- \
27+
-C link-arg=-undefined \
28+
-C link-arg=dynamic_lookup \
29+
-C opt-level=3 \
30+
-C debuginfo=0 \
31+
--target x86_64-apple-darwin
32+
33+
rm ./target/CACHEDIR.TAG
34+
rm ./target/.rustc_info.json
35+
rm ./target/release/libeasycomplete_rust_speed.d
36+
rm ./target/release/.cargo-lock
37+
rm -rf ./target/release/incremental
38+
rm -rf ./target/release/examples
39+
rm -rf ./target/release/deps
40+
rm -rf ./target/release/build
41+
rm -rf ./target/release/.fingerprint
42+
fi
43+
4644
echo "dylib created!"

lua/easycomplete/lib/speed.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,18 +407,19 @@ fn trim_array_to_length(lua: &Lua, (arr, n): (LuaTable, i32)) -> Result<LuaTable
407407
}
408408

409409
// https://crates.io/crates/sublime_fuzzy
410-
// 废弃:vim 原生matchfuzzypos速度最快,rust 涉及到跨语言,2~8ms,原生0.2ms
411410
fn matchfuzzypos(lua: &Lua, (list, word, opt): (LuaTable, String, LuaTable)) -> Result<LuaTable, LuaError> {
412411

413412
let mut matchfuzzy = lua.create_table()?;
414413
let mut positions = lua.create_table()?;
415414
let mut scores = lua.create_table()?;
415+
let key: String = opt.get("key")?;
416+
let limit: i32 = opt.get("limit")?;
416417

417-
let mut iter = list.sequence_values::<Table>();
418+
let mut list_iter = list.sequence_values::<Table>();
418419
let mut i: usize = 1;
419-
while let Some(every_item) = iter.next() {
420-
let item = every_item?;
421-
let t_word: String = item.get("word")?;
420+
while let Some(every_item) = list_iter.next() {
421+
let mut item = every_item?;
422+
let t_word: String = item.get(key.clone())?;
422423
let mut position: LuaTable = lua.create_table()?;
423424
let mut score: i32;
424425
// let m = best_match(&word, &t_word).expect("No match");
@@ -431,23 +432,49 @@ fn matchfuzzypos(lua: &Lua, (list, word, opt): (LuaTable, String, LuaTable)) ->
431432
position.push(*p as i32);
432433
}
433434
score = m.score() as i32;
435+
436+
item.set("score", score.clone());
437+
item.set("position", position.clone());
434438
matchfuzzy.push(item.clone());
435-
positions.push(position.clone());
436-
scores.push(score.clone());
437439
}
438440
}
439441
None => {
440442
// catch 异常情况
443+
// do nothing and continue
441444
}
442445
}
443446
i += 1;
444447
}
445448

449+
let mut matchfuzzy_vec: Vec<Table> = matchfuzzy
450+
.sequence_values::<Table>()
451+
.collect::<Result<Vec<_>, _>>()?;
452+
453+
matchfuzzy_vec.sort_by(|a, b| {
454+
let score_a = a.get::<i32>("score").unwrap_or_default();
455+
let score_b = b.get::<i32>("score").unwrap_or_default();
456+
score_b.cmp(&score_a)
457+
});
458+
459+
let mut new_matchfuzzy = lua.create_table()?;
460+
let max: usize = limit as usize;
461+
462+
// 4. 写回排序后的结果
463+
for (i, item) in matchfuzzy_vec.into_iter().enumerate() {
464+
let mut p: LuaTable = item.get("position")?;
465+
let mut s: i32 = item.get("score")?;
466+
new_matchfuzzy.set(i+1, item)?; // Lua 索引从 1 开始
467+
positions.set(i+1, p.clone())?;
468+
scores.set(i+1, s.clone())?;
469+
if i > max {
470+
break;
471+
}
472+
}
473+
446474
let mut result = lua.create_table()?;
447-
result.push(matchfuzzy);
475+
result.push(new_matchfuzzy);
448476
result.push(positions);
449477
result.push(scores);
450-
// TODO 根据 Score 进行排序
451478
Ok(result)
452479
}
453480

lua/easycomplete/util.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ function util.fuzzy_search(haystack, needle)
291291
end
292292
end
293293

294+
function util.matchfuzzypos(a,b,c)
295+
local ret = rust_speed.matchfuzzypos(a,b,c)
296+
return ret
297+
end
298+
294299
-- easycomplete#util#GetVimCompletionItems 的 lua 实现
295300
function util.get_vim_complete_items(response, plugin_name, word)
296301
local tt = vim.fn.reltime()

target/release

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
debug
-754 KB
Binary file not shown.

0 commit comments

Comments
 (0)