Skip to content

Commit 09800a6

Browse files
committed
fix a bug in line flipping
1 parent 1edc205 commit 09800a6

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/parse.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,13 +1537,29 @@ fn flip_unsplit_lines_impl(lines: Vec<Vec<Sp<Word>>>, in_array: bool) -> Vec<Vec
15371537
.last()
15381538
.is_some_and(|w| matches!(w.value, Word::FlipLine));
15391539

1540+
let trim_spaces = |words: &mut Vec<Sp<Word>>| {
1541+
while (words.first()).is_some_and(|w| matches!(w.value, Word::Spaces)) {
1542+
words.remove(0);
1543+
}
1544+
while words
1545+
.last()
1546+
.is_some_and(|w| matches!(w.value, Word::Spaces))
1547+
{
1548+
words.pop();
1549+
}
1550+
};
1551+
15401552
let flip_line = |mut line: Vec<Sp<Word>>| {
15411553
if line.iter().any(|w| matches!(w.value, Word::FlipLine)) {
15421554
let mut parts = Vec::new();
15431555
while let Some(i) = (line.iter()).rposition(|w| matches!(w.value, Word::FlipLine)) {
1544-
parts.push(line.split_off(i + 1));
1545-
line.pop();
1556+
let mut part = line.split_off(i + 1);
1557+
trim_spaces(&mut part);
1558+
parts.push(part);
1559+
let span = line.pop().unwrap().span;
1560+
parts.push(vec![span.sp(Word::Spaces)]);
15461561
}
1562+
trim_spaces(&mut line);
15471563
parts.push(line);
15481564
line = parts.into_iter().flatten().collect();
15491565
}
@@ -1553,13 +1569,7 @@ fn flip_unsplit_lines_impl(lines: Vec<Vec<Sp<Word>>>, in_array: bool) -> Vec<Vec
15531569
let mut new_lines = vec![flip_line(first)];
15541570

15551571
for mut line in lines {
1556-
// Trim spaces
1557-
while (line.first()).is_some_and(|w| matches!(w.value, Word::Spaces)) {
1558-
line.remove(0);
1559-
}
1560-
while line.last().is_some_and(|w| matches!(w.value, Word::Spaces)) {
1561-
line.pop();
1562-
}
1572+
trim_spaces(&mut line);
15631573
// Check for leading and trailing unbreak lines
15641574
let unsplit_front = (line.first()).is_some_and(|w| matches!(w.value, Word::FlipLine));
15651575
if unsplit_front {

0 commit comments

Comments
 (0)