Skip to content

Commit 586b3a2

Browse files
[5] refactor(treesitter): use declarative node names instead of context checking (#571)
1 parent 2eca24d commit 586b3a2

File tree

8 files changed

+103
-113
lines changed

8 files changed

+103
-113
lines changed

crates/pgt_completions/src/relevance/filtering.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ impl<'a> From<CompletionRelevanceData<'a>> for CompletionFilter<'a> {
1717
impl CompletionFilter<'_> {
1818
pub fn is_relevant(&self, ctx: &TreesitterContext) -> Option<()> {
1919
self.completable_context(ctx)?;
20-
self.check_clause(ctx)?;
20+
21+
self.check_node_type(ctx)
22+
// we want to rely on treesitter more, so checking the clause is a fallback
23+
.or_else(|| self.check_clause(ctx))?;
24+
2125
self.check_invocation(ctx)?;
2226
self.check_mentioned_schema_or_alias(ctx)?;
2327

@@ -88,6 +92,21 @@ impl CompletionFilter<'_> {
8892
Some(())
8993
}
9094

95+
fn check_node_type(&self, ctx: &TreesitterContext) -> Option<()> {
96+
let kind = ctx.node_under_cursor.as_ref().map(|n| n.kind())?;
97+
98+
let is_allowed = match kind {
99+
"column_identifier" => {
100+
matches!(self.data, CompletionRelevanceData::Column(_))
101+
&& !ctx.matches_ancestor_history(&["insert_values", "field"])
102+
&& !ctx.node_under_cursor_is_within_field_name("binary_expr_right")
103+
}
104+
_ => false,
105+
};
106+
107+
if is_allowed { Some(()) } else { None }
108+
}
109+
91110
fn check_clause(&self, ctx: &TreesitterContext) -> Option<()> {
92111
ctx.wrapping_clause_type
93112
.as_ref()

crates/pgt_hover/src/hovered_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl HoveredNode {
7373
}
7474
}
7575

76-
"any_identifier" if ctx.matches_ancestor_history(&["field"]) => {
76+
"column_identifier" => {
7777
if let Some(table_or_alias) = ctx.schema_or_alias_name.as_ref() {
7878
Some(HoveredNode::Column(NodeIdentification::SchemaAndName((
7979
table_or_alias.clone(),

crates/pgt_treesitter/src/context/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,7 @@ impl<'a> TreesitterContext<'a> {
590590
// `node.child_by_field_id(..)` does not work as expected
591591
let mut on_node = None;
592592
for child in node.children(cursor) {
593-
// 28 is the id for "keyword_on"
594-
if child.kind_id() == 28 {
593+
if child.kind() == "keyword_on" {
595594
on_node = Some(child);
596595
}
597596
}

crates/pgt_treesitter/src/queries/insert_columns.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@ use super::QueryTryFrom;
77

88
static TS_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
99
static QUERY_STR: &str = r#"
10-
(insert
11-
(object_reference)
12-
(list
13-
"("?
14-
(column) @column
15-
","?
16-
")"?
17-
)
10+
(insert_columns
11+
(column_identifier) @column
1812
)
1913
"#;
2014
tree_sitter::Query::new(&pgt_treesitter_grammar::LANGUAGE.into(), QUERY_STR)

crates/pgt_treesitter/src/queries/parameters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static TS_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
1111
[
1212
(field
1313
(field_qualifier)?
14-
(any_identifier)
14+
(column_identifier)
1515
) @reference
1616
1717
(parameter) @parameter

crates/pgt_treesitter/src/queries/select_columns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static TS_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
1414
(object_reference) @alias
1515
"."
1616
)?
17-
(any_identifier) @column
17+
(column_identifier) @column
1818
)
1919
)
2020
","?

crates/pgt_treesitter/src/queries/where_columns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static TS_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
1616
(object_reference) @alias
1717
"."
1818
)?
19-
(any_identifier) @column
19+
(column_identifier) @column
2020
)
2121
)
2222
)

0 commit comments

Comments
 (0)