fix: prefer exact keyword match over score match

This commit is contained in:
dracarys18 2025-11-18 10:44:08 +05:30
parent 0c55cb9621
commit 84080ff59f
2 changed files with 24 additions and 1 deletions

View File

@ -179,6 +179,25 @@ impl Database {
self.with_dirty_mut(|dirty| *dirty = true);
}
pub fn sort_by_keywords(&mut self, keywords: &[String]) {
if keywords.is_empty() {
return;
}
self.with_dirs_mut(|dirs| {
dirs.sort_by_key(|dir| Self::has_exact_match(&dir.path, keywords));
});
self.with_dirty_mut(|dirty| *dirty = true);
}
fn has_exact_match(path: &str, keywords: &[String]) -> bool {
keywords.last().map_or(false, |keyword| {
let path_lower = util::to_lowercase(path);
let last_component = path_lower.rsplit(std::path::is_separator).next().unwrap_or("");
last_component == keyword
})
}
pub fn dirty(&self) -> bool {
*self.borrow_dirty()
}

View File

@ -16,7 +16,11 @@ pub struct Stream<'a> {
impl<'a> Stream<'a> {
pub fn new(db: &'a mut Database, options: StreamOptions) -> Self {
db.sort_by_score(options.now);
let now = options.now;
let keywords = &options.keywords;
db.sort_by_score(now);
db.sort_by_keywords(keywords);
let idxs = (0..db.dirs().len()).rev();
Stream { db, idxs, options }
}