[Issue #6631] fix normalize comments eats single line comments in code blocks#6924
Conversation
| line = trim_end_unless_two_whitespaces(line.trim_start(), is_doc_comment); | ||
| // Drop old closer. | ||
| if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") { | ||
| line = line[..(line.len() - 2)].trim_end(); | ||
| } | ||
|
|
||
| line | ||
| let code_block_matches = line.matches("```").count(); | ||
| if code_block_matches != 0 && code_block_matches % 2 == 1 { | ||
| is_in_code_block = !is_in_code_block; | ||
| left_trim_comment_line(line, &style) | ||
| } else if is_in_code_block { | ||
| left_trim_comment_code_line(line, &style) | ||
| } else { | ||
| left_trim_comment_line(line, &style) | ||
| } |
There was a problem hiding this comment.
nit: This feels subjectively a bit above the sort of complexity I'd want to generally see inside a map block. I'd recommend finding a way to simplify this - likely a small helper method that has an intent revealing name and a short comment.
Not a big deal, mostly it's the shape of this code makes it that to understand what it does and what it should do you have to make inferences about the behavior after reading all of it, rather than starting with a mental model and knowing what the implementing code should do.
There was a problem hiding this comment.
I created a CodeBlockTracker struct in my new commit. This makes the code block state explicit and easier to understand. I added a bunch of comments defining the expected behavior and a few tests for the new struct.
Fix for Issue 6631
Changes
Check if we are in a code block. If we are within one, use the
left_trim_comment_code_linefunction instead ofleft_trim_comment_line.