fix formatting and merging if chains in attributes (#2697)

* fix formatting and merging if chains in attributes

* fix autoformat if attribute chains

* Fix IfAttributeValue ToTokens implementation

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
Evan AlmloffandJonathan Kelley authored and GitHub committed 2024-07-24 18:56:21 -07:00
1 parent a6e41e9d68
commit 7f210633eb
8 files changed
+301 -145

No files matched your search

+6 -28
View File
@@ -20,6 +20,12 @@ pub struct IfmtInput {
pub segments: Vec<Segment>,
}
impl Default for IfmtInput {
fn default() -> Self {
Self::new(Span::call_site())
}
}
impl IfmtInput {
pub fn new(span: Span) -> Self {
Self {
@@ -45,22 +51,6 @@ impl IfmtInput {
self.segments.extend(other.segments);
}
pub fn push_condition(&mut self, condition: Expr, contents: IfmtInput) {
let desugared = quote! {
{
let _cond = if #condition { #contents.to_string() } else { String::new() };
_cond
}
};
let parsed = syn::parse2::<Expr>(desugared).unwrap();
self.segments.push(Segment::Formatted(FormattedSegment {
format_args: String::new(),
segment: FormattedSegmentType::Expr(Box::new(parsed)),
}));
}
pub fn push_expr(&mut self, expr: Expr) {
self.segments.push(Segment::Formatted(FormattedSegment {
format_args: String::new(),
@@ -481,18 +471,6 @@ mod tests {
assert!(input.is_static());
}
#[test]
fn pushing_conditional() {
let mut input = syn::parse2::<IfmtInput>(quote! { "hello " }).unwrap();
input.push_condition(
parse_quote! { true },
syn::parse2::<IfmtInput>(quote! { "world" }).unwrap(),
);
println!("{}", input.to_token_stream().pretty_unparse());
dbg!(input.segments);
}
#[test]
fn fmt_segments() {
let left = syn::parse2::<IfmtInput>(quote! { "thing {abc}" }).unwrap();