Skip to content

fix(completions): use fuzzy matching for user input #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/pgt_completions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async-std = "1.12.0"
pgt_text_size.workspace = true


fuzzy-matcher = "0.3.7"
pgt_schema_cache.workspace = true
pgt_treesitter_queries.workspace = true
schemars = { workspace = true, optional = true }
Expand Down
76 changes: 33 additions & 43 deletions crates/pgt_completions/src/providers/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,60 +273,50 @@ mod tests {
id1 serial primary key,
name1 text,
address1 text,
email1 text
email1 text,
user_settings jsonb
);

create table public.users (
id2 serial primary key,
name2 text,
address2 text,
email2 text
email2 text,
settings jsonb
);
"#;

{
let test_case = TestCase {
message: "",
query: format!(r#"select {} from users"#, CURSOR_POS),
label: "suggests from table",
description: "",
};

let (tree, cache) = get_test_deps(setup, test_case.get_input_query()).await;
let params = get_test_params(&tree, &cache, test_case.get_input_query());
let results = complete(params);

assert_eq!(
results
.into_iter()
.take(4)
.map(|item| item.label)
.collect::<Vec<String>>(),
vec!["address2", "email2", "id2", "name2"]
);
}

{
let test_case = TestCase {
message: "",
query: format!(r#"select {} from private.users"#, CURSOR_POS),
label: "suggests from table",
description: "",
};
assert_complete_results(
format!(r#"select {} from users"#, CURSOR_POS).as_str(),
vec![
CompletionAssertion::Label("address2".into()),
CompletionAssertion::Label("email2".into()),
CompletionAssertion::Label("id2".into()),
CompletionAssertion::Label("name2".into()),
],
setup,
)
.await;

let (tree, cache) = get_test_deps(setup, test_case.get_input_query()).await;
let params = get_test_params(&tree, &cache, test_case.get_input_query());
let results = complete(params);
assert_complete_results(
format!(r#"select {} from private.users"#, CURSOR_POS).as_str(),
vec![
CompletionAssertion::Label("address1".into()),
CompletionAssertion::Label("email1".into()),
CompletionAssertion::Label("id1".into()),
CompletionAssertion::Label("name1".into()),
],
setup,
)
.await;

assert_eq!(
results
.into_iter()
.take(4)
.map(|item| item.label)
.collect::<Vec<String>>(),
vec!["address1", "email1", "id1", "name1"]
);
}
// asserts fuzzy finding for "settings"
assert_complete_results(
format!(r#"select sett{} from private.users"#, CURSOR_POS).as_str(),
vec![CompletionAssertion::Label("user_settings".into())],
setup,
)
.await;
}

#[tokio::test]
Expand Down
19 changes: 14 additions & 5 deletions crates/pgt_completions/src/relevance/scoring.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use fuzzy_matcher::{FuzzyMatcher, skim::SkimMatcherV2};

use crate::context::{CompletionContext, WrappingClause, WrappingNode};

use super::CompletionRelevanceData;
Expand Down Expand Up @@ -45,14 +47,21 @@ impl CompletionScore<'_> {
CompletionRelevanceData::Schema(s) => s.name.as_str(),
};

if name.starts_with(content.as_str()) {
let len: i32 = content
.len()
let fz_matcher = SkimMatcherV2::default();

if let Some(score) = fz_matcher.fuzzy_match(name, content.as_str()) {
let scorei32: i32 = score
.try_into()
.expect("The length of the input exceeds i32 capacity");

self.score += len * 10;
};
// the scoring value isn't linear.
// here are a couple of samples:
// - item: bytea_string_agg_transfn, input: n, score: 15
// - item: numeric_uplus, input: n, score: 31
// - item: settings, input: sett, score: 91
// - item: user_settings, input: sett, score: 82
self.score += scorei32 / 2;
}
}

fn check_matching_clause_type(&mut self, ctx: &CompletionContext) {
Expand Down