Skip to content

Commit

Permalink
Fix formatting of binding patterns
Browse files Browse the repository at this point in the history
This ensures that `inko fmt` doesn't remove the explicit type annotation
and `mut` modifier when formatting binding patterns.

This fixes #762.

Changelog: fixed
  • Loading branch information
yorickpeterse committed Oct 9, 2024
1 parent 5c0239e commit f94e4d1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
18 changes: 17 additions & 1 deletion compiler/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,7 @@ impl Document {
nodes::Pattern::Or(_)
| nodes::Pattern::Class(_)
| nodes::Pattern::Tuple(_)
| nodes::Pattern::Identifier(_)
) || node.guard.is_some()
{
Node::HardLine
Expand Down Expand Up @@ -2138,7 +2139,22 @@ impl Document {
nodes::Pattern::Int(n) => vec![Node::text(&n.value)],
nodes::Pattern::True(_) => vec![Node::text("true")],
nodes::Pattern::False(_) => vec![Node::text("false")],
nodes::Pattern::Identifier(n) => vec![Node::text(&n.name.name)],
nodes::Pattern::Identifier(n) => {
let mut nodes = Vec::new();

if n.mutable {
nodes.push(Node::text("mut "));
}

nodes.push(Node::text(&n.name.name));

if let Some(typ) = &n.value_type {
nodes.push(Node::text(": "));
nodes.push(self.type_reference(typ));
}

vec![self.group(nodes)]
}
nodes::Pattern::Tuple(n) => {
let gid = self.new_group_id();
let vals = self.list(&n.values, gid, |s, n| s.pattern(n));
Expand Down
29 changes: 29 additions & 0 deletions std/fixtures/fmt/binding_pattern/input.inko
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
fn example1 {
match (10, 20) {
case (a: Int, b: Int) -> {}
}
}

fn example2 {
match 42 {
case mut a -> {}
}
}

fn example3 {
match 42 {
case mut loooooooooooooooooooooooooong_naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaameeeeeeeeee -> {}
}
}

fn example4 {
match 42 {
case mut loooooooooooooooooooooooooong_naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaameeeeeeeeee: TypeNameHere -> {}
}
}

fn example5 {
match 42 {
case mut loooooooooooooooooooooooooong_naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaameeeeeeeeee: TypeNameHere if true -> {}
}
}
36 changes: 36 additions & 0 deletions std/fixtures/fmt/binding_pattern/output.inko
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
fn example1 {
match (10, 20) {
case (a: Int, b: Int) -> {}
}
}

fn example2 {
match 42 {
case mut a -> {}
}
}

fn example3 {
match 42 {
case
mut loooooooooooooooooooooooooong_naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaameeeeeeeeee
-> {}
}
}

fn example4 {
match 42 {
case
mut loooooooooooooooooooooooooong_naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaameeeeeeeeee: TypeNameHere
-> {}
}
}

fn example5 {
match 42 {
case
mut loooooooooooooooooooooooooong_naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaameeeeeeeeee: TypeNameHere
if true
-> {}
}
}

0 comments on commit f94e4d1

Please sign in to comment.