Skip to content

Commit 077cf68

Browse files
tests green
1 parent 06b3a17 commit 077cf68

File tree

9 files changed

+383603
-406606
lines changed

9 files changed

+383603
-406606
lines changed

crates/pgls_completions/src/providers/roles.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@ mod tests {
194194

195195
assert_complete_results(
196196
format!(
197-
r#"grant select
198-
on table public.users
199-
to {}"#,
197+
r#"grant select on table public.users to {}"#,
200198
QueryWithCursorPosition::cursor_marker()
201199
)
202200
.as_str(),

crates/pgls_completions/src/providers/tables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn get_completion_text(ctx: &TreesitterContext, table: &Table) -> CompletionText
5959
mod tests {
6060

6161
use pgls_text_size::TextRange;
62-
use sqlx::{Executor, PgPool, query::Query};
62+
use sqlx::{Executor, PgPool};
6363

6464
use crate::{
6565
CompletionItem, CompletionItemKind, complete,

crates/pgls_completions/src/relevance/filtering.rs

Lines changed: 80 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ impl CompletionFilter<'_> {
7070
}
7171
}
7272

73+
if ctx
74+
.node_under_cursor
75+
.as_ref()
76+
.is_some_and(|n| n.kind() == "any_identifier")
77+
&& ctx.matches_ancestor_history(&["alias"])
78+
{
79+
return None;
80+
}
81+
7382
// No autocompletions if there are two identifiers without a separator.
7483
if ctx.node_under_cursor.as_ref().is_some_and(|node| {
7584
node.prev_sibling().is_some_and(|p| {
@@ -95,62 +104,78 @@ impl CompletionFilter<'_> {
95104
fn check_specific_node_type(&self, ctx: &TreesitterContext) -> Option<()> {
96105
let kind = ctx.node_under_cursor.as_ref().map(|n| n.kind())?;
97106

98-
let is_allowed =
99-
match kind {
100-
"column_identifier" => matches!(self.data, CompletionRelevanceData::Column(_)),
101-
"role_identifier" => matches!(self.data, CompletionRelevanceData::Role(_)),
102-
"function_identifier" => matches!(self.data, CompletionRelevanceData::Function(_)),
103-
"schema_identifier" => matches!(self.data, CompletionRelevanceData::Schema(_)),
104-
"table_identifier" => matches!(self.data, CompletionRelevanceData::Table(_)),
105-
"policy_identifier" => matches!(self.data, CompletionRelevanceData::Policy(_)),
106-
107-
"any_identifier" => match self.data {
108-
CompletionRelevanceData::Column(_) => ctx
109-
.node_under_cursor_is_within_field_name(&[
110-
"object_reference_1of1",
111-
"object_reference_2of2",
112-
"object_reference_3of3",
113-
"column_reference_1of1",
114-
"column_reference_2of2",
115-
"column_reference_3of3",
116-
]),
117-
118-
CompletionRelevanceData::Schema(_) => ctx
119-
.node_under_cursor_is_within_field_name(&[
120-
"object_reference_1of1",
121-
"object_reference_1of2",
122-
"object_reference_1of3",
123-
"type_reference_1of1",
124-
"table_reference_1of1",
125-
"column_reference_1of1",
126-
"column_reference_1of2",
127-
"function_reference_1of1",
128-
]),
129-
130-
CompletionRelevanceData::Function(_) => ctx
131-
.node_under_cursor_is_within_field_name(&[
132-
"object_reference_1of1",
133-
"object_reference_2of2",
134-
"function_reference_1of1",
135-
]),
136-
137-
CompletionRelevanceData::Table(_) => ctx
138-
.node_under_cursor_is_within_field_name(&[
139-
"object_reference_1of1",
140-
"object_reference_1of2",
141-
"object_reference_2of2",
142-
"object_reference_2of3",
143-
"table_reference_1of1",
144-
"column_reference_1of1",
145-
"column_reference_1of2",
146-
"column_reference_2of2",
147-
]),
148-
149-
_ => false,
150-
},
107+
let is_allowed = match kind {
108+
"column_identifier" => matches!(self.data, CompletionRelevanceData::Column(_)),
109+
"role_identifier" => matches!(self.data, CompletionRelevanceData::Role(_)),
110+
"function_identifier" => matches!(self.data, CompletionRelevanceData::Function(_)),
111+
"schema_identifier" => matches!(self.data, CompletionRelevanceData::Schema(_)),
112+
"table_identifier" => matches!(self.data, CompletionRelevanceData::Table(_)),
113+
"policy_identifier" => matches!(self.data, CompletionRelevanceData::Policy(_)),
114+
115+
"any_identifier" => {
116+
if false || ctx.matches_ancestor_history(&["insert_values", "object_reference"]) {
117+
false
118+
} else {
119+
match self.data {
120+
CompletionRelevanceData::Column(_) => {
121+
ctx.node_under_cursor_is_within_field_name(&[
122+
"object_reference_1of1",
123+
"object_reference_2of2",
124+
"object_reference_3of3",
125+
"column_reference_1of1",
126+
"column_reference_2of2",
127+
"column_reference_3of3",
128+
]) && !ctx
129+
.node_under_cursor_is_within_field_name(&["binary_expr_right"])
130+
&& !ctx.matches_ancestor_history(&[
131+
"insert_values",
132+
"object_reference",
133+
])
134+
}
151135

152-
_ => false,
153-
};
136+
CompletionRelevanceData::Schema(_) => ctx
137+
.node_under_cursor_is_within_field_name(&[
138+
"object_reference_1of1",
139+
"object_reference_1of2",
140+
"object_reference_1of3",
141+
"type_reference_1of1",
142+
"table_reference_1of1",
143+
"column_reference_1of1",
144+
"column_reference_1of2",
145+
"function_reference_1of1",
146+
]),
147+
148+
CompletionRelevanceData::Function(f) => {
149+
ctx.node_under_cursor_is_within_field_name(&[
150+
"object_reference_1of1",
151+
"object_reference_2of2",
152+
"function_reference_1of1",
153+
]) && !(ctx.matches_ancestor_history(&[
154+
"check_or_using_clause",
155+
"binary_expression",
156+
"object_reference",
157+
]) && matches!(f.kind, ProcKind::Aggregate))
158+
}
159+
160+
CompletionRelevanceData::Table(_) => ctx
161+
.node_under_cursor_is_within_field_name(&[
162+
"object_reference_1of1",
163+
"object_reference_1of2",
164+
"object_reference_2of2",
165+
"object_reference_2of3",
166+
"table_reference_1of1",
167+
"column_reference_1of1",
168+
"column_reference_1of2",
169+
"column_reference_2of2",
170+
]),
171+
172+
_ => false,
173+
}
174+
}
175+
}
176+
177+
_ => false,
178+
};
154179

155180
if is_allowed { Some(()) } else { None }
156181
}

crates/pgls_treesitter/src/context/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ impl<'a> TreesitterContext<'a> {
144144
ctx.gather_tree_context();
145145
ctx.gather_info_from_ts_queries();
146146

147-
println!("{:#?}", ctx);
148-
149147
ctx
150148
}
151149

@@ -634,11 +632,7 @@ impl<'a> TreesitterContext<'a> {
634632
})
635633
}
636634

637-
/// Verifies whether the node_under_cursor has the passed in ancestors in the right order.
638-
/// Note that you need to pass in the ancestors in the order as they would appear in the tree:
639-
///
640-
/// If the tree shows `relation > object_reference > any_identifier` and the "any_identifier" is a leaf node,
641-
/// you need to pass `&["relation", "object_reference"]`.
635+
/// Verifies if the node has one of the named direct ancestors.
642636
pub fn matches_one_of_ancestors(&self, expected_ancestors: &[&'static str]) -> bool {
643637
self.node_under_cursor.as_ref().is_some_and(|node| {
644638
node.parent()

crates/pgls_treesitter/src/queries/table_aliases.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ static TS_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
99
static QUERY_STR: &str = r#"
1010
(relation
1111
(table_reference) @ref
12-
(keyword_as)?
13-
(any_identifier) @alias
12+
(alias
13+
(keyword_as)?
14+
(any_identifier) @alias
15+
)?
1416
)
1517
"#;
1618
tree_sitter::Query::new(&pgls_treesitter_grammar::LANGUAGE.into(), QUERY_STR)

0 commit comments

Comments
 (0)