Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/pgt_completions/src/providers/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ mod tests {
QueryWithCursorPosition::cursor_marker()
),
format!(
"revoke all on table userse from owner, {}",
"revoke all on table users from owner, {}",
QueryWithCursorPosition::cursor_marker()
),
];
Expand Down
21 changes: 21 additions & 0 deletions crates/pgt_completions/src/relevance/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ impl CompletionFilter<'_> {
CompletionRelevanceData::Table(_) => match clause {
WrappingClause::From | WrappingClause::Update => true,

WrappingClause::RevokeStatement => {
ctx.matches_ancestor_history(&["revoke_on_table", "object_reference"])
}

WrappingClause::Join { on_node: None } => true,
WrappingClause::Join { on_node: Some(on) } => ctx
.node_under_cursor
Expand Down Expand Up @@ -202,6 +206,12 @@ impl CompletionFilter<'_> {
| WrappingClause::Update
| WrappingClause::Delete => true,

WrappingClause::RevokeStatement => {
(ctx.matches_ancestor_history(&["revoke_on_table", "object_reference"])
&& ctx.schema_or_alias_name.is_none())
|| ctx.matches_ancestor_history(&["revoke_on_all"])
}

WrappingClause::Where => {
ctx.before_cursor_matches_kind(&["keyword_and", "keyword_where"])
}
Expand Down Expand Up @@ -245,6 +255,17 @@ impl CompletionFilter<'_> {
WrappingClause::SetStatement => ctx
.before_cursor_matches_kind(&["keyword_role", "keyword_authorization"]),

WrappingClause::RevokeStatement => {
ctx.matches_ancestor_history(&["role_specification"])
|| ctx.node_under_cursor.as_ref().is_some_and(|k| {
k.kind() == "identifier"
&& ctx.before_cursor_matches_kind(&[
"keyword_revoke",
"keyword_for",
])
})
}

WrappingClause::AlterPolicy | WrappingClause::CreatePolicy => {
ctx.before_cursor_matches_kind(&["keyword_to"])
&& ctx.matches_ancestor_history(&["policy_to_role"])
Expand Down
22 changes: 15 additions & 7 deletions crates/pgt_hover/src/hovered_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ impl HoveredNode {
let under_cursor = ctx.node_under_cursor.as_ref()?;

match under_cursor.kind() {
"identifier" if ctx.matches_ancestor_history(&["relation", "object_reference"]) => {
"identifier"
if ctx.matches_ancestor_history(&["relation", "object_reference"])
|| ctx.matches_ancestor_history(&["revoke_on_table", "object_reference"]) =>
{
let num_sibs = ctx.num_siblings();
if ctx.node_under_cursor_is_nth_child(1) && num_sibs > 0 {
return Some(HoveredNode::Schema(NodeIdentification::Name(node_content)));
Expand Down Expand Up @@ -93,7 +96,16 @@ impl HoveredNode {
}
}

"identifier" if ctx.matches_one_of_ancestors(&["alter_role", "policy_to_role"]) => {
"identifier"
if ctx.matches_one_of_ancestors(&[
"alter_role",
"policy_to_role",
"role_specification",
]) || ctx.before_cursor_matches_kind(&["keyword_revoke"]) =>
{
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
}
"grant_role" | "policy_role" => {
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
}

Expand Down Expand Up @@ -127,16 +139,12 @@ impl HoveredNode {
}
}

"revoke_role" | "grant_role" | "policy_role" => {
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
}

// quoted columns
"literal" if ctx.matches_ancestor_history(&["select_expression", "term"]) => {
Some(HoveredNode::Column(NodeIdentification::Name(node_content)))
}

"revoke_table" | "grant_table" => {
"grant_table" => {
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
Some(HoveredNode::Table(NodeIdentification::SchemaAndName((
schema.clone(),
Expand Down
6 changes: 0 additions & 6 deletions crates/pgt_treesitter/src/context/base_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ impl TokenNavigator {
.is_some_and(|c| options.contains(&c.get_word_without_quotes().as_str()))
}

pub(crate) fn prev_matches(&self, options: &[&str]) -> bool {
self.previous_token
.as_ref()
.is_some_and(|t| options.contains(&t.get_word_without_quotes().as_str()))
}

pub(crate) fn advance(&mut self) -> Option<WordWithIndex> {
// we can't peek back n an iterator, so we'll have to keep track manually.
self.previous_token = self.current_token.take();
Expand Down
37 changes: 3 additions & 34 deletions crates/pgt_treesitter/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ use std::{
};
mod base_parser;
mod grant_parser;
mod revoke_parser;

use crate::queries::{self, QueryResult, TreeSitterQueriesExecutor};
use pgt_text_size::{TextRange, TextSize};

use crate::context::{
base_parser::CompletionStatementParser, grant_parser::GrantParser, revoke_parser::RevokeParser,
};
use crate::context::{base_parser::CompletionStatementParser, grant_parser::GrantParser};

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum WrappingClause<'a> {
Expand All @@ -34,6 +31,7 @@ pub enum WrappingClause<'a> {
SetStatement,
AlterRole,
DropRole,
RevokeStatement,

CreatePolicy,
AlterPolicy,
Expand Down Expand Up @@ -198,8 +196,6 @@ impl<'a> TreesitterContext<'a> {

if GrantParser::looks_like_matching_stmt(params.text) {
ctx.gather_grant_context();
} else if RevokeParser::looks_like_matching_stmt(params.text) {
ctx.gather_revoke_context();
} else {
ctx.gather_tree_context();
ctx.gather_info_from_ts_queries();
Expand All @@ -208,34 +204,6 @@ impl<'a> TreesitterContext<'a> {
ctx
}

fn gather_revoke_context(&mut self) {
let revoke_context = RevokeParser::get_context(self.text, self.position);

self.node_under_cursor = Some(NodeUnderCursor::CustomNode {
text: revoke_context.node_text,
range: revoke_context.node_range,
kind: revoke_context.node_kind.clone(),
previous_node_kind: None,
});

if revoke_context.node_kind == "revoke_table" {
self.schema_or_alias_name = revoke_context.schema_name.clone();
}

if revoke_context.table_name.is_some() {
let mut new = HashSet::new();
new.insert(revoke_context.table_name.unwrap());
self.mentioned_relations
.insert(revoke_context.schema_name, new);
}

self.wrapping_clause_type = match revoke_context.node_kind.as_str() {
"revoke_role" => Some(WrappingClause::ToRoleAssignment),
"revoke_table" => Some(WrappingClause::From),
_ => None,
};
}

fn gather_grant_context(&mut self) {
let grant_context = GrantParser::get_context(self.text, self.position);

Expand Down Expand Up @@ -686,6 +654,7 @@ impl<'a> TreesitterContext<'a> {
"rename_column" => Some(WrappingClause::RenameColumn),
"alter_table" => Some(WrappingClause::AlterTable),
"set_statement" => Some(WrappingClause::SetStatement),
"revoke_statement" => Some(WrappingClause::RevokeStatement),
"column_definitions" => Some(WrappingClause::ColumnDefinitions),
"create_policy" => Some(WrappingClause::CreatePolicy),
"alter_policy" => Some(WrappingClause::AlterPolicy),
Expand Down
Loading