Skip to content

Commit

Permalink
refactor: impl Copy for Language
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jan 18, 2025
1 parent 8128b14 commit e0597ce
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion markup_fmt/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ where
}
_ => match self.options.whitespace_sensitivity {
WhitespaceSensitivity::Css => {
helpers::is_whitespace_sensitive_tag(tag_name, self.language.clone())
helpers::is_whitespace_sensitive_tag(tag_name, self.language)
}
WhitespaceSensitivity::Strict => true,
WhitespaceSensitivity::Ignore => false,
Expand Down
2 changes: 1 addition & 1 deletion markup_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn format_text<E, F>(
where
F: for<'a> FnMut(&'a str, Hints) -> Result<Cow<'a, str>, E>,
{
let mut parser = Parser::new(code, language.clone());
let mut parser = Parser::new(code, language);
let ast = parser.parse_root().map_err(FormatError::Syntax)?;

if ast.children.first().is_some_and(|child| {
Expand Down
19 changes: 11 additions & 8 deletions markup_fmt/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
};
use std::{cmp::Ordering, iter::Peekable, ops::ControlFlow, str::CharIndices};

#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Supported languages.
pub enum Language {
Html,
Expand Down Expand Up @@ -930,7 +930,7 @@ impl<'s> Parser<'s> {
return Err(self.emit_error(SyntaxErrorKind::ExpectElement));
};
let tag_name = self.parse_tag_name()?;
let void_element = helpers::is_void_element(tag_name, self.language.clone());
let void_element = helpers::is_void_element(tag_name, self.language);

let mut attrs = vec![];
let mut first_attr_same_line = true;
Expand Down Expand Up @@ -1403,7 +1403,7 @@ impl<'s> Parser<'s> {
match chars.next() {
Some((_, c))
if is_html_tag_name_char(c)
|| is_special_tag_name_char(c, &self.language) =>
|| is_special_tag_name_char(c, self.language) =>
{
self.parse_element().map(NodeKind::Element)
}
Expand Down Expand Up @@ -2305,7 +2305,7 @@ impl<'s> Parser<'s> {
match chars.next() {
Some((_, c))
if is_html_tag_name_char(c)
|| is_special_tag_name_char(c, &self.language)
|| is_special_tag_name_char(c, self.language)
|| c == '/'
|| c == '!' =>
{
Expand Down Expand Up @@ -2566,14 +2566,17 @@ fn is_html_tag_name_char(c: char) -> bool {
|| c == '\\'
}

/// Checks whether a character is valid in an HTML tag name, for a specific template languages.
/// Checks whether a character is valid in an HTML tag name, for specific template languages.
///
/// For example:
/// - Astro allows '>' in tag names (for fragments)
/// - Jinja allows '{' for template expressions like <{{ tag_name }}>
fn is_special_tag_name_char(c: char, language: &Language) -> bool {
c == '>' && matches!(language, Language::Astro)
|| c == '{' && matches!(language, Language::Jinja)
fn is_special_tag_name_char(c: char, language: Language) -> bool {
match language {
Language::Astro => c == '>',
Language::Jinja => c == '{',
_ => false,
}
}

fn is_attr_name_char(c: char) -> bool {
Expand Down
8 changes: 4 additions & 4 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,11 @@ impl<'s> DocGen<'s> for Element<'s> {
.iter()
.any(|tag| tag.eq_ignore_ascii_case(self.tag_name));

let self_closing = if helpers::is_void_element(tag_name, ctx.language.clone()) {
let self_closing = if helpers::is_void_element(tag_name, ctx.language) {
ctx.options
.html_void_self_closing
.unwrap_or(self.self_closing)
} else if helpers::is_html_tag(tag_name, ctx.language.clone()) {
} else if helpers::is_html_tag(tag_name, ctx.language) {
ctx.options
.html_normal_self_closing
.unwrap_or(self.self_closing)
Expand All @@ -429,9 +429,9 @@ impl<'s> DocGen<'s> for Element<'s> {
ctx.options
.component_self_closing
.unwrap_or(self.self_closing)
} else if helpers::is_svg_tag(self.tag_name, ctx.language.clone()) {
} else if helpers::is_svg_tag(self.tag_name, ctx.language) {
ctx.options.svg_self_closing.unwrap_or(self.self_closing)
} else if helpers::is_mathml_tag(self.tag_name, ctx.language.clone()) {
} else if helpers::is_mathml_tag(self.tag_name, ctx.language) {
ctx.options.mathml_self_closing.unwrap_or(self.self_closing)
} else {
self.self_closing
Expand Down

0 comments on commit e0597ce

Please sign in to comment.