Skip to content

Commit a948990

Browse files
dimfeldshssoichiro
andauthored
Format "begin" and "declare" for plpgsql (#30)
* treat $$ as a reserved token that sits on its own line * format BEGIN and DECLARE for plpgsql --------- Co-authored-by: Josh Holmer <[email protected]>
1 parent 0fd0485 commit a948990

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/lib.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1435,4 +1435,27 @@ mod tests {
14351435

14361436
assert_eq!(format(input, &QueryParams::None, options), expected);
14371437
}
1438+
1439+
#[test]
1440+
fn it_formats_pgplsql() {
1441+
let input = "CREATE FUNCTION abc() AS $$ DECLARE a int := 1; b int := 2; BEGIN SELECT * FROM table $$ LANGUAGE plpgsql;";
1442+
let options = FormatOptions::default();
1443+
let expected = indoc!(
1444+
"
1445+
CREATE FUNCTION abc() AS
1446+
$$
1447+
DECLARE
1448+
a int := 1;
1449+
b int := 2;
1450+
BEGIN
1451+
SELECT
1452+
*
1453+
FROM
1454+
table
1455+
$$
1456+
LANGUAGE plpgsql;"
1457+
);
1458+
1459+
assert_eq!(format(input, &QueryParams::None, options), expected);
1460+
}
14381461
}

src/tokenizer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ fn get_newline_reserved_token<'a>(
528528
fn get_top_level_reserved_token_no_indent(input: &str) -> IResult<&str, Token<'_>> {
529529
let uc_input = get_uc_words(input, 2);
530530
let result: IResult<&str, &str> = alt((
531+
terminated(tag("BEGIN"), end_of_word),
532+
terminated(tag("DECLARE"), end_of_word),
531533
terminated(tag("INTERSECT"), end_of_word),
532534
terminated(tag("INTERSECT ALL"), end_of_word),
533535
terminated(tag("MINUS"), end_of_word),
@@ -569,7 +571,6 @@ fn get_plain_reserved_token(input: &str) -> IResult<&str, Token<'_>> {
569571
terminated(tag("AUTOCOMMIT"), end_of_word),
570572
terminated(tag("AUTO_INCREMENT"), end_of_word),
571573
terminated(tag("BACKUP"), end_of_word),
572-
terminated(tag("BEGIN"), end_of_word),
573574
terminated(tag("BETWEEN"), end_of_word),
574575
terminated(tag("BINLOG"), end_of_word),
575576
terminated(tag("BOTH"), end_of_word),

0 commit comments

Comments
 (0)