Skip to content

Commit

Permalink
Slightly more correctly formatting multiline attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Glusker committed Jan 15, 2024
1 parent 6356fca commit 13dd829
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::shape::Shape;
use crate::source_map::SpanUtils;
use crate::types::{rewrite_path, PathContext};
use crate::utils::{count_newlines, mk_sp};
use std::ops::Add;

mod doc_comment;

Expand Down Expand Up @@ -317,6 +318,7 @@ impl Rewrite for ast::MetaItem {
impl Rewrite for ast::Attribute {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
let snippet = context.snippet(self.span);
dbg!(snippet);
if self.is_doc_comment() {
rewrite_doc_comment(snippet, shape.comment(context.config), context.config)
} else {
Expand Down Expand Up @@ -358,7 +360,29 @@ impl Rewrite for ast::Attribute {
.map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
)
} else {
Some(snippet.to_owned())
let count = count_newlines(snippet);
if count > 0 {
let mut lines = snippet.lines();
let mut res = String::new();
let first: &str = lines.next().unwrap();
//first string without indent
res.push_str(first);
res.push('\n');
let indent_last = shape.indent.to_string(context.config);
let indent_middle = shape.indent.add(4).to_string(context.config);
for _ in 1..count {
let next = lines.next().unwrap().trim();
res.push_str(&indent_middle);
res.push_str(next);
res.push('\n');
}
let last = lines.next().unwrap().trim();
res.push_str(&indent_last);
res.push_str(last);
Some(res)
} else {
Some(snippet.to_owned())
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/source/issue-5974/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
macro_rules! repro {
() => {
#[doc = concat!("let var = ",
"false;")]
fn f() {}
};
}

7 changes: 7 additions & 0 deletions tests/target/issue-5974/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
macro_rules! repro {
() => {
#[doc = concat!("let var = ",
"false;")]
fn f() {}
};
}

0 comments on commit 13dd829

Please sign in to comment.