Skip to content

Commit

Permalink
feat: add tailwind support for attr values
Browse files Browse the repository at this point in the history
  • Loading branch information
bram209 committed Jun 12, 2024
1 parent f4229d6 commit 7969ace
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 32 deletions.
132 changes: 130 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ leptosfmt-prettyplease.workspace = true
rstml = "0.11.2"
syn = { workspace = true }
proc-macro2 = { workspace = true }
thiserror = "1.0.40"
thiserror = "1.0.61"
crop = "0.3.0"
serde = { version = "1.0.163", features = ["derive"] }
quote = "1.0.26"
serde = { version = "1.0.203", features = ["derive"] }
quote = "1.0.36"
rustywind_core = "0.1.2"

[dev-dependencies]
indoc = "2.0.1"
insta = "1.28.0"
quote = "1.0.26"
indoc = "2.0.5"
insta = "1.39.0"
quote = "1.0.36"
24 changes: 17 additions & 7 deletions formatter/src/formatter/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use syn::{spanned::Spanned, Expr};

use crate::{formatter::Formatter, AttributeValueBraceStyle as Braces};

use super::ExpressionFormatter;

impl Formatter<'_> {
pub fn attribute(&mut self, attribute: &NodeAttribute) {
self.flush_comments(attribute.span().start().line - 1);
Expand All @@ -16,24 +18,32 @@ impl Formatter<'_> {
self.node_name(&attribute.key);

if let Some(value) = attribute.value() {
let formatter = self
.settings
.attr_values
.get(&attribute.key.to_string())
.copied();

self.printer.word("=");
self.attribute_value(value);
self.attribute_value(value, formatter);
}
}

fn attribute_value(&mut self, value: &Expr) {
fn attribute_value(&mut self, value: &Expr, formatter: Option<ExpressionFormatter>) {
match (self.settings.attr_value_brace_style, value) {
(Braces::Always, syn::Expr::Block(_)) => self.node_value_expr(value, false, false),
(Braces::Always, syn::Expr::Block(_)) => {
self.node_value_expr(value, false, false, formatter)
}
(Braces::AlwaysUnlessLit, syn::Expr::Block(_) | syn::Expr::Lit(_)) => {
self.node_value_expr(value, false, true)
self.node_value_expr(value, false, true, formatter)
}
(Braces::Always | Braces::AlwaysUnlessLit, _) => {
self.printer.word("{");
self.node_value_expr(value, false, false);
self.node_value_expr(value, false, false, formatter);
self.printer.word("}");
}
(Braces::WhenRequired, _) => self.node_value_expr(value, true, true),
(Braces::Preserve, _) => self.node_value_expr(value, false, false),
(Braces::WhenRequired, _) => self.node_value_expr(value, true, true, formatter),
(Braces::Preserve, _) => self.node_value_expr(value, false, false, formatter),
}
}
}
Expand Down
30 changes: 20 additions & 10 deletions formatter/src/formatter/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use syn::{spanned::Spanned, Block, Expr, ExprBlock, ExprLit, LitStr};

use crate::{formatter::Formatter, get_text_beween_spans, view_macro::ViewMacroFormatter};

use super::ExpressionFormatter;

fn trim_start_with_max(str: &str, max_chars: usize) -> &str {
let mut chars = 0;
str.trim_start_matches(|c: char| {
Expand Down Expand Up @@ -65,27 +67,31 @@ impl Formatter<'_> {
if unwrap_single_expr_blocks
|| (unwrap_single_lit_blocks && matches!(single_expr, syn::Expr::Lit(_)))
{
self.expr(single_expr);
self.expr(single_expr, None);
} else {
self.printer.word("{");
self.expr(single_expr);
self.expr(single_expr, None);
self.printer.word("}");
}
return;
}

self.expr(&Expr::Block(ExprBlock {
attrs: vec![],
label: None,
block: block.clone(),
}))
self.expr(
&Expr::Block(ExprBlock {
attrs: vec![],
label: None,
block: block.clone(),
}),
None,
)
}

pub fn node_value_expr(
&mut self,
value: &syn::Expr,
unwrap_single_expr_blocks: bool,
unwrap_single_lit_blocks: bool,
formatter: Option<ExpressionFormatter>,
) {
// if single line expression, format as '{expr}' instead of '{ expr }' (prettyplease inserts a space)
if let syn::Expr::Block(expr_block) = value {
Expand All @@ -98,18 +104,22 @@ impl Formatter<'_> {
}
}

self.expr(value)
self.expr(value, formatter)
}

fn expr(&mut self, expr: &syn::Expr) {
fn expr(&mut self, expr: &syn::Expr, formatter: Option<ExpressionFormatter>) {
let span = expr.span();
self.flush_comments(span.start().line - 1);
if let syn::Expr::Lit(ExprLit {
lit: syn::Lit::Str(lit_str),
..
}) = expr
{
self.literal_str(lit_str);
if let Some(formatter) = formatter {
formatter.format(self, lit_str.value())
} else {
self.literal_str(lit_str);
}
return;
}

Expand Down
Loading

0 comments on commit 7969ace

Please sign in to comment.