Skip to content

Commit

Permalink
Fix underflows when formatting certain call chains
Browse files Browse the repository at this point in the history
For certain call chains the formatter wouldn't reset the indentation
correctly, resulting in an underflow for the indent count.

Changelog: fixed
  • Loading branch information
yorickpeterse committed Jul 18, 2024
1 parent 63086b2 commit b3cc7cf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
10 changes: 9 additions & 1 deletion compiler/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2584,11 +2584,19 @@ impl Generator {
Node::IndentNext(nodes) if wrap.is_enabled() => {
self.pending_indents += 1;

let before = self.pending_indents;

for n in nodes {
self.node(n, wrap);
}

self.indent -= 2;
// If we didn't encounter a new line, reset the state. If we do,
// we have to dedent the lines that follow.
if self.pending_indents == before {
self.pending_indents -= 1;
} else {
self.indent -= 2;
}
}
Node::IndentNext(nodes) => {
for n in nodes {
Expand Down
13 changes: 13 additions & 0 deletions std/fixtures/fmt/indented_call_chains/input.inko
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn foo {
aaa.aaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.cccc('aaaaaaaa').cccc('aaa').cccc(aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)
100
}

fn bar {
aaa.aaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.cccc('aaaaaaaa').cccc('aaa').cccc(aaaa)
100
}

fn baz {
# line
}
26 changes: 26 additions & 0 deletions std/fixtures/fmt/indented_call_chains/output.inko
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn foo {
aaa
.aaaaaaaaaaaaaaaaa
.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
.cccc('aaaaaaaa')
.cccc('aaa')
.cccc(
aaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
)
100
}

fn bar {
aaa
.aaaaaaaaaaaaaaaaa
.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
.cccc('aaaaaaaa')
.cccc('aaa')
.cccc(aaaa)
100
}

fn baz {
# line
}

0 comments on commit b3cc7cf

Please sign in to comment.