Skip to content

Commit dae4b9a

Browse files
ok so far
1 parent d4ece2e commit dae4b9a

File tree

9 files changed

+294459
-294650
lines changed

9 files changed

+294459
-294650
lines changed

crates/pgt_completions/src/providers/policies.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ mod tests {
8080

8181
pool.execute(setup).await.unwrap();
8282

83+
assert_complete_results(
84+
format!(
85+
"alter policy \"{}",
86+
QueryWithCursorPosition::cursor_marker()
87+
)
88+
.as_str(),
89+
vec![
90+
CompletionAssertion::Label("read for public users disallowed".into()),
91+
CompletionAssertion::Label("write for public users allowed".into()),
92+
],
93+
None,
94+
&pool,
95+
)
96+
.await;
97+
8398
assert_complete_results(
8499
format!(
85100
"alter policy \"{}\" on private.users;",

crates/pgt_completions/src/relevance/filtering.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ impl CompletionFilter<'_> {
233233
matches!(
234234
clause,
235235
// not CREATE – there can't be existing policies.
236-
WrappingClause::DropPolicy | WrappingClause::AlterPolicy
237-
)
236+
WrappingClause::AlterPolicy | WrappingClause::DropPolicy
237+
) && ctx.before_cursor_matches_kind(&["keyword_policy", "keyword_exists"])
238238
}
239239

240240
CompletionRelevanceData::Role(_) => match clause {
@@ -247,6 +247,7 @@ impl CompletionFilter<'_> {
247247

248248
WrappingClause::AlterPolicy | WrappingClause::CreatePolicy => {
249249
ctx.before_cursor_matches_kind(&["keyword_to"])
250+
&& ctx.matches_ancestor_history(&["policy_to_role"])
250251
}
251252

252253
_ => false,

crates/pgt_completions/src/relevance/scoring.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ impl CompletionScore<'_> {
126126
WrappingClause::Join { .. } if !has_mentioned_schema => 15,
127127
WrappingClause::Update if !has_mentioned_schema => 15,
128128
WrappingClause::Delete if !has_mentioned_schema => 15,
129+
WrappingClause::AlterPolicy if !has_mentioned_schema => 15,
130+
WrappingClause::DropPolicy if !has_mentioned_schema => 15,
131+
WrappingClause::CreatePolicy if !has_mentioned_schema => 15,
129132
_ => -50,
130133
},
131134
CompletionRelevanceData::Policy(_) => match clause_type {

crates/pgt_hover/src/hovered_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl HoveredNode {
8686
)))
8787
}
8888
}
89-
"identifier" if ctx.matches_ancestor_history(&["alter_role"]) => {
89+
"identifier" if ctx.matches_one_of_ancestors(&["alter_role", "policy_to_role"]) => {
9090
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
9191
}
9292

crates/pgt_treesitter/src/context/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,22 @@ impl<'a> TreesitterContext<'a> {
756756
})
757757
}
758758

759+
/// Verifies whether the node_under_cursor has the passed in ancestors in the right order.
760+
/// Note that you need to pass in the ancestors in the order as they would appear in the tree:
761+
///
762+
/// If the tree shows `relation > object_reference > identifier` and the "identifier" is a leaf node,
763+
/// you need to pass `&["relation", "object_reference"]`.
764+
pub fn matches_one_of_ancestors(&self, expected_ancestors: &[&'static str]) -> bool {
765+
self.node_under_cursor
766+
.as_ref()
767+
.is_some_and(|under_cursor| match under_cursor {
768+
NodeUnderCursor::TsNode(node) => node
769+
.parent()
770+
.is_some_and(|p| expected_ancestors.contains(&p.kind())),
771+
NodeUnderCursor::CustomNode { .. } => false,
772+
})
773+
}
774+
759775
/// Checks whether the Node under the cursor is the nth child of the parent.
760776
///
761777
/// ```

crates/pgt_treesitter_grammar/grammar.js

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,59 +1034,56 @@ module.exports = grammar({
10341034
)
10351035
)
10361036
),
1037-
optional(
1038-
seq(
1039-
$.keyword_to,
1040-
comma_list(
1041-
choice(
1042-
$.identifier,
1043-
$.keyword_public,
1044-
$.keyword_current_user,
1045-
$.keyword_current_role
1046-
),
1047-
true
1048-
)
1049-
)
1050-
),
1037+
optional($.policy_to_role),
10511038
optional($.check_or_using_clause)
10521039
),
10531040

10541041
alter_policy: ($) =>
10551042
seq(
1056-
$.keyword_alter,
1057-
$.keyword_policy,
1058-
$.identifier,
1059-
$.keyword_on,
1060-
$.object_reference,
1061-
choice(
1062-
seq($.keyword_rename, $.keyword_to, $.identifier),
1043+
seq($.keyword_alter, $.keyword_policy, $.identifier),
1044+
optional(
10631045
seq(
1064-
optional(
1065-
seq(
1066-
$.keyword_to,
1067-
choice(
1068-
$.identifier,
1069-
$.keyword_public,
1070-
$.keyword_current_role,
1071-
$.keyword_current_user,
1072-
$.keyword_session_user
1073-
)
1074-
)
1075-
),
1076-
optional($.check_or_using_clause)
1046+
$.keyword_on,
1047+
$.object_reference,
1048+
choice(
1049+
seq($.keyword_rename, $.keyword_to, $.identifier),
1050+
$.policy_to_role,
1051+
optional($.check_or_using_clause)
1052+
)
10771053
)
10781054
)
10791055
),
10801056

1057+
policy_to_role: ($) =>
1058+
seq(
1059+
$.keyword_to,
1060+
comma_list(
1061+
choice(
1062+
$.identifier,
1063+
$.keyword_public,
1064+
$.keyword_current_user,
1065+
$.keyword_current_role,
1066+
$.keyword_session_user
1067+
),
1068+
true
1069+
)
1070+
),
1071+
10811072
drop_policy: ($) =>
10821073
seq(
1083-
$.keyword_drop,
1084-
$.keyword_policy,
1085-
optional($._if_exists),
1086-
$.identifier,
1087-
$.keyword_on,
1088-
$.object_reference,
1089-
optional(choice($.keyword_cascade, $.keyword_restrict))
1074+
seq(
1075+
$.keyword_drop,
1076+
$.keyword_policy,
1077+
optional($._if_exists),
1078+
$.identifier
1079+
),
1080+
optional(
1081+
seq(
1082+
$.keyword_on,
1083+
$.object_reference,
1084+
optional(choice($.keyword_cascade, $.keyword_restrict))
1085+
)
1086+
)
10901087
),
10911088

10921089
check_or_using_clause: ($) =>

0 commit comments

Comments
 (0)