Skip to content

Commit

Permalink
fix a formatting bug with comments and multiline strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jan 9, 2024
1 parent c439ed9 commit 39fa464
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,13 +844,19 @@ impl<'a> Formatter<'a> {
if lines.is_empty() {
return;
}
let last_word_comment = lines
let prevent_compact = lines
.iter()
.flatten()
.filter(|word| !matches!(word.value, Word::Spaces))
.last()
.unwrap()
.last()
.is_some_and(|word| matches!(word.value, Word::Comment(_)));
.is_some_and(|word| {
matches!(
word.value,
Word::Comment(_) | Word::OutputComment { .. } | Word::MultilineString(_)
)
});
if lines.len() == 1
&& !last_word_comment
&& !prevent_compact
&& !lines[0].iter().any(|word| word_is_multiline(&word.value))
{
self.format_words(&lines[0], true, depth);
Expand All @@ -863,7 +869,7 @@ impl<'a> Formatter<'a> {
curr_line.chars().count()
};
let compact = allow_compact
&& !last_word_comment
&& !prevent_compact
&& match self.config.compact_multiline_mode {
CompactMultilineMode::Always => true,
CompactMultilineMode::Never => false,
Expand All @@ -890,6 +896,9 @@ impl<'a> Formatter<'a> {
self.format_words(line, true, depth);
}
if !compact {
if prevent_compact {
self.output.push('\n');
}
for _ in 0..self.config.multiline_indent * depth.saturating_sub(1) {
self.output.push(' ');
}
Expand Down

0 comments on commit 39fa464

Please sign in to comment.