-
-
Notifications
You must be signed in to change notification settings - Fork 963
feat(md/fmt): better formatting for some cases #9917
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 34 additions & 3 deletions
37
crates/biome_markdown_formatter/src/markdown/auxiliary/newline.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,40 @@ | ||
| use crate::prelude::*; | ||
| use biome_markdown_syntax::MdNewline; | ||
| use biome_formatter::{FormatRuleWithOptions, write}; | ||
| use biome_markdown_syntax::{MdHeader, MdNewline}; | ||
| use biome_rowan::AstNode; | ||
| #[derive(Debug, Clone, Default)] | ||
| pub(crate) struct FormatMdNewline; | ||
| pub(crate) struct FormatMdNewline { | ||
| should_remove: bool, | ||
| } | ||
| impl FormatNodeRule<MdNewline> for FormatMdNewline { | ||
| fn fmt_fields(&self, node: &MdNewline, f: &mut MarkdownFormatter) -> FormatResult<()> { | ||
| node.value_token().format().fmt(f) | ||
| if self.should_remove { | ||
| return write!(f, [format_removed(&node.value_token()?)]); | ||
| } | ||
|
|
||
| let after_header = node | ||
| .syntax() | ||
| .prev_sibling() | ||
| .is_some_and(|s| MdHeader::can_cast(s.kind())); | ||
|
|
||
| if after_header { | ||
| let token = node.value_token()?; | ||
| write!(f, [format_removed(&token), hard_line_break()]) | ||
| } else { | ||
| node.value_token().format().fmt(f) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct FormatMdNewlineOptions { | ||
| pub(crate) should_remove: bool, | ||
| } | ||
|
|
||
| impl FormatRuleWithOptions<MdNewline> for FormatMdNewline { | ||
| type Options = FormatMdNewlineOptions; | ||
|
|
||
| fn with_options(mut self, options: Self::Options) -> Self { | ||
| self.should_remove = options.should_remove; | ||
| self | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 25 additions & 3 deletions
28
crates/biome_markdown_formatter/src/markdown/auxiliary/reference_image.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,32 @@ | ||
| use crate::prelude::*; | ||
| use biome_markdown_syntax::MdReferenceImage; | ||
| use biome_rowan::AstNode; | ||
| use biome_formatter::write; | ||
| use biome_markdown_syntax::{MdReferenceImage, MdReferenceImageFields}; | ||
| #[derive(Debug, Clone, Default)] | ||
| pub(crate) struct FormatMdReferenceImage; | ||
| impl FormatNodeRule<MdReferenceImage> for FormatMdReferenceImage { | ||
| fn fmt_fields(&self, node: &MdReferenceImage, f: &mut MarkdownFormatter) -> FormatResult<()> { | ||
| format_verbatim_node(node.syntax()).fmt(f) | ||
| let MdReferenceImageFields { | ||
| excl_token, | ||
| l_brack_token, | ||
| alt, | ||
| r_brack_token, | ||
| label, | ||
| } = node.as_fields(); | ||
|
|
||
| write!( | ||
| f, | ||
| [ | ||
| excl_token.format(), | ||
| l_brack_token.format(), | ||
| alt.format(), | ||
| r_brack_token.format() | ||
| ] | ||
| )?; | ||
|
|
||
| if let Some(label) = label { | ||
| write!(f, [label.format()])?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 56 additions & 3 deletions
59
crates/biome_markdown_formatter/src/markdown/lists/block_list.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,63 @@ | ||
| use crate::markdown::auxiliary::newline::FormatMdNewlineOptions; | ||
| use crate::prelude::*; | ||
| use biome_markdown_syntax::MdBlockList; | ||
| use biome_formatter::FormatRuleWithOptions; | ||
| use biome_markdown_syntax::{AnyMdBlock, AnyMdLeafBlock, MdBlockList}; | ||
|
|
||
| #[derive(Debug, Clone, Default)] | ||
| pub(crate) struct FormatMdBlockList; | ||
| pub(crate) struct FormatMdBlockList { | ||
| /// When true, it removes all leading newlines and trailing newlines | ||
| trim: bool, | ||
| } | ||
| impl FormatRule<MdBlockList> for FormatMdBlockList { | ||
| type Context = MarkdownFormatContext; | ||
| fn fmt(&self, node: &MdBlockList, f: &mut MarkdownFormatter) -> FormatResult<()> { | ||
| f.join().entries(node.iter().formatted()).finish() | ||
| let mut joiner = f.join(); | ||
|
|
||
| if !self.trim { | ||
| return f.join().entries(node.iter().formatted()).finish(); | ||
| } | ||
|
|
||
| let mut iter = node.iter(); | ||
|
|
||
| // Count trailing newlines using next_back | ||
| let mut trailing_count = 0; | ||
| while let Some(AnyMdBlock::AnyMdLeafBlock(AnyMdLeafBlock::MdNewline(_))) = iter.next_back() | ||
| { | ||
| trailing_count += 1; | ||
| } | ||
|
|
||
| // we don't need the iter anymore | ||
| drop(iter); | ||
|
|
||
| // Single forward pass in document order | ||
| let mut still_leading = true; | ||
| let content_count = node.len() - trailing_count; | ||
| for (index, node) in node.iter().enumerate() { | ||
| if let AnyMdBlock::AnyMdLeafBlock(AnyMdLeafBlock::MdNewline(newline)) = node { | ||
| let is_leading = still_leading; | ||
| let is_trailing = index >= content_count; | ||
| joiner.entry(&newline.format().with_options(FormatMdNewlineOptions { | ||
| should_remove: is_leading || is_trailing, | ||
| })); | ||
| } else { | ||
| still_leading = false; | ||
| joiner.entry(&node.format()); | ||
| } | ||
| } | ||
|
|
||
| joiner.finish() | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct FormatMdBlockListOptions { | ||
| pub(crate) trim: bool, | ||
| } | ||
|
|
||
| impl FormatRuleWithOptions<MdBlockList> for FormatMdBlockList { | ||
| type Options = FormatMdBlockListOptions; | ||
|
|
||
| fn with_options(mut self, options: Self::Options) -> Self { | ||
| self.trim = options.trim; | ||
| self | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.