Skip to content

Commit 17b5b5e

Browse files
merged
2 parents 7484ccd + 98ae38f commit 17b5b5e

File tree

16 files changed

+303
-14
lines changed

16 files changed

+303
-14
lines changed

crates/pgt_hover/src/hovered_node.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ impl HoveredNode {
6969
Some(HoveredNode::Column(NodeIdentification::Name(node_content)))
7070
}
7171

72+
"policy_table" | "revoke_table" | "grant_table" => {
73+
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
74+
Some(HoveredNode::Table(NodeIdentification::SchemaAndName((
75+
schema.clone(),
76+
node_content,
77+
))))
78+
} else {
79+
Some(HoveredNode::Table(NodeIdentification::Name(node_content)))
80+
}
81+
}
82+
7283
_ => None,
7384
}
7485
}

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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,59 @@ async fn test_column_hover_with_quoted_column_name_with_table(test_db: PgPool) {
442442
)
443443
.await;
444444
}
445+
446+
async fn test_policy_table_hover(test_db: PgPool) {
447+
let setup = r#"
448+
create table users (
449+
id serial primary key,
450+
name text
451+
);
452+
"#;
453+
454+
test_db.execute(setup).await.unwrap();
455+
456+
let query = format!(
457+
r#"create policy "my cool pol" on us{}ers for all to public with check (true);"#,
458+
QueryWithCursorPosition::cursor_marker()
459+
);
460+
461+
test_hover_at_cursor("create_policy", query, None, &test_db).await;
462+
}
463+
464+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
465+
async fn test_revoke_table_hover(test_db: PgPool) {
466+
let setup = r#"
467+
create table users (
468+
id serial primary key,
469+
name text
470+
);
471+
"#;
472+
473+
test_db.execute(setup).await.unwrap();
474+
475+
let query = format!(
476+
"revoke select on us{}ers from public;",
477+
QueryWithCursorPosition::cursor_marker()
478+
);
479+
480+
test_hover_at_cursor("revoke_select", query, None, &test_db).await;
481+
}
482+
483+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
484+
async fn test_grant_table_hover(test_db: PgPool) {
485+
let setup = r#"
486+
create table users (
487+
id serial primary key,
488+
name text
489+
);
490+
"#;
491+
492+
test_db.execute(setup).await.unwrap();
493+
494+
let query = format!(
495+
"grant select on us{}ers to public;",
496+
QueryWithCursorPosition::cursor_marker()
497+
);
498+
499+
test_hover_at_cursor("grant_select", query, None, &test_db).await;
500+
}
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_lexer/src/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'a> Lexer<'a> {
143143
}
144144
_ => {}
145145
};
146-
SyntaxKind::POSITIONAL_PARAM
146+
SyntaxKind::NAMED_PARAM
147147
}
148148
pgt_tokenizer::TokenKind::QuotedIdent { terminated } => {
149149
if !terminated {

crates/pgt_lexer_codegen/src/syntax_kind.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ pub fn syntax_kind_mod() -> proc_macro2::TokenStream {
6565

6666
let mut enum_variants: Vec<TokenStream> = Vec::new();
6767
let mut from_kw_match_arms: Vec<TokenStream> = Vec::new();
68+
let mut is_kw_match_arms: Vec<TokenStream> = Vec::new();
69+
70+
let mut is_trivia_match_arms: Vec<TokenStream> = Vec::new();
6871

6972
// collect keywords
7073
for kw in &all_keywords {
@@ -78,18 +81,30 @@ pub fn syntax_kind_mod() -> proc_macro2::TokenStream {
7881
from_kw_match_arms.push(quote! {
7982
#kw => Some(SyntaxKind::#kind_ident)
8083
});
84+
is_kw_match_arms.push(quote! {
85+
SyntaxKind::#kind_ident => true
86+
});
8187
}
8288

8389
// collect extra keywords
8490
EXTRA.iter().for_each(|&name| {
8591
let variant_name = format_ident!("{}", name);
8692
enum_variants.push(quote! { #variant_name });
93+
94+
if name == "COMMENT" {
95+
is_trivia_match_arms.push(quote! {
96+
SyntaxKind::#variant_name => true
97+
});
98+
}
8799
});
88100

89101
// collect whitespace variants
90102
WHITESPACE.iter().for_each(|&name| {
91103
let variant_name = format_ident!("{}", name);
92104
enum_variants.push(quote! { #variant_name });
105+
is_trivia_match_arms.push(quote! {
106+
SyntaxKind::#variant_name => true
107+
});
93108
});
94109

95110
// collect punctuations
@@ -119,6 +134,20 @@ pub fn syntax_kind_mod() -> proc_macro2::TokenStream {
119134
_ => None
120135
}
121136
}
137+
138+
pub fn is_keyword(&self) -> bool {
139+
match self {
140+
#(#is_kw_match_arms),*,
141+
_ => false
142+
}
143+
}
144+
145+
pub fn is_trivia(&self) -> bool {
146+
match self {
147+
#(#is_trivia_match_arms),*,
148+
_ => false
149+
}
150+
}
122151
}
123152
}
124153
}

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_query_macros/src/iter_mut.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ pub fn iter_mut_mod(analyser: ProtoAnalyzer) -> proc_macro2::TokenStream {
1919

2020
for node in &nodes {
2121
// Use the enum variant name from the Node enum
22-
if let Some(variant_name) = type_to_variant.get(&node.name) {
22+
if let Some(variant_name) = type_to_variant.get(&node.enum_variant_name) {
2323
let variant_ident = format_ident!("{}", variant_name);
2424
node_variant_names.push(variant_ident);
2525

2626
let property_handlers = property_handlers(node);
2727
node_property_handlers.push(property_handlers);
28+
} else {
29+
panic!(
30+
"No enum variant found for node type: {}",
31+
node.enum_variant_name
32+
);
2833
}
2934
}
3035

0 commit comments

Comments
 (0)