Skip to content

Commit 7bb4000

Browse files
committed
fix: proximity and frecency bonus
1 parent b93a5e3 commit 7bb4000

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

lua/blink/cmp/fuzzy/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod fuzzy;
55

66
pub mod extern_ffi {
77
use crate::frecency::FrecencyTracker;
8-
use crate::fuzzy::{self, FuzzyOptions, LspItem, MatchedLspItem};
8+
use crate::fuzzy::{self, FuzzyOptions, LspItem};
99
use lazy_static::lazy_static;
1010
use regex::Regex;
1111
use std::collections::HashSet;

lua/blink/cmp/fuzzy/frecency.rs

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
1-
use std::fs;
2-
use std::time::{SystemTime, UNIX_EPOCH};
3-
1+
use crate::fuzzy::LspItem;
42
use heed::types::*;
53
use heed::{Database, Env, EnvOpenOptions};
4+
use serde::{Deserialize, Serialize};
5+
use std::fs;
6+
use std::time::{SystemTime, UNIX_EPOCH};
67

7-
// todo: changing score offset will change all the keys
8+
#[derive(Clone, Serialize, Deserialize)]
9+
struct CompletionItemKey {
10+
label: String,
11+
kind: u32,
12+
source: String,
13+
}
814

9-
use crate::fuzzy::LspItem;
15+
impl From<&LspItem> for CompletionItemKey {
16+
fn from(item: &LspItem) -> Self {
17+
Self {
18+
label: item.label.clone(),
19+
kind: item.kind,
20+
source: item.source.clone(),
21+
}
22+
}
23+
}
1024

1125
pub struct FrecencyTracker {
1226
env: Env,
13-
db: Database<SerdeBincode<LspItem>, SerdeBincode<Vec<u64>>>,
27+
db: Database<SerdeBincode<CompletionItemKey>, SerdeBincode<Vec<u64>>>,
1428
access_thresholds: Vec<(f64, u64)>,
1529
}
1630

1731
impl FrecencyTracker {
1832
pub fn new(db_path: &str) -> Self {
19-
fs::create_dir_all(db_path);
33+
fs::create_dir_all(db_path).unwrap();
2034
let env = unsafe { EnvOpenOptions::new().open(db_path).unwrap() };
21-
env.clear_stale_readers();
35+
env.clear_stale_readers().unwrap();
2236

2337
// we will open the default unnamed database
2438
let mut wtxn = env.write_txn().unwrap();
2539
let db = env.create_database(&mut wtxn, None).unwrap();
2640

2741
let access_thresholds = [
28-
(2., 1000 * 60 * 2), // 2 minutes
29-
(1., 1000 * 60 * 5), // 5 minutes
30-
(0.5, 1000 * 60 * 30), // 2 hours
31-
(0.2, 1000 * 60 * 60 * 24), // 1 day
32-
(0.1, 1000 * 60 * 60 * 24 * 7), // 1 week
42+
(1., 1000 * 60 * 2), // 2 minutes
43+
(0.5, 1000 * 60 * 5), // 5 minutes
44+
(0.2, 1000 * 60 * 30), // 2 hours
45+
(0.1, 1000 * 60 * 60 * 24), // 1 day
46+
(0.05, 1000 * 60 * 60 * 24 * 7), // 1 week
3347
]
3448
.to_vec();
3549

@@ -46,7 +60,7 @@ impl FrecencyTracker {
4660
.read_txn()
4761
.expect("Failed to start read transaction");
4862
self.db
49-
.get(&rtxn, item)
63+
.get(&rtxn, &CompletionItemKey::from(item))
5064
.expect("Failed to read from database")
5165
}
5266

@@ -61,7 +75,8 @@ impl FrecencyTracker {
6175
let mut wtxn = self.env.write_txn()?;
6276
let mut accesses = self.get_accesses(item).unwrap_or_else(Vec::new);
6377
accesses.push(self.get_now());
64-
self.db.put(&mut wtxn, item, &accesses)?;
78+
self.db
79+
.put(&mut wtxn, &CompletionItemKey::from(item), &accesses)?;
6580
wtxn.commit()?;
6681
Ok(())
6782
}
@@ -79,6 +94,6 @@ impl FrecencyTracker {
7994
continue 'outer;
8095
}
8196
}
82-
(score * accesses.len() as f64).min(5.) as i64
97+
(score * accesses.len() as f64).min(4.) as i64
8398
}
8499
}

lua/blink/cmp/fuzzy/fuzzy.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ pub fn fuzzy(
6666
+ haystack[mtch.index_in_haystack].score_offset.unwrap_or(0)
6767
})
6868
.collect::<Vec<_>>();
69-
matches.sort_by_cached_key(|mtch| Reverse(match_scores[mtch.index]));
7069

7170
// Sort matches by sort criteria
7271
for sort in opts.sorts.iter() {
@@ -75,7 +74,7 @@ pub fn fuzzy(
7574
matches.sort_by_key(|mtch| haystack[mtch.index_in_haystack].kind);
7675
}
7776
"score" => {
78-
matches.sort_by_key(|mtch| Reverse(mtch.score));
77+
matches.sort_by_cached_key(|mtch| Reverse(match_scores[mtch.index]));
7978
}
8079
"label" => {
8180
matches.sort_by_key(|mtch| haystack[mtch.index_in_haystack].label.clone());

0 commit comments

Comments
 (0)