Skip to content

Commit 7eb0b54

Browse files
feat(completions): syntax-aware completions for select statement (#654)
1 parent c39d3b8 commit 7eb0b54

File tree

62 files changed

+391916
-517400
lines changed

Some content is hidden

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

62 files changed

+391916
-517400
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ mod tests {
440440
"select name from instruments i join others o on i.z = o.a <sql>",
441441
)
442442
.type_sql("where o.<1>a = <2>i.z and <3>i.id > 5;")
443-
.comment("should respect alias speciifcation")
443+
.comment("should respect alias specification")
444444
.comment("should not prioritize suggest columns or schemas (right side of binary expression)")
445445
.comment("should prioritize columns that aren't already mentioned"),
446446
)

0 commit comments

Comments
 (0)