diff --git a/crates/pgt_hover/src/hovered_node.rs b/crates/pgt_hover/src/hovered_node.rs index 44c8041cb..2a637bf7d 100644 --- a/crates/pgt_hover/src/hovered_node.rs +++ b/crates/pgt_hover/src/hovered_node.rs @@ -64,6 +64,17 @@ impl HoveredNode { Some(HoveredNode::Role(NodeIdentification::Name(node_content))) } + "policy_table" | "revoke_table" | "grant_table" => { + if let Some(schema) = ctx.schema_or_alias_name.as_ref() { + Some(HoveredNode::Table(NodeIdentification::SchemaAndName(( + schema.clone(), + node_content, + )))) + } else { + Some(HoveredNode::Table(NodeIdentification::Name(node_content))) + } + } + _ => None, } } diff --git a/crates/pgt_hover/src/lib.rs b/crates/pgt_hover/src/lib.rs index f3b8a2640..3f0f893eb 100644 --- a/crates/pgt_hover/src/lib.rs +++ b/crates/pgt_hover/src/lib.rs @@ -20,6 +20,10 @@ pub struct OnHoverParams<'a> { pub ts_tree: &'a tree_sitter::Tree, } +#[tracing::instrument(level = "debug", skip_all, fields( + text = params.stmt_sql, + position = params.position.to_string() +))] pub fn on_hover(params: OnHoverParams) -> Vec { let ctx = pgt_treesitter::context::TreesitterContext::new(TreeSitterContextParams { position: params.position, diff --git a/crates/pgt_hover/tests/hover_integration_tests.rs b/crates/pgt_hover/tests/hover_integration_tests.rs index d63daea2e..c13f0c900 100644 --- a/crates/pgt_hover/tests/hover_integration_tests.rs +++ b/crates/pgt_hover/tests/hover_integration_tests.rs @@ -298,3 +298,60 @@ async fn test_role_hover_alter_role(test_db: PgPool) { test_hover_at_cursor("role_alter", query, None, &test_db).await; } + +#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")] +async fn test_policy_table_hover(test_db: PgPool) { + let setup = r#" + create table users ( + id serial primary key, + name text + ); + "#; + + test_db.execute(setup).await.unwrap(); + + let query = format!( + r#"create policy "my cool pol" on us{}ers for all to public with check (true);"#, + QueryWithCursorPosition::cursor_marker() + ); + + test_hover_at_cursor("create_policy", query, None, &test_db).await; +} + +#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")] +async fn test_revoke_table_hover(test_db: PgPool) { + let setup = r#" + create table users ( + id serial primary key, + name text + ); + "#; + + test_db.execute(setup).await.unwrap(); + + let query = format!( + "revoke select on us{}ers from public;", + QueryWithCursorPosition::cursor_marker() + ); + + test_hover_at_cursor("revoke_select", query, None, &test_db).await; +} + +#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")] +async fn test_grant_table_hover(test_db: PgPool) { + let setup = r#" + create table users ( + id serial primary key, + name text + ); + "#; + + test_db.execute(setup).await.unwrap(); + + let query = format!( + "grant select on us{}ers to public;", + QueryWithCursorPosition::cursor_marker() + ); + + test_hover_at_cursor("grant_select", query, None, &test_db).await; +} diff --git a/crates/pgt_hover/tests/snapshots/create_policy.snap b/crates/pgt_hover/tests/snapshots/create_policy.snap new file mode 100644 index 000000000..87c9aac5b --- /dev/null +++ b/crates/pgt_hover/tests/snapshots/create_policy.snap @@ -0,0 +1,20 @@ +--- +source: crates/pgt_hover/tests/hover_integration_tests.rs +expression: snapshot +--- +# Input +```sql +create policy "my cool pol" on users for all to public with check (true); + ↑ hovered here +``` + +# Hover Results +### `public.users` - 🔓 RLS disabled +```plain + +``` +--- +```plain + +~0 rows, ~0 dead rows, 16.38 kB +``` diff --git a/crates/pgt_hover/tests/snapshots/grant_select.snap b/crates/pgt_hover/tests/snapshots/grant_select.snap new file mode 100644 index 000000000..5eddd3cdf --- /dev/null +++ b/crates/pgt_hover/tests/snapshots/grant_select.snap @@ -0,0 +1,20 @@ +--- +source: crates/pgt_hover/tests/hover_integration_tests.rs +expression: snapshot +--- +# Input +```sql +grant select on users to public; + ↑ hovered here +``` + +# Hover Results +### `public.users` - 🔓 RLS disabled +```plain + +``` +--- +```plain + +~0 rows, ~0 dead rows, 16.38 kB +``` diff --git a/crates/pgt_hover/tests/snapshots/revoke_select.snap b/crates/pgt_hover/tests/snapshots/revoke_select.snap new file mode 100644 index 000000000..5550a0972 --- /dev/null +++ b/crates/pgt_hover/tests/snapshots/revoke_select.snap @@ -0,0 +1,20 @@ +--- +source: crates/pgt_hover/tests/hover_integration_tests.rs +expression: snapshot +--- +# Input +```sql +revoke select on users from public; + ↑ hovered here +``` + +# Hover Results +### `public.users` - 🔓 RLS disabled +```plain + +``` +--- +```plain + +~0 rows, ~0 dead rows, 16.38 kB +``` diff --git a/crates/pgt_lsp/src/handlers/hover.rs b/crates/pgt_lsp/src/handlers/hover.rs index 59d70ca74..ed7173551 100644 --- a/crates/pgt_lsp/src/handlers/hover.rs +++ b/crates/pgt_lsp/src/handlers/hover.rs @@ -3,6 +3,7 @@ use tower_lsp::lsp_types::{self, MarkedString, MarkupContent}; use crate::{adapters::get_cursor_position, diagnostics::LspError, session::Session}; +#[tracing::instrument(level = "debug", skip(session), err)] pub(crate) fn on_hover( session: &Session, params: lsp_types::HoverParams, diff --git a/crates/pgt_workspace/src/workspace/server.rs b/crates/pgt_workspace/src/workspace/server.rs index 6812c246c..b13a34422 100644 --- a/crates/pgt_workspace/src/workspace/server.rs +++ b/crates/pgt_workspace/src/workspace/server.rs @@ -719,6 +719,11 @@ impl Workspace for WorkspaceServer { } } + #[ignored_path(path=¶ms.path)] + #[tracing::instrument(level = "debug", skip_all, fields( + path = params.path.as_os_str().to_str(), + position = params.position.to_string() + ), err)] fn on_hover(&self, params: OnHoverParams) -> Result { let documents = self.documents.read().unwrap(); let doc = documents @@ -742,7 +747,7 @@ impl Workspace for WorkspaceServer { .next() { Some((stmt_id, range, ts_tree, maybe_ast)) => { - let position_in_stmt = params.position + range.start(); + let position_in_stmt = params.position - range.start(); let markdown_blocks = pgt_hover::on_hover(pgt_hover::OnHoverParams { ts_tree: &ts_tree,