Skip to content

Commit dd6a23f

Browse files
fix(plpgsql_check): whitespace issue (#501)
1 parent 8ee97ba commit dd6a23f

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

crates/pgt_plpgsql_check/src/diagnostics.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,14 @@ fn resolve_span(issue: &PlpgSqlCheckIssue, fn_body: &str, offset: usize) -> Opti
140140
let stmt = match issue.statement.as_ref() {
141141
Some(s) => s,
142142
None => {
143+
let leading_whitespace = fn_body.len() - fn_body.trim_ascii_start().len();
144+
let trailing_whitespace = fn_body.len() - fn_body.trim_ascii_end().len();
145+
143146
return Some(TextRange::new(
144-
(offset as u32).into(),
145-
((offset + fn_body.len()) as u32).into(),
147+
(offset + leading_whitespace).try_into().unwrap(),
148+
(offset + fn_body.len() - trailing_whitespace)
149+
.try_into()
150+
.unwrap(),
146151
));
147152
}
148153
};

crates/pgt_plpgsql_check/src/lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,46 @@ mod tests {
405405
assert_eq!(span_texts[0].as_deref(), Some("c"));
406406
}
407407

408+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
409+
async fn test_entire_body_broken(test_db: PgPool) {
410+
let setup = r#"
411+
create extension if not exists plpgsql_check;
412+
413+
CREATE TABLE t1(a int, b int);
414+
"#;
415+
416+
let create_fn_sql = r#"
417+
CREATE OR REPLACE FUNCTION public.f1()
418+
RETURNS void
419+
LANGUAGE plpgsql
420+
AS
421+
$function$ DECLRE r record; -- spelled declare wrong!
422+
BEGIN
423+
select * from t1;
424+
END; $function$;
425+
"#;
426+
427+
let (diagnostics, span_texts) = run_plpgsql_check_test(&test_db, setup, create_fn_sql)
428+
.await
429+
.expect("Failed to run plpgsql_check test");
430+
431+
assert_eq!(diagnostics.len(), 1);
432+
assert!(matches!(
433+
diagnostics[0].severity,
434+
pgt_diagnostics::Severity::Error
435+
));
436+
assert_eq!(
437+
span_texts[0].as_deref(),
438+
// the span starts at the keyword and omits the whitespace before/after $function$
439+
Some(
440+
r#"DECLRE r record; -- spelled declare wrong!
441+
BEGIN
442+
select * from t1;
443+
END;"#
444+
)
445+
);
446+
}
447+
408448
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
409449
async fn test_plpgsql_check(test_db: PgPool) {
410450
let setup = r#"

crates/pgt_workspace/src/workspace/server.tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ async fn test_dedupe_diagnostics(test_db: PgPool) {
278278

279279
assert_eq!(
280280
diagnostic.location().span,
281-
Some(TextRange::new(115.into(), 210.into()))
281+
Some(TextRange::new(124.into(), 201.into()))
282282
);
283283
}
284284

0 commit comments

Comments
 (0)