Skip to content

Commit 853929c

Browse files
many fixings
1 parent 09664c1 commit 853929c

File tree

6 files changed

+72
-21
lines changed

6 files changed

+72
-21
lines changed

crates/pgls_completions/src/builder.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
CompletionItemKind, CompletionText,
33
item::CompletionItem,
44
relevance::{filtering::CompletionFilter, scoring::CompletionScore},
5+
sanitization,
56
};
67

78
use pgls_treesitter::TreesitterContext;
@@ -24,6 +25,12 @@ pub(crate) struct CompletionBuilder<'a> {
2425

2526
impl<'a> CompletionBuilder<'a> {
2627
pub fn new(ctx: &'a TreesitterContext) -> Self {
28+
println!(
29+
"is sanitized: {:#?}",
30+
ctx.get_node_under_cursor_content()
31+
.map(|txt| sanitization::is_sanitized_token(txt.as_str()))
32+
);
33+
2734
CompletionBuilder { items: vec![], ctx }
2835
}
2936

@@ -42,6 +49,11 @@ impl<'a> CompletionBuilder<'a> {
4249
item.score.calc_score(self.ctx);
4350
}
4451

52+
items = items
53+
.into_iter()
54+
.filter(|i| !i.score.should_skip())
55+
.collect();
56+
4557
items.sort_by(|a, b| {
4658
b.score
4759
.get_score()

crates/pgls_completions/src/providers/columns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn get_completion_text(ctx: &TreesitterContext, col: &Column) -> CompletionText
5252
mod tests {
5353
use sqlx::PgPool;
5454

55-
use crate::test_helper::{TestCompletionsCase, TestCompletionsSuite};
55+
use crate::test_helper::{TestCompletionsCase, TestCompletionsSuite, assert_complete_results};
5656

5757
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]
5858
async fn handles_nested_queries(pool: PgPool) {

crates/pgls_completions/src/relevance/scoring.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@ use super::CompletionRelevanceData;
99
#[derive(Debug)]
1010
pub(crate) struct CompletionScore<'a> {
1111
score: i32,
12+
skip: bool,
1213
data: CompletionRelevanceData<'a>,
1314
}
1415

1516
impl<'a> From<CompletionRelevanceData<'a>> for CompletionScore<'a> {
1617
fn from(value: CompletionRelevanceData<'a>) -> Self {
1718
Self {
1819
score: 0,
20+
skip: false,
1921
data: value,
2022
}
2123
}
2224
}
2325

2426
impl CompletionScore<'_> {
27+
pub fn should_skip(&self) -> bool {
28+
self.skip
29+
}
30+
2531
pub fn get_score(&self) -> i32 {
2632
self.score
2733
}
@@ -55,20 +61,35 @@ impl CompletionScore<'_> {
5561

5662
let fz_matcher = SkimMatcherV2::default();
5763

58-
if let Some(score) =
59-
fz_matcher.fuzzy_match(name.as_str(), content.to_ascii_lowercase().as_str())
60-
{
61-
let scorei32: i32 = score
62-
.try_into()
63-
.expect("The length of the input exceeds i32 capacity");
64-
65-
// the scoring value isn't linear.
66-
// here are a couple of samples:
67-
// - item: bytea_string_agg_transfn, input: n, score: 15
68-
// - item: numeric_uplus, input: n, score: 31
69-
// - item: settings, input: sett, score: 91
70-
// - item: user_settings, input: sett, score: 82
71-
self.score += scorei32 / 2;
64+
let check_against = match ctx.identifier_qualifiers {
65+
// If both qualifiers are already written out, we must check the item's name itself.
66+
(Some(_), Some(_)) => content.to_ascii_lowercase(),
67+
68+
_ => self
69+
.get_table_name()
70+
.and_then(|tbl| ctx.get_used_alias_for_table(tbl))
71+
.map(|alias| format!("{}.{}", alias, name))
72+
.unwrap_or(name),
73+
};
74+
75+
match fz_matcher.fuzzy_match(
76+
check_against.as_str(),
77+
content.to_ascii_lowercase().as_str(),
78+
) {
79+
Some(score) => {
80+
let scorei32: i32 = score
81+
.try_into()
82+
.expect("The length of the input exceeds i32 capacity");
83+
84+
// the scoring value isn't linear.
85+
// here are a couple of samples:
86+
// - item: bytea_string_agg_transfn, input: n, score: 15
87+
// - item: numeric_uplus, input: n, score: 31
88+
// - item: settings, input: sett, score: 91
89+
// - item: user_settings, input: sett, score: 82
90+
self.score += scorei32 / 2;
91+
}
92+
None => self.skip = true,
7293
}
7394
}
7495

crates/pgls_completions/src/snapshots/pgls_completions__test_helper__completes_in_join_on_clause.snap

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ select u.id, auth.posts.content from auth.users u join auth.posts p on p.u|
5252

5353
Results:
5454
user_id - auth.posts.user_id (Column)
55-
pid - auth.posts.pid (Column)
56-
content - auth.posts.content (Column)
57-
created_at - auth.posts.created_at (Column)
58-
title - auth.posts.title (Column)
5955

6056
--------------
6157

@@ -96,8 +92,6 @@ select u.id, auth.posts.content from auth.users u join auth.posts p on p.user_id
9692

9793
Results:
9894
uid - auth.users.uid (Column)
99-
email - auth.users.email (Column)
100-
name - auth.users.name (Column)
10195

10296
--------------
10397

crates/pgls_treesitter/src/context/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ impl<'a> TreesitterContext<'a> {
153153
ctx.gather_tree_context();
154154
ctx.gather_info_from_ts_queries();
155155

156+
println!("{} {:#?}", ctx.text, ctx.get_node_under_cursor_content(),);
157+
156158
ctx
157159
}
158160

todo.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
#### Snapshot review
3+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__completes_in_join_on_clause.snap
4+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__completes_quoted_columns_with_aliases.snap
5+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__completes_quoted_columns.snap
6+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__does_not_complete_cols_in_join_clauses.snap
7+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__filters_out_by_aliases_in_join_on.snap
8+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__filters_out_by_aliases_in_select.snap
9+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__handles_nested_queries.snap
10+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__ignores_cols_in_from_clause.snap
11+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__prefers_columns_of_mentioned_tables.snap
12+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__prefers_not_mentioned_columns.snap
13+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__shows_multiple_columns_if_no_relation_specified.snap
14+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__suggests_columns_in_alter_table_and_drop_table.snap
15+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__suggests_columns_in_insert_clause.snap
16+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__suggests_columns_in_where_clause.snap
17+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__suggests_columns_policy_using_clause.snap
18+
- [ ] crates/pgls_completions/src/snapshots/pgls_completions__test_helper__suggests_relevant_columns_without_letters.snap
19+
20+
#### General Todos
21+
- [ ] Should filter out any items where typed letters don't match at all
22+
- [ ]

0 commit comments

Comments
 (0)