-
-
Notifications
You must be signed in to change notification settings - Fork 989
fix(formatter/md): fix formatting for MdHardLine #9480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,31 @@ use biome_markdown_syntax::MdHardLine; | |
| pub(crate) struct FormatMdHardLine; | ||
| impl FormatNodeRule<MdHardLine> for FormatMdHardLine { | ||
| fn fmt_fields(&self, node: &MdHardLine, f: &mut MarkdownFormatter) -> FormatResult<()> { | ||
| write!(f, [format_removed(&node.value_token()?), hard_line_break()]) | ||
| let token = node.value_token()?; | ||
| let text_content = token.text(); | ||
|
|
||
| if text_content.trim_end().ends_with('\\') { | ||
| // Preserve backslash form | ||
| write!( | ||
| f, | ||
| [ | ||
| format_removed(&token), | ||
| text("\\", token.text_range().start()), | ||
| hard_line_break() | ||
| ] | ||
| ) | ||
| } else { | ||
| // Given two or more spaces in MdHardLine, only two spaces has semantic meaning | ||
| // so we are adding back two spaces as required by the spec | ||
| // https://spec.commonmark.org/0.31.2/#hard-line-break | ||
| write!( | ||
| f, | ||
| [ | ||
| format_removed(&token), | ||
| text(" ", token.text_range().start()), | ||
| hard_line_break(), | ||
| ] | ||
| ) | ||
| } | ||
|
Comment on lines
+11
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep hard-line emission in the caller, not in Line 18 and Line 28 emit Suggested patch if text_content.trim_end().ends_with('\\') {
// Preserve backslash form
write!(
f,
[
format_removed(&token),
- text("\\", token.text_range().start()),
- hard_line_break()
+ text("\\", token.text_range().start()),
]
)
} else {
// Normalize spaces to 2 spaces
write!(
f,
[
format_removed(&token),
- text(" ", token.text_range().start()),
- hard_line_break(),
+ text(" ", token.text_range().start()),
]
)
}Based on learnings: In 🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai I believe @ematipico moved it inside in #9331 (comment) though. Check the test snapshots and you can see there are no redundant spaces.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainThe web search didn't find the specific discussion. Let me look at the test snapshots directly in the repository to verify the current behaviour. 🏁 Script executed: Length of output: 1970 🏁 Script executed: Length of output: 2477 🏁 Script executed: Length of output: 2843 Let me also check the caller of 🏁 Script executed: Length of output: 368 🏁 Script executed: Length of output: 376 🏁 Script executed: Length of output: 5870
My original review comment was based on a stale learning from the pre-#9331 design. I'll update it now. ✏️ Learnings added
⛔ Learnings removed🧠 Learnings used |
||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,14 +18,14 @@ bbb | |
| --- Prettier | ||
| +++ Biome | ||
| @@ -1,2 +1,2 @@ | ||
| -aaa | ||
| +aaa | ||
| bbb | ||
| aaa | ||
| -bbb | ||
| +bbb | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ematipico This is an edge case I couldn't figure out how to fix. We want to trim the double spaces if the next line is empty. But we want to preserve double spaces if next line isn't empty.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai create an issue to track this edge case. Create the issue as a sub issue of #3718
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: Length of output: 44 Now let me link it as a sub-issue of 🏁 Script executed: Length of output: 199
🧠 Learnings used |
||
| ``` | ||
|
|
||
| # Output | ||
|
|
||
| ```md | ||
| aaa | ||
| bbb | ||
| aaa | ||
| bbb | ||
| ``` | ||
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,14 @@ foo | |
| --- Prettier | ||
| +++ Biome | ||
| @@ -1,2 +1,2 @@ | ||
| -foo | ||
| foo | ||
| - bar | ||
| +foo | ||
| +bar | ||
| ``` | ||
|
|
||
| # Output | ||
|
|
||
| ```md | ||
| foo | ||
| foo | ||
| bar | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,14 @@ foo\ | |
| --- Prettier | ||
| +++ Biome | ||
| @@ -1,2 +1,2 @@ | ||
| -foo\ | ||
| foo\ | ||
| - bar | ||
| +foo | ||
| +bar | ||
| ``` | ||
|
|
||
| # Output | ||
|
|
||
| ```md | ||
| foo | ||
| foo\ | ||
| bar | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the correct move? Genuinely asking. Isn't this part subject to the indentation option? Also whitespaces should be emitted using
space(), unless it's a specific case.If so please comment the why, not the what
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is already in a few lines above. In CommonMark, two or more spaces mean a hard line break. https://spec.commonmark.org/0.31.2/#hard-line-break
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prettier trims "two or more spaces" to just "two spaces" so this is to match that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says the what, not the why. It simply says "here we normalised", but it doesn't explain why
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. On my phone, let me try something ...
@coderabbitai add a code comment based on this thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tidefield, here's a comment rewrite that captures the why for both branches:The key additions:
text(...)instead ofspace()(it's a spec-mandated literal sequence, not indentation).🧠 Learnings used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ematipico I added the comment about why. PTAL.