Skip to content

Commit dd3ba82

Browse files
[4] refactor: rename "identifier" node to "any_identifier" (#570)
The plan is to introduce more specific nodes, such as "column_identifier", "table_identifier" and so on, which should allow more specific completions.
1 parent 2aa5c9f commit dd3ba82

File tree

10 files changed

+261
-245
lines changed

10 files changed

+261
-245
lines changed

crates/pgt_completions/src/relevance/filtering.rs

Lines changed: 24 additions & 5 deletions
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

@@ -69,8 +73,8 @@ impl CompletionFilter<'_> {
6973
// No autocompletions if there are two identifiers without a separator.
7074
if ctx.node_under_cursor.as_ref().is_some_and(|node| {
7175
node.prev_sibling().is_some_and(|p| {
72-
(p.kind() == "identifier" || p.kind() == "object_reference")
73-
&& node.kind() == "identifier"
76+
(p.kind() == "any_identifier" || p.kind() == "object_reference")
77+
&& node.kind() == "any_identifier"
7478
})
7579
}) {
7680
return None;
@@ -80,14 +84,29 @@ impl CompletionFilter<'_> {
8084
// `select * {}`
8185
if ctx.node_under_cursor.as_ref().is_some_and(|node| {
8286
node.prev_sibling()
83-
.is_some_and(|p| (p.kind() == "all_fields") && node.kind() == "identifier")
87+
.is_some_and(|p| (p.kind() == "all_fields") && node.kind() == "any_identifier")
8488
}) {
8589
return None;
8690
}
8791

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()
@@ -254,7 +273,7 @@ impl CompletionFilter<'_> {
254273
WrappingClause::RevokeStatement | WrappingClause::GrantStatement => {
255274
ctx.matches_ancestor_history(&["role_specification"])
256275
|| ctx.node_under_cursor.as_ref().is_some_and(|k| {
257-
k.kind() == "identifier"
276+
k.kind() == "any_identifier"
258277
&& ctx.before_cursor_matches_kind(&[
259278
"keyword_grant",
260279
"keyword_revoke",

crates/pgt_hover/src/hovered_node.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl HoveredNode {
3232
let under_cursor = ctx.node_under_cursor.as_ref()?;
3333

3434
match under_cursor.kind() {
35-
"identifier"
35+
"any_identifier"
3636
if ctx.matches_ancestor_history(&["relation", "object_reference"])
3737
|| ctx
3838
.matches_ancestor_history(&["grantable_on_table", "object_reference"]) =>
@@ -52,7 +52,7 @@ impl HoveredNode {
5252
}
5353
}
5454

55-
"identifier"
55+
"any_identifier"
5656
if ctx.matches_ancestor_history(&["object_reference"])
5757
&& ctx.wrapping_clause_type.as_ref().is_some_and(|clause| {
5858
matches!(
@@ -73,7 +73,7 @@ impl HoveredNode {
7373
}
7474
}
7575

76-
"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(),
@@ -84,7 +84,9 @@ impl HoveredNode {
8484
}
8585
}
8686

87-
"identifier" if ctx.matches_ancestor_history(&["invocation", "object_reference"]) => {
87+
"any_identifier"
88+
if ctx.matches_ancestor_history(&["invocation", "object_reference"]) =>
89+
{
8890
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
8991
Some(HoveredNode::Function(NodeIdentification::SchemaAndName((
9092
schema.clone(),
@@ -97,7 +99,7 @@ impl HoveredNode {
9799
}
98100
}
99101

100-
"identifier"
102+
"any_identifier"
101103
if ctx.matches_one_of_ancestors(&[
102104
"alter_role",
103105
"policy_to_role",
@@ -110,7 +112,7 @@ impl HoveredNode {
110112
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
111113
}
112114

113-
"identifier"
115+
"any_identifier"
114116
if (
115117
// hover over custom type in `create table` or `returns`
116118
(ctx.matches_ancestor_history(&["type", "object_reference"])

crates/pgt_treesitter/src/context/mod.rs

Lines changed: 8 additions & 9 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
}
@@ -620,7 +619,7 @@ impl<'a> TreesitterContext<'a> {
620619
/// Verifies whether the node_under_cursor has the passed in ancestors in the right order.
621620
/// Note that you need to pass in the ancestors in the order as they would appear in the tree:
622621
///
623-
/// If the tree shows `relation > object_reference > identifier` and the "identifier" is a leaf node,
622+
/// If the tree shows `relation > object_reference > any_identifier` and the "any_identifier" is a leaf node,
624623
/// you need to pass `&["relation", "object_reference"]`.
625624
pub fn matches_ancestor_history(&self, expected_ancestors: &[&'static str]) -> bool {
626625
self.node_under_cursor.as_ref().is_some_and(|node| {
@@ -642,7 +641,7 @@ impl<'a> TreesitterContext<'a> {
642641
/// Verifies whether the node_under_cursor has the passed in ancestors in the right order.
643642
/// Note that you need to pass in the ancestors in the order as they would appear in the tree:
644643
///
645-
/// If the tree shows `relation > object_reference > identifier` and the "identifier" is a leaf node,
644+
/// If the tree shows `relation > object_reference > any_identifier` and the "any_identifier" is a leaf node,
646645
/// you need to pass `&["relation", "object_reference"]`.
647646
pub fn matches_one_of_ancestors(&self, expected_ancestors: &[&'static str]) -> bool {
648647
self.node_under_cursor.as_ref().is_some_and(|node| {
@@ -663,9 +662,9 @@ impl<'a> TreesitterContext<'a> {
663662
/// * keyword_from [9..13] 'from'
664663
/// * relation [14..28] '"auth"."users"'
665664
/// * object_reference [14..28] '"auth"."users"'
666-
/// * identifier [14..20] '"auth"'
665+
/// * any_identifier [14..20] '"auth"'
667666
/// * . [20..21] '.'
668-
/// * identifier [21..28] '"users"'
667+
/// * any_identifier [21..28] '"users"'
669668
/// */
670669
///
671670
/// if node_under_cursor_is_nth_child(1) {
@@ -1132,13 +1131,13 @@ mod tests {
11321131
keyword_where [29..34] 'where'
11331132
binary_expression [35..43] 'id = @id'
11341133
field [35..37] 'id'
1135-
identifier [35..37] 'id'
1134+
any_identifier [35..37] 'id'
11361135
= [38..39] '='
11371136
field [40..43] '@id'
1138-
identifier [40..43] '@id'
1137+
any_identifier [40..43] '@id'
11391138
@ [40..41] '@'
11401139
1141-
You can see that the '@' is a child of the "identifier" but has a range smaller than its parent's.
1140+
You can see that the '@' is a child of the "any_identifier" but has a range smaller than its parent's.
11421141
This would crash our context parsing because, at position 42, we weren't at the leaf node but also couldn't
11431142
go to a child on that position.
11441143
*/

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-
(identifier)
14+
(column_identifier)
1515
) @reference
1616
1717
(parameter) @parameter

crates/pgt_treesitter/src/queries/relations.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ static TS_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
1111
(relation
1212
(object_reference
1313
.
14-
(identifier) @schema_or_table
14+
(any_identifier) @schema_or_table
1515
"."?
16-
(identifier)? @table
16+
(any_identifier)? @table
1717
)+
1818
)
1919
(insert
2020
(object_reference
2121
.
22-
(identifier) @schema_or_table
22+
(any_identifier) @schema_or_table
2323
"."?
24-
(identifier)? @table
24+
(any_identifier)? @table
2525
)+
2626
)
2727
(alter_table
2828
(keyword_alter)
2929
(keyword_table)
3030
(object_reference
3131
.
32-
(identifier) @schema_or_table
32+
(any_identifier) @schema_or_table
3333
"."?
34-
(identifier)? @table
34+
(any_identifier)? @table
3535
)+
3636
)
3737
"#;

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-
(identifier) @column
17+
(column_identifier) @column
1818
)
1919
)
2020
","?

crates/pgt_treesitter/src/queries/table_aliases.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ static TS_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
1010
(relation
1111
(object_reference
1212
.
13-
(identifier) @schema_or_table
13+
(any_identifier) @schema_or_table
1414
"."?
15-
(identifier)? @table
15+
(any_identifier)? @table
1616
)
1717
(keyword_as)?
18-
(identifier) @alias
18+
(any_identifier) @alias
1919
)
2020
"#;
2121
tree_sitter::Query::new(&pgt_treesitter_grammar::LANGUAGE.into(), QUERY_STR)

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-
(identifier) @column
19+
(column_identifier) @column
2020
)
2121
)
2222
)

0 commit comments

Comments
 (0)