Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions crates/pgt_hover/src/hovered_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/pgt_hover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let ctx = pgt_treesitter::context::TreesitterContext::new(TreeSitterContextParams {
position: params.position,
Expand Down
57 changes: 57 additions & 0 deletions crates/pgt_hover/tests/hover_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
20 changes: 20 additions & 0 deletions crates/pgt_hover/tests/snapshots/create_policy.snap
Original file line number Diff line number Diff line change
@@ -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
```
20 changes: 20 additions & 0 deletions crates/pgt_hover/tests/snapshots/grant_select.snap
Original file line number Diff line number Diff line change
@@ -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
```
20 changes: 20 additions & 0 deletions crates/pgt_hover/tests/snapshots/revoke_select.snap
Original file line number Diff line number Diff line change
@@ -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
```
1 change: 1 addition & 0 deletions crates/pgt_lsp/src/handlers/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion crates/pgt_workspace/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,11 @@ impl Workspace for WorkspaceServer {
}
}

#[ignored_path(path=&params.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<OnHoverResult, WorkspaceError> {
let documents = self.documents.read().unwrap();
let doc = documents
Expand All @@ -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();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this led to errors when we had multiple statements in a file. Only the first could be hovered


let markdown_blocks = pgt_hover::on_hover(pgt_hover::OnHoverParams {
ts_tree: &ts_tree,
Expand Down