-
-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(format/html): attribute formatting
- Loading branch information
Showing
5 changed files
with
52 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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,12 @@ | ||
use crate::prelude::*; | ||
use biome_html_syntax::HtmlAttribute; | ||
use biome_rowan::AstNode; | ||
use biome_formatter::write; | ||
use biome_html_syntax::{HtmlAttribute, HtmlAttributeFields}; | ||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatHtmlAttribute; | ||
impl FormatNodeRule<HtmlAttribute> for FormatHtmlAttribute { | ||
fn fmt_fields(&self, node: &HtmlAttribute, f: &mut HtmlFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let HtmlAttributeFields { name, initializer } = node.as_fields(); | ||
|
||
write![f, [name.format(), initializer.format()]] | ||
} | ||
} |
This file contains 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 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 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,34 @@ | ||
use crate::prelude::*; | ||
use biome_html_syntax::HtmlString; | ||
use biome_rowan::AstNode; | ||
use biome_formatter::{format_args, write}; | ||
use biome_html_syntax::{HtmlString, HtmlStringFields}; | ||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatHtmlString; | ||
impl FormatNodeRule<HtmlString> for FormatHtmlString { | ||
fn fmt_fields(&self, node: &HtmlString, f: &mut HtmlFormatter) -> FormatResult<()> { | ||
format_verbatim_node(node.syntax()).fmt(f) | ||
let HtmlStringFields { value_token } = node.as_fields(); | ||
|
||
// Prettier always uses double quotes for HTML strings, regardless of configuration. | ||
if let Ok(value) = value_token.as_ref() { | ||
let value_text = value.text(); | ||
if value_text.starts_with('\'') && value_text.ends_with('\'') { | ||
write!( | ||
f, | ||
[format_replaced( | ||
value, | ||
&group(&format_args![ | ||
text("\""), | ||
located_token_text( | ||
value, | ||
value.text_range().add_start(1.into()).sub_end(1.into()), | ||
), | ||
text("\""), | ||
]) | ||
)] | ||
)?; | ||
return Ok(()); | ||
} | ||
} | ||
|
||
write!(f, [value_token.format()]) | ||
} | ||
} |
11 changes: 10 additions & 1 deletion
11
crates/biome_html_formatter/src/html/lists/attribute_list.rs
This file contains 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,19 @@ | ||
use crate::prelude::*; | ||
use biome_formatter::AttributePosition; | ||
use biome_html_syntax::HtmlAttributeList; | ||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct FormatHtmlAttributeList; | ||
impl FormatRule<HtmlAttributeList> for FormatHtmlAttributeList { | ||
type Context = HtmlFormatContext; | ||
fn fmt(&self, node: &HtmlAttributeList, f: &mut HtmlFormatter) -> FormatResult<()> { | ||
f.join().entries(node.iter().formatted()).finish() | ||
let line_break = if f.options().attribute_position() == AttributePosition::Multiline { | ||
hard_line_break() | ||
} else { | ||
soft_line_break_or_space() | ||
}; | ||
|
||
f.join_with(&line_break) | ||
.entries(node.iter().formatted()) | ||
.finish() | ||
} | ||
} |