Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions crates/pgt_plpgsql_check/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,14 @@ fn resolve_span(issue: &PlpgSqlCheckIssue, fn_body: &str, offset: usize) -> Opti
let stmt = match issue.statement.as_ref() {
Some(s) => s,
None => {
let leading_whitespace = fn_body.len() - fn_body.trim_ascii_start().len();
let trailing_whitespace = fn_body.len() - fn_body.trim_ascii_end().len();

return Some(TextRange::new(
(offset as u32).into(),
((offset + fn_body.len()) as u32).into(),
(offset + leading_whitespace).try_into().unwrap(),
(offset + fn_body.len() - trailing_whitespace)
.try_into()
.unwrap(),
));
}
};
Expand Down
40 changes: 40 additions & 0 deletions crates/pgt_plpgsql_check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,46 @@ mod tests {
assert_eq!(span_texts[0].as_deref(), Some("c"));
}

#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
async fn test_entire_body_broken(test_db: PgPool) {
let setup = r#"
create extension if not exists plpgsql_check;

CREATE TABLE t1(a int, b int);
"#;

let create_fn_sql = r#"
CREATE OR REPLACE FUNCTION public.f1()
RETURNS void
LANGUAGE plpgsql
AS
$function$ DECLRE r record; -- spelled declare wrong!
BEGIN
select * from t1;
END; $function$;
"#;

let (diagnostics, span_texts) = run_plpgsql_check_test(&test_db, setup, create_fn_sql)
.await
.expect("Failed to run plpgsql_check test");

assert_eq!(diagnostics.len(), 1);
assert!(matches!(
diagnostics[0].severity,
pgt_diagnostics::Severity::Error
));
assert_eq!(
span_texts[0].as_deref(),
// the span starts at the keyword and omits the whitespace before/after $function$
Some(
r#"DECLRE r record; -- spelled declare wrong!
BEGIN
select * from t1;
END;"#
)
);
}

#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
async fn test_plpgsql_check(test_db: PgPool) {
let setup = r#"
Expand Down
2 changes: 1 addition & 1 deletion crates/pgt_workspace/src/workspace/server.tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ async fn test_dedupe_diagnostics(test_db: PgPool) {

assert_eq!(
diagnostic.location().span,
Some(TextRange::new(115.into(), 210.into()))
Some(TextRange::new(124.into(), 201.into()))
);
}

Expand Down