Skip to content

Commit 43df7de

Browse files
fix: do not display hover items when hovering over param (#553)
As reported in #528
1 parent 193671c commit 43df7de

File tree

7 files changed

+102
-2
lines changed

7 files changed

+102
-2
lines changed

crates/pgt_hover/src/hovered_node.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ impl HoveredNode {
2424
pub(crate) fn get(ctx: &pgt_treesitter::context::TreesitterContext) -> Option<Self> {
2525
let node_content = ctx.get_node_under_cursor_content()?;
2626

27+
if looks_like_sql_param(node_content.as_str()) {
28+
return None;
29+
}
30+
2731
let under_cursor = ctx.node_under_cursor.as_ref()?;
2832

2933
match under_cursor.kind() {
@@ -114,3 +118,10 @@ impl HoveredNode {
114118
}
115119
}
116120
}
121+
122+
fn looks_like_sql_param(content: &str) -> bool {
123+
(content.starts_with("$") && !content.starts_with("$$"))
124+
|| (content.starts_with(":") && !content.starts_with("::"))
125+
|| (content.starts_with("@"))
126+
|| content.starts_with("?")
127+
}

crates/pgt_hover/tests/hover_integration_tests.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,44 @@ async fn test_grant_table_hover(test_db: PgPool) {
518518

519519
test_hover_at_cursor("grant_select", query, None, &test_db).await;
520520
}
521+
522+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
523+
async fn no_hover_results_over_params(test_db: PgPool) {
524+
let setup = r#"
525+
create table users (
526+
id serial primary key,
527+
name text
528+
);
529+
"#;
530+
531+
test_db.execute(setup).await.unwrap();
532+
533+
{
534+
let query = format!(
535+
"select * from users where name = $n{}ame;",
536+
QueryWithCursorPosition::cursor_marker()
537+
);
538+
test_hover_at_cursor("dollar-param", query, None, &test_db).await;
539+
}
540+
{
541+
let query = format!(
542+
"select * from users where name = :n{}ame;",
543+
QueryWithCursorPosition::cursor_marker()
544+
);
545+
test_hover_at_cursor("colon-param", query, None, &test_db).await;
546+
}
547+
{
548+
let query = format!(
549+
"select * from users where name = @n{}ame;",
550+
QueryWithCursorPosition::cursor_marker()
551+
);
552+
test_hover_at_cursor("at-param", query, None, &test_db).await;
553+
}
554+
{
555+
let query = format!(
556+
"select * from users where name = ?n{}ame;",
557+
QueryWithCursorPosition::cursor_marker()
558+
);
559+
test_hover_at_cursor("questionmark-param", query, None, &test_db).await;
560+
}
561+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/pgt_hover/tests/hover_integration_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```sql
7+
select * from users where name = @name;
8+
↑ hovered here
9+
```
10+
11+
# Hover Results
12+
No hover information found.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/pgt_hover/tests/hover_integration_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```sql
7+
select * from users where name = :name;
8+
↑ hovered here
9+
```
10+
11+
# Hover Results
12+
No hover information found.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/pgt_hover/tests/hover_integration_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```sql
7+
select * from users where name = $name;
8+
↑ hovered here
9+
```
10+
11+
# Hover Results
12+
No hover information found.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/pgt_hover/tests/hover_integration_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```sql
7+
select * from users where name = ?name;
8+
↑ hovered here
9+
```
10+
11+
# Hover Results
12+
No hover information found.

crates/pgt_treesitter_grammar/grammar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,10 +3378,10 @@ module.exports = grammar({
33783378
choice(
33793379
$._identifier,
33803380
$._double_quote_string,
3381-
$._tsql_parameter,
3381+
$._sql_parameter,
33823382
seq("`", $._identifier, "`")
33833383
),
3384-
_tsql_parameter: ($) => seq("@", $._identifier),
3384+
_sql_parameter: (_) => /[:$@?][a-zA-Z_][0-9a-zA-Z_]*/,
33853385
_identifier: (_) => /[a-zA-Z_][0-9a-zA-Z_]*/,
33863386
},
33873387
});

0 commit comments

Comments
 (0)