Skip to content

Commit 72de7b0

Browse files
6/11: fix(completions): sanitization edge cases (#659)
1 parent 5793124 commit 72de7b0

File tree

57 files changed

+3244
-923
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3244
-923
lines changed

crates/pgls_completions/src/builder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ impl<'a> CompletionBuilder<'a> {
3232
}
3333

3434
pub fn finish(self) -> Vec<CompletionItem> {
35+
let mut shared_tree = self.ctx.tree.clone();
36+
3537
let mut items: Vec<PossibleCompletionItem> = self
3638
.items
3739
.into_iter()
38-
.filter(|i| i.filter.is_relevant(self.ctx).is_some())
40+
.filter(|i| i.filter.is_relevant(self.ctx, &mut shared_tree).is_some())
3941
.collect();
4042

4143
for item in items.iter_mut() {

crates/pgls_completions/src/complete.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::{
66
builder::CompletionBuilder,
77
item::CompletionItem,
88
providers::{
9-
complete_columns, complete_functions, complete_policies, complete_roles, complete_schemas,
10-
complete_tables,
9+
complete_columns, complete_functions, complete_keywords, complete_policies, complete_roles,
10+
complete_schemas, complete_tables,
1111
},
1212
sanitization::SanitizedCompletionParams,
1313
};
@@ -27,6 +27,13 @@ pub struct CompletionParams<'a> {
2727
position = params.position.to_string()
2828
))]
2929
pub fn complete(params: CompletionParams) -> Vec<CompletionItem> {
30+
let uses_upper_case = params
31+
.text
32+
.split_ascii_whitespace()
33+
// filter out special chars and numbers
34+
.filter(|word| word.chars().all(|c| c.is_alphabetic()))
35+
.any(|t| t == t.to_ascii_uppercase());
36+
3037
let sanitized_params = SanitizedCompletionParams::from(params);
3138

3239
let ctx = TreesitterContext::new(TreeSitterContextParams {
@@ -43,6 +50,7 @@ pub fn complete(params: CompletionParams) -> Vec<CompletionItem> {
4350
complete_schemas(&ctx, sanitized_params.schema, &mut builder);
4451
complete_policies(&ctx, sanitized_params.schema, &mut builder);
4552
complete_roles(&ctx, sanitized_params.schema, &mut builder);
53+
complete_keywords(&ctx, &mut builder, uses_upper_case);
4654

4755
builder.finish()
4856
}

crates/pgls_completions/src/item.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum CompletionItemKind {
1313
Schema,
1414
Policy,
1515
Role,
16+
Keyword,
1617
}
1718

1819
impl Display for CompletionItemKind {
@@ -24,6 +25,7 @@ impl Display for CompletionItemKind {
2425
CompletionItemKind::Schema => "Schema",
2526
CompletionItemKind::Policy => "Policy",
2627
CompletionItemKind::Role => "Role",
28+
CompletionItemKind::Keyword => "Keyword",
2729
};
2830

2931
write!(f, "{txt}")

crates/pgls_completions/src/providers/columns.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ mod tests {
417417
}
418418

419419
#[sqlx::test(migrator = "pgls_test_utils::MIGRATIONS")]
420-
#[ignore = "will be reintroduced after stacked keyword-completion PRs merge"]
421420
async fn suggests_columns_in_where_clause(pool: PgPool) {
422421
let setup = r#"
423422
create table instruments (
@@ -441,7 +440,7 @@ mod tests {
441440
"select name from instruments i join others o on i.z = o.a <sql>",
442441
)
443442
.type_sql("where o.<1>a = <2>i.z and <3>i.id > 5;")
444-
.comment("should respect alias speciifcation")
443+
.comment("should respect alias specification")
445444
.comment("should not prioritize suggest columns or schemas (right side of binary expression)")
446445
.comment("should prioritize columns that aren't already mentioned"),
447446
)

0 commit comments

Comments
 (0)