feat: --inline-attributes option for autofmt

This commit is contained in:
marc2332 committed 2023-12-09 11:16:26 +01:00
1 parent 544ca55596
commit 52cb5ff951
5 files changed
+48 -20

No files matched your search

+13 -3
View File
@@ -67,7 +67,7 @@ impl Writer<'_> {
}
// multiline handlers bump everything down
if attr_len > 1000 {
if attr_len > 1000 || self.out.indent.inline_attributes() {
opt_level = ShortOptimization::NoOpt;
}
@@ -154,11 +154,17 @@ impl Writer<'_> {
manual_props: &Option<syn::Expr>,
sameline: bool,
) -> Result {
println!("fields {fields:?}");
println!("manual_props {manual_props:?}");
let mut field_iter = fields.iter().peekable();
while let Some(field) = field_iter.next() {
println!("{:?}", field);
if !sameline {
self.out.indented_tabbed_line()?;
self.out.indented_tabbed_line().unwrap();
} else {
println!("not line????");
}
let name = &field.name;
@@ -200,13 +206,17 @@ impl Writer<'_> {
if sameline {
write!(self.out, " ")?;
println!("not line-----");
}
}
}
if let Some(exp) = manual_props {
println!("{:?}", exp);
if !sameline {
self.out.indented_tabbed_line()?;
self.out.indented_tabbed_line().unwrap();
} else {
println!("not line!!!!!!");
}
self.write_manual_props(exp)?;
}
+1 -1
View File
@@ -102,7 +102,7 @@ impl Writer<'_> {
}
// multiline handlers bump everything down
if attr_len > 1000 {
if attr_len > 1000 || self.out.indent.inline_attributes() {
opt_level = ShortOptimization::NoOpt;
}
+15 -9
View File
@@ -8,10 +8,11 @@ pub enum IndentType {
pub struct IndentOptions {
width: usize,
indent_string: String,
inline_attributes: bool,
}
impl IndentOptions {
pub fn new(typ: IndentType, width: usize) -> Self {
pub fn new(typ: IndentType, width: usize, inline_attributes: bool) -> Self {
assert_ne!(width, 0, "Cannot have an indent width of 0");
Self {
width,
@@ -19,6 +20,7 @@ impl IndentOptions {
IndentType::Tabs => "\t".into(),
IndentType::Spaces => " ".repeat(width),
},
inline_attributes,
}
}
@@ -62,11 +64,15 @@ impl IndentOptions {
}
indent
}
pub fn inline_attributes(&self) -> bool {
self.inline_attributes
}
}
impl Default for IndentOptions {
fn default() -> Self {
Self::new(IndentType::Spaces, 4)
Self::new(IndentType::Spaces, 4, false)
}
}
@@ -77,31 +83,31 @@ mod tests {
#[test]
fn count_indents() {
assert_eq!(
IndentOptions::new(IndentType::Spaces, 4).count_indents("no indentation here!"),
IndentOptions::new(IndentType::Spaces, 4, false).count_indents("no indentation here!"),
0
);
assert_eq!(
IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"),
IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"),
1
);
assert_eq!(
IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"),
IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"),
2
);
assert_eq!(
IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"),
IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"),
2
);
assert_eq!(
IndentOptions::new(IndentType::Spaces, 4).count_indents("\t\tv += 2"),
IndentOptions::new(IndentType::Spaces, 4, false).count_indents("\t\tv += 2"),
2
);
assert_eq!(
IndentOptions::new(IndentType::Spaces, 4).count_indents("\t\t v += 2"),
IndentOptions::new(IndentType::Spaces, 4, false).count_indents("\t\t v += 2"),
2
);
assert_eq!(
IndentOptions::new(IndentType::Spaces, 2).count_indents(" v += 2"),
IndentOptions::new(IndentType::Spaces, 2, false).count_indents(" v += 2"),
2
);
}