Skip to content
Merged
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 @@ -24,6 +24,10 @@ impl HoveredNode {
pub(crate) fn get(ctx: &pgt_treesitter::context::TreesitterContext) -> Option<Self> {
let node_content = ctx.get_node_under_cursor_content()?;

if looks_like_sql_param(node_content.as_str()) {
return None;
}

let under_cursor = ctx.node_under_cursor.as_ref()?;

match under_cursor.kind() {
Expand Down Expand Up @@ -114,3 +118,10 @@ impl HoveredNode {
}
}
}

fn looks_like_sql_param(content: &str) -> bool {
(content.starts_with("$") && !content.starts_with("$$"))
|| (content.starts_with(":") && !content.starts_with("::"))
|| (content.starts_with("@"))
|| content.starts_with("?")
}
41 changes: 41 additions & 0 deletions crates/pgt_hover/tests/hover_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,44 @@ async fn test_grant_table_hover(test_db: PgPool) {

test_hover_at_cursor("grant_select", query, None, &test_db).await;
}

#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
async fn no_hover_results_over_params(test_db: PgPool) {
let setup = r#"
create table users (
id serial primary key,
name text
);
"#;

test_db.execute(setup).await.unwrap();

{
let query = format!(
"select * from users where name = $n{}ame;",
QueryWithCursorPosition::cursor_marker()
);
test_hover_at_cursor("dollar-param", query, None, &test_db).await;
}
{
let query = format!(
"select * from users where name = :n{}ame;",
QueryWithCursorPosition::cursor_marker()
);
test_hover_at_cursor("colon-param", query, None, &test_db).await;
}
{
let query = format!(
"select * from users where name = @n{}ame;",
QueryWithCursorPosition::cursor_marker()
);
test_hover_at_cursor("at-param", query, None, &test_db).await;
}
{
let query = format!(
"select * from users where name = ?n{}ame;",
QueryWithCursorPosition::cursor_marker()
);
test_hover_at_cursor("questionmark-param", query, None, &test_db).await;
}
}
12 changes: 12 additions & 0 deletions crates/pgt_hover/tests/snapshots/at-param.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/pgt_hover/tests/hover_integration_tests.rs
expression: snapshot
---
# Input
```sql
select * from users where name = @name;
↑ hovered here
```

# Hover Results
No hover information found.
12 changes: 12 additions & 0 deletions crates/pgt_hover/tests/snapshots/colon-param.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/pgt_hover/tests/hover_integration_tests.rs
expression: snapshot
---
# Input
```sql
select * from users where name = :name;
↑ hovered here
```

# Hover Results
No hover information found.
12 changes: 12 additions & 0 deletions crates/pgt_hover/tests/snapshots/dollar-param.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/pgt_hover/tests/hover_integration_tests.rs
expression: snapshot
---
# Input
```sql
select * from users where name = $name;
↑ hovered here
```

# Hover Results
No hover information found.
12 changes: 12 additions & 0 deletions crates/pgt_hover/tests/snapshots/questionmark-param.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/pgt_hover/tests/hover_integration_tests.rs
expression: snapshot
---
# Input
```sql
select * from users where name = ?name;
↑ hovered here
```

# Hover Results
No hover information found.
4 changes: 2 additions & 2 deletions crates/pgt_treesitter_grammar/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3378,10 +3378,10 @@ module.exports = grammar({
choice(
$._identifier,
$._double_quote_string,
$._tsql_parameter,
$._sql_parameter,
seq("`", $._identifier, "`")
),
_tsql_parameter: ($) => seq("@", $._identifier),
_sql_parameter: (_) => /[:$@?][a-zA-Z_][0-9a-zA-Z_]*/,
_identifier: (_) => /[a-zA-Z_][0-9a-zA-Z_]*/,
},
});
Expand Down