rename to split-line-attributes
This commit is contained in:
5 files changed
+26
-21
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 || self.out.indent.inline_attributes() {
|
if attr_len > 1000 || self.out.indent.split_line_attributes() {
|
||||||
opt_level = ShortOptimization::NoOpt;
|
opt_level = ShortOptimization::NoOpt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ impl Writer<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// multiline handlers bump everything down
|
// multiline handlers bump everything down
|
||||||
if attr_len > 1000 || self.out.indent.inline_attributes() {
|
if attr_len > 1000 || self.out.indent.split_line_attributes() {
|
||||||
opt_level = ShortOptimization::NoOpt;
|
opt_level = ShortOptimization::NoOpt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,7 +254,6 @@ impl Writer<'_> {
|
|||||||
|
|
||||||
ElementAttr::EventTokens { name, tokens } => {
|
ElementAttr::EventTokens { name, tokens } => {
|
||||||
let out = self.retrieve_formatted_expr(tokens).to_string();
|
let out = self.retrieve_formatted_expr(tokens).to_string();
|
||||||
|
|
||||||
let mut lines = out.split('\n').peekable();
|
let mut lines = out.split('\n').peekable();
|
||||||
let first = lines.next().unwrap();
|
let first = lines.next().unwrap();
|
||||||
|
|
||||||
@@ -297,6 +296,10 @@ impl Writer<'_> {
|
|||||||
beginning.is_empty()
|
beginning.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_empty_children(&self, children: &[BodyNode]) -> bool {
|
||||||
|
children.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
// check if the children are short enough to be on the same line
|
// check if the children are short enough to be on the same line
|
||||||
// We don't have the notion of current line depth - each line tries to be < 80 total
|
// We don't have the notion of current line depth - each line tries to be < 80 total
|
||||||
// returns the total line length if it's short
|
// returns the total line length if it's short
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ pub enum IndentType {
|
|||||||
pub struct IndentOptions {
|
pub struct IndentOptions {
|
||||||
width: usize,
|
width: usize,
|
||||||
indent_string: String,
|
indent_string: String,
|
||||||
inline_attributes: bool,
|
split_line_attributes: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IndentOptions {
|
impl IndentOptions {
|
||||||
pub fn new(typ: IndentType, width: usize, inline_attributes: bool) -> Self {
|
pub fn new(typ: IndentType, width: usize, split_line_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,
|
||||||
@@ -20,7 +20,7 @@ impl IndentOptions {
|
|||||||
IndentType::Tabs => "\t".into(),
|
IndentType::Tabs => "\t".into(),
|
||||||
IndentType::Spaces => " ".repeat(width),
|
IndentType::Spaces => " ".repeat(width),
|
||||||
},
|
},
|
||||||
inline_attributes,
|
split_line_attributes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,8 +65,8 @@ impl IndentOptions {
|
|||||||
indent
|
indent
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inline_attributes(&self) -> bool {
|
pub fn split_line_attributes(&self) -> bool {
|
||||||
self.inline_attributes
|
self.split_line_attributes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -140,7 +140,9 @@ pub fn write_block_out(body: CallBody) -> Option<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn write_body(buf: &mut Writer, body: &CallBody) {
|
fn write_body(buf: &mut Writer, body: &CallBody) {
|
||||||
if buf.is_short_children(&body.roots).is_some() && !buf.out.indent.inline_attributes() {
|
let is_short = buf.is_short_children(&body.roots).is_some();
|
||||||
|
let is_empty = buf.is_empty_children(&body.roots);
|
||||||
|
if (is_short && !buf.out.indent.split_line_attributes()) || is_empty {
|
||||||
// write all the indents with spaces and commas between
|
// write all the indents with spaces and commas between
|
||||||
for idx in 0..body.roots.len() - 1 {
|
for idx in 0..body.roots.len() - 1 {
|
||||||
let ident = &body.roots[idx];
|
let ident = &body.roots[idx];
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ pub struct Autoformat {
|
|||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
pub file: Option<String>,
|
pub file: Option<String>,
|
||||||
|
|
||||||
/// Inline attributes or not
|
/// Split attributes in lines or not
|
||||||
#[clap(short, long)]
|
#[clap(short, long, default_value = "false")]
|
||||||
pub inline_attributes: bool,
|
pub split_line_attributes: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Autoformat {
|
impl Autoformat {
|
||||||
@@ -33,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, self.inline_attributes).await {
|
if let Err(e) = autoformat_project(self.check, self.split_line_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(".", self.inline_attributes)?;
|
let indent = indentation_for(".", self.split_line_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 {
|
||||||
@@ -55,12 +55,12 @@ impl Autoformat {
|
|||||||
let file_content;
|
let file_content;
|
||||||
let indent;
|
let indent;
|
||||||
if file == "-" {
|
if file == "-" {
|
||||||
indent = indentation_for(".", self.inline_attributes)?;
|
indent = indentation_for(".", self.split_line_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(".", self.inline_attributes)?;
|
indent = indentation_for(".", self.split_line_attributes)?;
|
||||||
file_content = fs::read_to_string(&file);
|
file_content = fs::read_to_string(&file);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -97,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, inline_attributes: bool) -> Result<()> {
|
async fn autoformat_project(check: bool, split_line_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![];
|
||||||
@@ -107,7 +107,7 @@ async fn autoformat_project(check: bool, inline_attributes: bool) -> Result<()>
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let indent = indentation_for(&files_to_format[0], inline_attributes)?;
|
let indent = indentation_for(&files_to_format[0], split_line_attributes)?;
|
||||||
|
|
||||||
let counts = files_to_format
|
let counts = files_to_format
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -170,7 +170,7 @@ async fn autoformat_project(check: bool, inline_attributes: bool) -> Result<()>
|
|||||||
|
|
||||||
fn indentation_for(
|
fn indentation_for(
|
||||||
file_or_dir: impl AsRef<Path>,
|
file_or_dir: impl AsRef<Path>,
|
||||||
inline_attributes: bool,
|
split_line_attributes: bool,
|
||||||
) -> Result<IndentOptions> {
|
) -> 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"])
|
||||||
@@ -211,7 +211,7 @@ fn indentation_for(
|
|||||||
IndentType::Spaces
|
IndentType::Spaces
|
||||||
},
|
},
|
||||||
tab_spaces,
|
tab_spaces,
|
||||||
inline_attributes,
|
split_line_attributes,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,7 +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,
|
split_line_attributes: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
fmt.autoformat().await.unwrap();
|
fmt.autoformat().await.unwrap();
|
||||||
|
|||||||
Reference in new issue
Block a user