diff --git a/crates/pgt_hover/src/hovered_node.rs b/crates/pgt_hover/src/hovered_node.rs index c67cb8d06..7f1b54c88 100644 --- a/crates/pgt_hover/src/hovered_node.rs +++ b/crates/pgt_hover/src/hovered_node.rs @@ -24,6 +24,10 @@ impl HoveredNode { pub(crate) fn get(ctx: &pgt_treesitter::context::TreesitterContext) -> Option { 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() { @@ -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("?") +} diff --git a/crates/pgt_hover/tests/hover_integration_tests.rs b/crates/pgt_hover/tests/hover_integration_tests.rs index 58ffdc3e3..e9c8f0c11 100644 --- a/crates/pgt_hover/tests/hover_integration_tests.rs +++ b/crates/pgt_hover/tests/hover_integration_tests.rs @@ -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; + } +} diff --git a/crates/pgt_hover/tests/snapshots/at-param.snap b/crates/pgt_hover/tests/snapshots/at-param.snap new file mode 100644 index 000000000..17f898d6d --- /dev/null +++ b/crates/pgt_hover/tests/snapshots/at-param.snap @@ -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. diff --git a/crates/pgt_hover/tests/snapshots/colon-param.snap b/crates/pgt_hover/tests/snapshots/colon-param.snap new file mode 100644 index 000000000..db9ef7fb1 --- /dev/null +++ b/crates/pgt_hover/tests/snapshots/colon-param.snap @@ -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. diff --git a/crates/pgt_hover/tests/snapshots/dollar-param.snap b/crates/pgt_hover/tests/snapshots/dollar-param.snap new file mode 100644 index 000000000..aab4f1c15 --- /dev/null +++ b/crates/pgt_hover/tests/snapshots/dollar-param.snap @@ -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. diff --git a/crates/pgt_hover/tests/snapshots/questionmark-param.snap b/crates/pgt_hover/tests/snapshots/questionmark-param.snap new file mode 100644 index 000000000..1f69294f6 --- /dev/null +++ b/crates/pgt_hover/tests/snapshots/questionmark-param.snap @@ -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. diff --git a/crates/pgt_treesitter_grammar/grammar.js b/crates/pgt_treesitter_grammar/grammar.js index 670c33442..6fc29ac95 100644 --- a/crates/pgt_treesitter_grammar/grammar.js +++ b/crates/pgt_treesitter_grammar/grammar.js @@ -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_]*/, }, });