Skip to content

Commit bd034e7

Browse files
revert logic to whitelist
1 parent 2ada420 commit bd034e7

File tree

3 files changed

+77
-38
lines changed

3 files changed

+77
-38
lines changed

crates/pgt_completions/src/context.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub enum WrappingClause<'a> {
1818
},
1919
Update,
2020
Delete,
21+
ColumnDefinitions,
2122
}
2223

2324
#[derive(PartialEq, Eq, Debug)]
@@ -76,6 +77,7 @@ impl TryFrom<String> for WrappingNode {
7677
}
7778
}
7879

80+
#[derive(Debug)]
7981
pub(crate) struct CompletionContext<'a> {
8082
pub node_under_cursor: Option<tree_sitter::Node<'a>>,
8183

@@ -140,6 +142,13 @@ impl<'a> CompletionContext<'a> {
140142
ctx.gather_tree_context();
141143
ctx.gather_info_from_ts_queries();
142144

145+
if cfg!(test) {
146+
println!("{:#?}", ctx.wrapping_clause_type);
147+
println!("{:#?}", ctx.wrapping_node_kind);
148+
println!("{:#?}", ctx.is_in_error_node);
149+
println!("{:#?}", ctx.text);
150+
}
151+
143152
ctx
144153
}
145154

@@ -303,7 +312,7 @@ impl<'a> CompletionContext<'a> {
303312
}
304313
}
305314

306-
"where" | "update" | "select" | "delete" | "from" | "join" => {
315+
"where" | "update" | "select" | "delete" | "from" | "join" | "column_definitions" => {
307316
self.wrapping_clause_type =
308317
self.get_wrapping_clause_from_current_node(current_node, &mut cursor);
309318
}
@@ -367,6 +376,7 @@ impl<'a> CompletionContext<'a> {
367376
"select" => Some(WrappingClause::Select),
368377
"delete" => Some(WrappingClause::Delete),
369378
"from" => Some(WrappingClause::From),
379+
"column_definitions" => Some(WrappingClause::ColumnDefinitions),
370380
"join" => {
371381
// sadly, we need to manually iterate over the children –
372382
// `node.child_by_field_id(..)` does not work as expected

crates/pgt_completions/src/relevance/filtering.rs

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ impl CompletionFilter<'_> {
2424
}
2525

2626
fn completable_context(&self, ctx: &CompletionContext) -> Option<()> {
27+
if ctx.wrapping_node_kind.is_none()
28+
&& ctx.wrapping_clause_type.is_none()
29+
&& ctx.is_in_error_node
30+
{
31+
return None;
32+
}
33+
2734
let current_node_kind = ctx.node_under_cursor.map(|n| n.kind()).unwrap_or("");
2835

2936
if current_node_kind.starts_with("keyword_")
@@ -58,44 +65,53 @@ impl CompletionFilter<'_> {
5865
}
5966

6067
fn check_clause(&self, ctx: &CompletionContext) -> Option<()> {
61-
let clause = ctx.wrapping_clause_type.as_ref();
62-
63-
match self.data {
64-
CompletionRelevanceData::Table(_) => {
65-
let in_select_clause = clause.is_some_and(|c| c == &WrappingClause::Select);
66-
let in_where_clause = clause.is_some_and(|c| c == &WrappingClause::Where);
67-
68-
if in_select_clause || in_where_clause {
69-
return None;
70-
};
71-
}
72-
CompletionRelevanceData::Column(_) => {
73-
let in_from_clause = clause.is_some_and(|c| c == &WrappingClause::From);
74-
if in_from_clause {
75-
return None;
76-
}
77-
78-
// We can complete columns in JOIN cluases, but only if we are after the
79-
// ON node in the "ON u.id = posts.user_id" part.
80-
let in_join_clause_before_on_node = clause.is_some_and(|c| match c {
81-
// we are in a JOIN, but definitely not after an ON
82-
WrappingClause::Join { on_node: None } => true,
83-
84-
WrappingClause::Join { on_node: Some(on) } => ctx
85-
.node_under_cursor
86-
.is_some_and(|n| n.end_byte() < on.start_byte()),
87-
88-
_ => false,
89-
});
90-
91-
if in_join_clause_before_on_node {
92-
return None;
68+
ctx.wrapping_clause_type
69+
.as_ref()
70+
.map(|clause| {
71+
match self.data {
72+
CompletionRelevanceData::Table(_) => match clause {
73+
WrappingClause::Select
74+
| WrappingClause::Where
75+
| WrappingClause::ColumnDefinitions => false,
76+
_ => true,
77+
},
78+
CompletionRelevanceData::Column(_) => {
79+
match clause {
80+
WrappingClause::From | WrappingClause::ColumnDefinitions => false,
81+
82+
// We can complete columns in JOIN cluases, but only if we are after the
83+
// ON node in the "ON u.id = posts.user_id" part.
84+
WrappingClause::Join { on_node: Some(on) } => ctx
85+
.node_under_cursor
86+
.is_some_and(|cn| cn.start_byte() >= on.end_byte()),
87+
88+
// we are in a JOIN, but definitely not after an ON
89+
WrappingClause::Join { on_node: None } => false,
90+
91+
_ => true,
92+
}
93+
}
94+
CompletionRelevanceData::Function(_) => match clause {
95+
WrappingClause::From
96+
| WrappingClause::Select
97+
| WrappingClause::Where
98+
| WrappingClause::Join { .. } => true,
99+
100+
_ => false,
101+
},
102+
CompletionRelevanceData::Schema(_) => match clause {
103+
WrappingClause::Select
104+
| WrappingClause::Where
105+
| WrappingClause::From
106+
| WrappingClause::Join { .. }
107+
| WrappingClause::Update
108+
| WrappingClause::Delete => true,
109+
110+
WrappingClause::ColumnDefinitions => false,
111+
},
93112
}
94-
}
95-
_ => {}
96-
}
97-
98-
Some(())
113+
})
114+
.and_then(|is_ok| if is_ok { Some(()) } else { None })
99115
}
100116

101117
fn check_invocation(&self, ctx: &CompletionContext) -> Option<()> {
@@ -170,4 +186,15 @@ mod tests {
170186
)
171187
.await;
172188
}
189+
190+
#[tokio::test]
191+
async fn completion_after_create_table() {
192+
assert_no_complete_results(format!("create table {}", CURSOR_POS).as_str(), "").await;
193+
}
194+
195+
#[tokio::test]
196+
async fn completion_in_column_definitions() {
197+
let query = format!(r#"create table instruments ( {} )"#, CURSOR_POS);
198+
assert_no_complete_results(query.as_str(), "").await;
199+
}
173200
}

crates/pgt_completions/src/test_helper.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ pub(crate) async fn assert_complete_results(
244244
pub(crate) async fn assert_no_complete_results(query: &str, setup: &str) {
245245
let (tree, cache) = get_test_deps(setup, query.into()).await;
246246
let params = get_test_params(&tree, &cache, query.into());
247+
println!("{:#?}", params.position);
248+
println!("{:#?}", params.text);
247249
let items = complete(params);
248250

249251
assert_eq!(items.len(), 0)

0 commit comments

Comments
 (0)