Skip to content

Commit 324c4f5

Browse files
commit
1 parent c0e2d35 commit 324c4f5

File tree

7 files changed

+33
-32
lines changed

7 files changed

+33
-32
lines changed

crates/pgls_completions/src/providers/columns.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ fn get_completion_text(ctx: &TreesitterContext, col: &Column) -> CompletionText
5050

5151
#[cfg(test)]
5252
mod tests {
53+
use pgls_test_utils::QueryWithCursorPosition;
5354
use sqlx::PgPool;
5455

55-
use crate::test_helper::{TestCompletionsCase, TestCompletionsSuite};
56+
use crate::test_helper::{
57+
TestCompletionsCase, TestCompletionsSuite, assert_complete_results,
58+
assert_no_complete_results,
59+
};
5660

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

crates/pgls_completions/src/relevance/filtering.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use pgls_schema_cache::ProcKind;
22
use pgls_treesitter::context::{TreesitterContext, WrappingClause, WrappingNode};
33

4+
use crate::is_sanitized_token;
5+
46
use super::CompletionRelevanceData;
57

68
#[derive(Debug)]
@@ -43,6 +45,14 @@ impl CompletionFilter<'_> {
4345
return None;
4446
}
4547

48+
// if ctx
49+
// .get_node_under_cursor_content()
50+
// .is_some_and(|c| is_sanitized_token(c.as_str()))
51+
// && ctx.before_cursor_matches_kind(&["ERROR"])
52+
// {
53+
// return None;
54+
// }
55+
4656
// "literal" nodes can be identfiers wrapped in quotes:
4757
// `select "email" from auth.users;`
4858
// Here, "email" is a literal node.

