Skip to content

Commit 08a000a

Browse files
authored
Fix empty comment handling (#826)
1 parent e9b202a commit 08a000a

File tree

3 files changed

+24
-59
lines changed

3 files changed

+24
-59
lines changed

librubyfmt/src/comment_block.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,30 @@ impl CommentBlock {
2525
}
2626

2727
pub fn into_line_tokens<'src>(self) -> impl Iterator<Item = ConcreteLineToken<'src>> {
28-
self.comments.into_iter().flat_map(|c| {
29-
[
30-
ConcreteLineToken::Comment { contents: c },
31-
ConcreteLineToken::HardNewLine,
32-
]
28+
let comments = self.comments;
29+
let len = comments.len();
30+
31+
comments.into_iter().enumerate().flat_map(move |(i, c)| {
32+
if c.is_empty() {
33+
// Empty strings represent blank lines
34+
// If this is a trailing empty comment (at the end), keep it as an empty Comment token
35+
// to bypass the HardNewLine deduplication logic. Otherwise convert to just HardNewLine.
36+
if i == len - 1 {
37+
// Trailing empty - keep as empty comment to preserve blank lines
38+
vec![
39+
ConcreteLineToken::Comment { contents: c },
40+
ConcreteLineToken::HardNewLine,
41+
]
42+
} else {
43+
// Between comments - convert to just HardNewLine
44+
vec![ConcreteLineToken::HardNewLine]
45+
}
46+
} else {
47+
vec![
48+
ConcreteLineToken::Comment { contents: c },
49+
ConcreteLineToken::HardNewLine,
50+
]
51+
}
3352
})
3453
}
3554

librubyfmt/src/intermediary.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,6 @@ impl<'src> Intermediary<'src> {
3434
self.tokens.len()
3535
}
3636

37-
// Pops off excessive whitespace for `require` calls followed
38-
// by comments. In the intermediary, this looks like
39-
// a `require` call followed by
40-
// - HardNewline
41-
// - HardNewline
42-
// - Comment { contents: "" }
43-
// - HardNewline
44-
// so this method actually pops off the extra empty comment whitespace
45-
pub fn pop_require_comment_whitespace(&mut self) {
46-
self.tokens.pop();
47-
self.tokens.pop();
48-
self.index_of_last_hard_newline = self.tokens.len() - 1;
49-
}
50-
5137
pub fn pop_heredoc_mistake(&mut self) {
5238
self.tokens.remove(self.tokens.len() - 1);
5339
self.tokens.remove(self.tokens.len() - 1);
@@ -136,39 +122,6 @@ impl<'src> Intermediary<'src> {
136122
self.current_line_metadata.set_has_require();
137123
}
138124
}
139-
ConcreteLineToken::Comment { .. } => {
140-
if matches!(
141-
self.last::<4>(),
142-
Some([
143-
_,
144-
_,
145-
ConcreteLineToken::HardNewLine,
146-
ConcreteLineToken::HardNewLine
147-
])
148-
) {
149-
let mut module_or_class_before_newline = false;
150-
let mut past_first_two_newlines = 0;
151-
for tok in self.tokens.iter().rev() {
152-
if tok == &ConcreteLineToken::HardNewLine {
153-
if past_first_two_newlines < 2 {
154-
past_first_two_newlines += 1;
155-
} else {
156-
break;
157-
}
158-
}
159-
if tok == &ConcreteLineToken::ModuleKeyword
160-
|| tok == &ConcreteLineToken::ClassKeyword
161-
{
162-
module_or_class_before_newline = true;
163-
}
164-
}
165-
166-
if module_or_class_before_newline {
167-
self.tokens.pop();
168-
self.index_of_last_hard_newline = self.tokens.len() - 1;
169-
}
170-
}
171-
}
172125
_ => {}
173126
}
174127

librubyfmt/src/render_queue_writer.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,6 @@ impl<'src> RenderQueueWriter<'src> {
158158
accum.insert_trailing_blankline(BlanklineReason::ComesAfterEnd);
159159
}
160160

161-
if let Some([HardNewLine, HardNewLine, Comment { contents }, HardNewLine]) =
162-
accum.last::<4>()
163-
&& contents.is_empty()
164-
{
165-
accum.pop_require_comment_whitespace();
166-
}
167-
168161
if let Some(
169162
[
170163
ConcreteLineToken::End,

0 commit comments

Comments
 (0)