Skip to content

Commit 7e592f7

Browse files
committed
Format FOR .. IN range LOOP
1 parent 00b695b commit 7e592f7

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/syntax/procedural_language.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const proceduralLanguageMap: Partial<CstToDocMap<AllProceduralNodes>> = {
141141
hardline,
142142
print.spaced("endWhileKw"),
143143
]),
144+
// WHILE .. LOOP .. END LOOP
144145
while_loop_stmt: (print) =>
145146
group(print.spaced(["whileKw", "condition", "loop"])),
146147
// FOR .. IN
@@ -151,6 +152,16 @@ export const proceduralLanguageMap: Partial<CstToDocMap<AllProceduralNodes>> = {
151152
hardline,
152153
print.spaced("endForKw"),
153154
]),
155+
// FOR .. IN range LOOP .. END LOOP
156+
for_loop_stmt: (print) =>
157+
group(print.spaced(["forKw", "left", "inKw", "right", "loop"])),
158+
for_range: (print) =>
159+
group([
160+
print.spaced(["reverseKw", "from"]),
161+
"..",
162+
print.spaced(["to", "by"]),
163+
]),
164+
for_by_clause: (print) => group(print.spaced(["byKw", "expr"])),
154165
// BREAK/CONTINUE
155166
break_stmt: (print) => group(print.spaced(["breakKw", "label", "when"])),
156167
continue_stmt: (print) =>

test/proc/loops.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,28 @@ describe("loops", () => {
4141
END FOR
4242
`);
4343
});
44+
45+
it(`formats FOR .. IN range LOOP`, async () => {
46+
await testPlpgsql(dedent`
47+
FOR n IN 1..10 LOOP
48+
x = x + n;
49+
END LOOP
50+
`);
51+
});
52+
53+
it(`formats FOR .. IN REVERSE range LOOP`, async () => {
54+
await testPlpgsql(dedent`
55+
FOR n IN REVERSE 10..1 LOOP
56+
x = x + n;
57+
END LOOP
58+
`);
59+
});
60+
61+
it(`formats FOR .. IN range BY step LOOP`, async () => {
62+
await testPlpgsql(dedent`
63+
FOR n IN x..y BY z LOOP
64+
x = x + n;
65+
END LOOP
66+
`);
67+
});
4468
});

0 commit comments

Comments
 (0)