Skip to content

Commit 2f2d900

Browse files
fix(hover): hover multiple statements, hover table in policy/revoke/grant (#500)
1 parent 7c62440 commit 2f2d900

File tree

8 files changed

+139
-1
lines changed

8 files changed

+139
-1
lines changed

crates/pgt_hover/src/hovered_node.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ impl HoveredNode {
6464
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
6565
}
6666

67+
"policy_table" | "revoke_table" | "grant_table" => {
68+
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
69+
Some(HoveredNode::Table(NodeIdentification::SchemaAndName((
70+
schema.clone(),
71+
node_content,
72+
))))
73+
} else {
74+
Some(HoveredNode::Table(NodeIdentification::Name(node_content)))
75+
}
76+
}
77+
6778
_ => None,
6879
}
6980
}

crates/pgt_hover/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ pub struct OnHoverParams<'a> {
2020
pub ts_tree: &'a tree_sitter::Tree,
2121
}
2222

23+
#[tracing::instrument(level = "debug", skip_all, fields(
24+
text = params.stmt_sql,
25+
position = params.position.to_string()
26+
))]
2327
pub fn on_hover(params: OnHoverParams) -> Vec<String> {
2428
let ctx = pgt_treesitter::context::TreesitterContext::new(TreeSitterContextParams {
2529
position: params.position,

crates/pgt_hover/tests/hover_integration_tests.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,60 @@ async fn test_role_hover_alter_role(test_db: PgPool) {
298298

299299
test_hover_at_cursor("role_alter", query, None, &test_db).await;
300300
}
301+
302+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
303+
async fn test_policy_table_hover(test_db: PgPool) {
304+
let setup = r#"
305+
create table users (
306+
id serial primary key,
307+
name text
308+
);
309+
"#;
310+
311+
test_db.execute(setup).await.unwrap();
312+
313+
let query = format!(
314+
r#"create policy "my cool pol" on us{}ers for all to public with check (true);"#,
315+
QueryWithCursorPosition::cursor_marker()
316+
);
317+
318+
test_hover_at_cursor("create_policy", query, None, &test_db).await;
319+
}
320+
321+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
322+
async fn test_revoke_table_hover(test_db: PgPool) {
323+
let setup = r#"
324+
create table users (
325+
id serial primary key,
326+
name text
327+
);
328+
"#;
329+
330+
test_db.execute(setup).await.unwrap();
331+
332+
let query = format!(
333+
"revoke select on us{}ers from public;",
334+
QueryWithCursorPosition::cursor_marker()
335+
);
336+
337+
test_hover_at_cursor("revoke_select", query, None, &test_db).await;
338+
}
339+
340+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
341+
async fn test_grant_table_hover(test_db: PgPool) {
342+
let setup = r#"
343+
create table users (
344+
id serial primary key,
345+
name text
346+
);
347+
"#;
348+
349+
test_db.execute(setup).await.unwrap();
350+
351+
let query = format!(
352+
"grant select on us{}ers to public;",
353+
QueryWithCursorPosition::cursor_marker()
354+
);
355+
356+
test_hover_at_cursor("grant_select", query, None, &test_db).await;
357+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: crates/pgt_hover/tests/hover_integration_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```sql
7+
create policy "my cool pol" on users for all to public with check (true);
8+
↑ hovered here
9+
```
10+
11+
# Hover Results
12+
### `public.users` - 🔓 RLS disabled
13+
```plain
14+
15+
```
16+
---
17+
```plain
18+
19+
~0 rows, ~0 dead rows, 16.38 kB
20+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: crates/pgt_hover/tests/hover_integration_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```sql
7+
grant select on users to public;
8+
↑ hovered here
9+
```
10+
11+
# Hover Results
12+
### `public.users` - 🔓 RLS disabled
13+
```plain
14+
15+
```
16+
---
17+
```plain
18+
19+
~0 rows, ~0 dead rows, 16.38 kB
20+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: crates/pgt_hover/tests/hover_integration_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```sql
7+
revoke select on users from public;
8+
↑ hovered here
9+
```
10+
11+
# Hover Results
12+
### `public.users` - 🔓 RLS disabled
13+
```plain
14+
15+
```
16+
---
17+
```plain
18+
19+
~0 rows, ~0 dead rows, 16.38 kB
20+
```

crates/pgt_lsp/src/handlers/hover.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use tower_lsp::lsp_types::{self, MarkedString, MarkupContent};
33

44
use crate::{adapters::get_cursor_position, diagnostics::LspError, session::Session};
55

6+
#[tracing::instrument(level = "debug", skip(session), err)]
67
pub(crate) fn on_hover(
78
session: &Session,
89
params: lsp_types::HoverParams,

crates/pgt_workspace/src/workspace/server.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,11 @@ impl Workspace for WorkspaceServer {
719719
}
720720
}
721721

722+
#[ignored_path(path=&params.path)]
723+
#[tracing::instrument(level = "debug", skip_all, fields(
724+
path = params.path.as_os_str().to_str(),
725+
position = params.position.to_string()
726+
), err)]
722727
fn on_hover(&self, params: OnHoverParams) -> Result<OnHoverResult, WorkspaceError> {
723728
let documents = self.documents.read().unwrap();
724729
let doc = documents
@@ -742,7 +747,7 @@ impl Workspace for WorkspaceServer {
742747
.next()
743748
{
744749
Some((stmt_id, range, ts_tree, maybe_ast)) => {
745-
let position_in_stmt = params.position + range.start();
750+
let position_in_stmt = params.position - range.start();
746751

747752
let markdown_blocks = pgt_hover::on_hover(pgt_hover::OnHoverParams {
748753
ts_tree: &ts_tree,

0 commit comments

Comments
 (0)