Skip to content

Commit

Permalink
fix-3140
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri authored and lpil committed May 17, 2024
1 parent 3f1ca30 commit c2cf622
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
18 changes: 14 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@
Hint: Rename this module and try again.
```

- New subcommand `gleam hex revert` added. ([Pi-Cla](https://github.com/Pi-Cla))
* You can specify the options like this: `gleam hex revert --package gling --version 1.2.3`
* A new package can be reverted or updated within 24 hours of it's initial publish,
a new version of an existing package can be reverted or updated within one hour.
* You could already update packages even before this release by running: `gleam publish` again.

- You can specify the options like this: `gleam hex revert --package gling --version 1.2.3`
- A new package can be reverted or updated within 24 hours of it's initial publish,
a new version of an existing package can be reverted or updated within one hour.
- You could already update packages even before this release by running: `gleam publish` again.

- When the user tries to replace a release without the `--replace` flag
the error message now mentions the lack of a `--replace` flag.
Expand Down Expand Up @@ -234,6 +236,8 @@
Matching on a literal value is redundant since you can already tell which
branch is going to match with this value.
```

- The compiler will now continue module analysis when there are errors in top
level definitions. This means that when these errors occur the compiler will
continue analysing the rest of the code to find other errors and type
Expand Down Expand Up @@ -413,7 +417,9 @@
#(_, _) -> 1
}
```

Is rewritten to:

```
case x, y {
1, 2 -> 0
Expand Down Expand Up @@ -479,3 +485,7 @@

- Fixed a bug where local path dependencies could be mishandled on Windows.
([Francisco Montanez](https://github.com/Francisco-Montanez))

- Fixed a bug where adding a comment to a case clause would cause it to break
on multiple lines.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
24 changes: 14 additions & 10 deletions compiler-core/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ impl<'comments> Formatter<'comments> {
clauses
.iter()
.enumerate()
.map(|(i, c)| self.clause(c, i as u32)),
.map(|(i, c)| self.clause(c, i as u32).group()),
);

// We get all remaining comments that come before the case's closing
Expand Down Expand Up @@ -1759,11 +1759,6 @@ impl<'comments> Formatter<'comments> {
.append(self.clause_guard(guard).group().nest(INDENT)),
};

let clause_doc = match printed_comments(comments, false) {
Some(comments) => comments.append(line()).append(clause_doc),
None => clause_doc,
};

// In case there's a guard or multiple subjects, if we decide to break
// the patterns on multiple lines we also want the arrow to end up on
// its own line to improve legibility.
Expand All @@ -1778,6 +1773,7 @@ impl<'comments> Formatter<'comments> {
// // for patterns with `if` guards.
// }
// ```

let has_guard = clause.guard.is_some();
let has_multiple_subjects = clause.pattern.len() > 1;
let arrow_break = if has_guard || has_multiple_subjects {
Expand All @@ -1786,17 +1782,25 @@ impl<'comments> Formatter<'comments> {
" ".to_doc()
};

let clause_doc = clause_doc
.append(arrow_break)
.group()
.append("->")
.append(self.case_clause_value(&clause.then).group())
.group();

let clause_doc = match printed_comments(comments, false) {
Some(comments) => comments.append(line()).append(clause_doc),
None => clause_doc,
};

if index == 0 {
clause_doc
} else if space_before {
lines(2).append(clause_doc)
} else {
lines(1).append(clause_doc)
}
.append(arrow_break)
.group()
.append("->")
.append(self.case_clause_value(&clause.then))
}

fn alternative_patterns<'a>(&mut self, clause: &'a UntypedClause) -> Document<'a> {
Expand Down
14 changes: 14 additions & 0 deletions compiler-core/src/format/tests/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,17 @@ fn case_pattern_split_on_multiple_lines_is_not_needlessly_nested() {
"#
);
}

// https://github.com/gleam-lang/gleam/issues/3140
#[test]
fn long_comment_before_case_with_multiple_subjects_doesnt_force_a_break() {
assert_format!(
r#"fn main() {
case a, b {
// a very long comment a very long comment a very long comment a very long comment
_, _ -> True
}
}
"#
);
}

0 comments on commit c2cf622

Please sign in to comment.