feat: --inline-attributes option for autofmt
This commit is contained in:
5 files changed
+48
-20
No files matched your search
@@ -67,7 +67,7 @@ impl Writer<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// multiline handlers bump everything down
|
// multiline handlers bump everything down
|
||||||
if attr_len > 1000 {
|
if attr_len > 1000 || self.out.indent.inline_attributes() {
|
||||||
opt_level = ShortOptimization::NoOpt;
|
opt_level = ShortOptimization::NoOpt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,11 +154,17 @@ impl Writer<'_> {
|
|||||||
manual_props: &Option<syn::Expr>,
|
manual_props: &Option<syn::Expr>,
|
||||||
sameline: bool,
|
sameline: bool,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
|
println!("fields {fields:?}");
|
||||||
|
println!("manual_props {manual_props:?}");
|
||||||
|
|
||||||
let mut field_iter = fields.iter().peekable();
|
let mut field_iter = fields.iter().peekable();
|
||||||
|
|
||||||
while let Some(field) = field_iter.next() {
|
while let Some(field) = field_iter.next() {
|
||||||
|
println!("{:?}", field);
|
||||||
if !sameline {
|
if !sameline {
|
||||||
self.out.indented_tabbed_line()?;
|
self.out.indented_tabbed_line().unwrap();
|
||||||
|
} else {
|
||||||
|
println!("not line????");
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = &field.name;
|
let name = &field.name;
|
||||||
@@ -200,13 +206,17 @@ impl Writer<'_> {
|
|||||||
|
|
||||||
if sameline {
|
if sameline {
|
||||||
write!(self.out, " ")?;
|
write!(self.out, " ")?;
|
||||||
|
println!("not line-----");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(exp) = manual_props {
|
if let Some(exp) = manual_props {
|
||||||
|
println!("{:?}", exp);
|
||||||
if !sameline {
|
if !sameline {
|
||||||
self.out.indented_tabbed_line()?;
|
self.out.indented_tabbed_line().unwrap();
|
||||||
|
} else {
|
||||||
|
println!("not line!!!!!!");
|
||||||
}
|
}
|
||||||
self.write_manual_props(exp)?;
|
self.write_manual_props(exp)?;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ impl Writer<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// multiline handlers bump everything down
|
// multiline handlers bump everything down
|
||||||
if attr_len > 1000 {
|
if attr_len > 1000 || self.out.indent.inline_attributes() {
|
||||||
opt_level = ShortOptimization::NoOpt;
|
opt_level = ShortOptimization::NoOpt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,10 +8,11 @@ pub enum IndentType {
|
|||||||
pub struct IndentOptions {
|
pub struct IndentOptions {
|
||||||
width: usize,
|
width: usize,
|
||||||
indent_string: String,
|
indent_string: String,
|
||||||
|
inline_attributes: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IndentOptions {
|
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");
|
assert_ne!(width, 0, "Cannot have an indent width of 0");
|
||||||
Self {
|
Self {
|
||||||
width,
|
width,
|
||||||
@@ -19,6 +20,7 @@ impl IndentOptions {
|
|||||||
IndentType::Tabs => "\t".into(),
|
IndentType::Tabs => "\t".into(),
|
||||||
IndentType::Spaces => " ".repeat(width),
|
IndentType::Spaces => " ".repeat(width),
|
||||||
},
|
},
|
||||||
|
inline_attributes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,11 +64,15 @@ impl IndentOptions {
|
|||||||
}
|
}
|
||||||
indent
|
indent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn inline_attributes(&self) -> bool {
|
||||||
|
self.inline_attributes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for IndentOptions {
|
impl Default for IndentOptions {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new(IndentType::Spaces, 4)
|
Self::new(IndentType::Spaces, 4, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,31 +83,31 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn count_indents() {
|
fn count_indents() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
IndentOptions::new(IndentType::Spaces, 4).count_indents("no indentation here!"),
|
IndentOptions::new(IndentType::Spaces, 4, false).count_indents("no indentation here!"),
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"),
|
IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"),
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"),
|
IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"),
|
||||||
2
|
2
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"),
|
IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"),
|
||||||
2
|
2
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
IndentOptions::new(IndentType::Spaces, 4).count_indents("\t\tv += 2"),
|
IndentOptions::new(IndentType::Spaces, 4, false).count_indents("\t\tv += 2"),
|
||||||
2
|
2
|
||||||
);
|
);
|
||||||
assert_eq!(
|
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
|
2
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
IndentOptions::new(IndentType::Spaces, 2).count_indents(" v += 2"),
|
IndentOptions::new(IndentType::Spaces, 2, false).count_indents(" v += 2"),
|
||||||
2
|
2
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ pub struct Autoformat {
|
|||||||
/// Input file
|
/// Input file
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
pub file: Option<String>,
|
pub file: Option<String>,
|
||||||
|
|
||||||
|
/// Inline attributes or not
|
||||||
|
#[clap(short, long)]
|
||||||
|
pub inline_attributes: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Autoformat {
|
impl Autoformat {
|
||||||
@@ -29,14 +33,14 @@ impl Autoformat {
|
|||||||
pub async fn autoformat(self) -> Result<()> {
|
pub async fn autoformat(self) -> Result<()> {
|
||||||
// Default to formatting the project
|
// Default to formatting the project
|
||||||
if self.raw.is_none() && self.file.is_none() {
|
if self.raw.is_none() && self.file.is_none() {
|
||||||
if let Err(e) = autoformat_project(self.check).await {
|
if let Err(e) = autoformat_project(self.check, self.inline_attributes).await {
|
||||||
eprintln!("error formatting project: {}", e);
|
eprintln!("error formatting project: {}", e);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(raw) = self.raw {
|
if let Some(raw) = self.raw {
|
||||||
let indent = indentation_for(".")?;
|
let indent = indentation_for(".", self.inline_attributes)?;
|
||||||
if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) {
|
if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) {
|
||||||
println!("{}", inner);
|
println!("{}", inner);
|
||||||
} else {
|
} else {
|
||||||
@@ -51,12 +55,12 @@ impl Autoformat {
|
|||||||
let file_content;
|
let file_content;
|
||||||
let indent;
|
let indent;
|
||||||
if file == "-" {
|
if file == "-" {
|
||||||
indent = indentation_for(".")?;
|
indent = indentation_for(".", self.inline_attributes)?;
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
std::io::stdin().read_to_string(&mut contents)?;
|
std::io::stdin().read_to_string(&mut contents)?;
|
||||||
file_content = Ok(contents);
|
file_content = Ok(contents);
|
||||||
} else {
|
} else {
|
||||||
indent = indentation_for(".")?;
|
indent = indentation_for(".", self.inline_attributes)?;
|
||||||
file_content = fs::read_to_string(&file);
|
file_content = fs::read_to_string(&file);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -93,7 +97,7 @@ impl Autoformat {
|
|||||||
/// Runs using Tokio for multithreading, so it should be really really fast
|
/// Runs using Tokio for multithreading, so it should be really really fast
|
||||||
///
|
///
|
||||||
/// Doesn't do mod-descending, so it will still try to format unreachable files. TODO.
|
/// Doesn't do mod-descending, so it will still try to format unreachable files. TODO.
|
||||||
async fn autoformat_project(check: bool) -> Result<()> {
|
async fn autoformat_project(check: bool, inline_attributes: bool) -> Result<()> {
|
||||||
let crate_config = crate::CrateConfig::new(None)?;
|
let crate_config = crate::CrateConfig::new(None)?;
|
||||||
|
|
||||||
let mut files_to_format = vec![];
|
let mut files_to_format = vec![];
|
||||||
@@ -103,7 +107,7 @@ async fn autoformat_project(check: bool) -> Result<()> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let indent = indentation_for(&files_to_format[0])?;
|
let indent = indentation_for(&files_to_format[0], inline_attributes)?;
|
||||||
|
|
||||||
let counts = files_to_format
|
let counts = files_to_format
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -164,7 +168,10 @@ async fn autoformat_project(check: bool) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn indentation_for(file_or_dir: impl AsRef<Path>) -> Result<IndentOptions> {
|
fn indentation_for(
|
||||||
|
file_or_dir: impl AsRef<Path>,
|
||||||
|
inline_attributes: bool,
|
||||||
|
) -> Result<IndentOptions> {
|
||||||
let out = std::process::Command::new("cargo")
|
let out = std::process::Command::new("cargo")
|
||||||
.args(["fmt", "--", "--print-config", "current"])
|
.args(["fmt", "--", "--print-config", "current"])
|
||||||
.arg(file_or_dir.as_ref())
|
.arg(file_or_dir.as_ref())
|
||||||
@@ -204,6 +211,7 @@ fn indentation_for(file_or_dir: impl AsRef<Path>) -> Result<IndentOptions> {
|
|||||||
IndentType::Spaces
|
IndentType::Spaces
|
||||||
},
|
},
|
||||||
tab_spaces,
|
tab_spaces,
|
||||||
|
inline_attributes,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,6 +262,7 @@ async fn test_auto_fmt() {
|
|||||||
check: false,
|
check: false,
|
||||||
raw: Some(test_rsx),
|
raw: Some(test_rsx),
|
||||||
file: None,
|
file: None,
|
||||||
|
inline_attributes: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
fmt.autoformat().await.unwrap();
|
fmt.autoformat().await.unwrap();
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ pub fn format_rsx(raw: String, use_tabs: bool, indent_size: usize) -> String {
|
|||||||
IndentType::Spaces
|
IndentType::Spaces
|
||||||
},
|
},
|
||||||
indent_size,
|
indent_size,
|
||||||
|
false,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
block.unwrap()
|
block.unwrap()
|
||||||
@@ -32,6 +33,7 @@ pub fn format_selection(raw: String, use_tabs: bool, indent_size: usize) -> Stri
|
|||||||
IndentType::Spaces
|
IndentType::Spaces
|
||||||
},
|
},
|
||||||
indent_size,
|
indent_size,
|
||||||
|
false,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
block.unwrap()
|
block.unwrap()
|
||||||
@@ -67,6 +69,7 @@ pub fn format_file(contents: String, use_tabs: bool, indent_size: usize) -> Form
|
|||||||
IndentType::Spaces
|
IndentType::Spaces
|
||||||
},
|
},
|
||||||
indent_size,
|
indent_size,
|
||||||
|
false,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
let out = dioxus_autofmt::apply_formats(&contents, _edits.clone());
|
let out = dioxus_autofmt::apply_formats(&contents, _edits.clone());
|
||||||
|
|||||||
Reference in new issue
Block a user