crates/pgls_completions/src/relevance/scoring.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ impl CompletionScore<'_> {
4747
fn check_matches_query_input(&mut self, ctx: &TreesitterContext) {
4848
let content = match ctx.get_node_under_cursor_content() {
4949
Some(c) if !sanitization::is_sanitized_token(c.as_str()) => c.replace('"', ""),
50-
_ => return,
50+
_ => {
51+
return;
52+
}
5153
};
5254

5355
let name = match self.data {
@@ -59,31 +61,28 @@ impl CompletionScore<'_> {
5961
CompletionRelevanceData::Role(r) => r.name.as_str().to_ascii_lowercase(),
6062
};
6163

62-
let is_case =
63-
matches!(self.data, CompletionRelevanceData::Column(_)) && content.as_str() == "u";
64-
6564
let fz_matcher = SkimMatcherV2::default();
6665

6766
let check_against = match &ctx.identifier_qualifiers {
6867
// If both qualifiers are already written out, we must check the item's name itself.
69-
(Some(_), Some(_)) => name,
68+
(Some(_), Some(_)) => name.clone(),
7069

7170
// If only one qualifier is written out, we might look at a schema, a table, or an alias.
7271
(None, Some(qualifier)) => {
7372
if self.get_schema_name().is_some_and(|s| s == qualifier) {
7473
self.get_table_name()
7574
.map(|t| format!("{}.{}", t, name))
76-
.unwrap_or(name)
75+
.unwrap_or(name.clone())
7776
} else if self.get_table_name().is_some_and(|t| t == qualifier) {
78-
name
77+
name.clone()
7978
} else if ctx
8079
.get_mentioned_table_for_alias(&qualifier)
8180
.is_some_and(|alias_tbl| {
8281
self.get_table_name()
8382
.is_some_and(|item_tbl| alias_tbl == item_tbl)
8483
})
8584
{
86-
name
85+
name.clone()
8786
} else {
8887
// the qualifier does not match schema, table, or alias.
8988
// what the hell is it?
@@ -102,10 +101,6 @@ impl CompletionScore<'_> {
102101
content.to_ascii_lowercase().as_str(),
103102
) {
104103
Some(score) => {
105-
if is_case {
106-
println!("check {}, score {}", check_against, score);
107-
}
108-
109104
let scorei32: i32 = score
110105
.try_into()
111106
.expect("The length of the input exceeds i32 capacity");
@@ -116,7 +111,11 @@ impl CompletionScore<'_> {
116111
// - item: numeric_uplus, input: n, score: 31
117112
// - item: settings, input: sett, score: 91
118113
// - item: user_settings, input: sett, score: 82
119-
self.score += scorei32 / 2;
114+
self.score += if check_against == name {
115+
scorei32 / 2
116+
} else {
117+
scorei32 / 3
118+
};
120119
}
121120
None => self.skip = true,
122121
}

crates/pgls_completions/src/sanitization.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ pub(crate) fn remove_sanitized_token(it: &str) -> String {
2626
}
2727

2828
pub(crate) fn is_sanitized_token(node_under_cursor_txt: &str) -> bool {
29-
node_under_cursor_txt == SANITIZED_TOKEN || is_sanitized_token_with_quote(node_under_cursor_txt)
29+
node_under_cursor_txt.replace('"', "") == SANITIZED_TOKEN
30+
|| is_sanitized_token_with_quote(node_under_cursor_txt)
3031
}
3132

3233
pub(crate) fn is_sanitized_token_with_quote(node_under_cursor_txt: &str) -> bool {

crates/pgls_completions/src/snapshots/pgls_completions__test_helper__completes_in_join_on_clause.snap

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,6 @@ user_id - auth.posts.user_id (Column)
5656
--------------
5757

5858
select u.id, auth.posts.content from auth.users u join auth.posts p on p.user_id |
59-
60-
Results:
61-
p.pid - auth.posts.pid (Column)
62-
u.uid - auth.users.uid (Column)
63-
p.content - auth.posts.content (Column)
64-
p.created_at - auth.posts.created_at (Column)
65-
u.email - auth.users.email (Column)
66-
67-
--------------
68-
6959
select u.id, auth.posts.content from auth.users u join auth.posts p on p.user_id = |
7060

7161
Results:

crates/pgls_completions/src/snapshots/pgls_completions__test_helper__completes_quoted_columns_with_aliases.snap

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public - public (Schema)
9292
private - private (Schema)
9393
names - public.names (Table)
9494
private.users - private.users (Table)
95-
information_schema.parameters - information_schema.parameters (Table)
95+
pg_catalog - pg_catalog (Schema)
9696

9797
--------------
9898

@@ -159,9 +159,9 @@ select "e|" from private.users "pr"
159159

160160
Results:
161161
pr"."email - private.users.email (Column)
162+
pr"."id - private.users.id (Column)
162163
pr"."name - private.users.name (Column)
163164
pr"."quoted_column - private.users.quoted_column (Column)
164-
pr"."id - private.users.id (Column)
165165
name - public.names.name (Column)
166166

167167
--------------
@@ -232,7 +232,6 @@ Results:
232232
email - private.users.email (Column)
233233
name - private.users.name (Column)
234234
quoted_column - private.users.quoted_column (Column)
235-
id - private.users.id (Column)
236235

237236
--------------
238237

@@ -324,7 +323,6 @@ Results:
324323
email - private.users.email (Column)
325324
name - private.users.name (Column)
326325
quoted_column - private.users.quoted_column (Column)
327-
id - private.users.id (Column)
328326

329327
--------------
330328

@@ -375,7 +373,6 @@ Results:
375373
email - private.users.email (Column)
376374
name - private.users.name (Column)
377375
quoted_column - private.users.quoted_column (Column)
378-
id - private.users.id (Column)
379376

380377
--------------
381378

@@ -447,7 +444,6 @@ Results:
447444
email - private.users.email (Column)
448445
name - private.users.name (Column)
449446
quoted_column - private.users.quoted_column (Column)
450-
id - private.users.id (Column)
451447

452448
--------------
453449

@@ -474,7 +470,6 @@ select "pr"."email", n.u| from private.users "pr" join public.names n on pr.id =
474470

475471
Results:
476472
uid - public.names.uid (Column)
477-
name - public.names.name (Column)
478473

479474
--------------
480475

crates/pgls_completions/src/test_helper.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ pub(crate) async fn assert_no_complete_results(query: &str, setup: Option<&str>,
205205
let params = get_test_params(&tree, &cache, query.into());
206206
let items = complete(params);
207207

208+
println!("Items: {:#?}", &items[..3]);
209+
208210
assert_eq!(items.len(), 0)
209211
}
210212

0 commit comments

Comments
 (0)