Skip to content

Commit 18a56bf

Browse files
authored
Merge pull request #1701 from marc2332/feat/inline-attributes-fmt
feat: `--split-line-attributes` parameter for CLI `fmt`
2 parents dd4dc93 + 2876a7d commit 18a56bf

File tree

7 files changed

+59
-30
lines changed

7 files changed

+59
-30
lines changed

packages/autofmt/src/component.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl Writer<'_> {
6767
}
6868

6969
// multiline handlers bump everything down
70-
if attr_len > 1000 {
70+
if attr_len > 1000 || self.out.indent.split_line_attributes() {
7171
opt_level = ShortOptimization::NoOpt;
7272
}
7373

@@ -158,7 +158,7 @@ impl Writer<'_> {
158158

159159
while let Some(field) = field_iter.next() {
160160
if !sameline {
161-
self.out.indented_tabbed_line()?;
161+
self.out.indented_tabbed_line().unwrap();
162162
}
163163

164164
let name = &field.name;
@@ -206,7 +206,7 @@ impl Writer<'_> {
206206

207207
if let Some(exp) = manual_props {
208208
if !sameline {
209-
self.out.indented_tabbed_line()?;
209+
self.out.indented_tabbed_line().unwrap();
210210
}
211211
self.write_manual_props(exp)?;
212212
}

packages/autofmt/src/element.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl Writer<'_> {
103103
}
104104

105105
// multiline handlers bump everything down
106-
if attr_len > 1000 {
106+
if attr_len > 1000 || self.out.indent.split_line_attributes() {
107107
opt_level = ShortOptimization::NoOpt;
108108
}
109109

@@ -262,7 +262,6 @@ impl Writer<'_> {
262262
}
263263
ElementAttrValue::EventTokens(tokens) => {
264264
let out = self.retrieve_formatted_expr(tokens).to_string();
265-
266265
let mut lines = out.split('\n').peekable();
267266
let first = lines.next().unwrap();
268267

@@ -327,6 +326,10 @@ impl Writer<'_> {
327326
beginning.is_empty()
328327
}
329328

329+
pub fn is_empty_children(&self, children: &[BodyNode]) -> bool {
330+
children.is_empty()
331+
}
332+
330333
// check if the children are short enough to be on the same line
331334
// We don't have the notion of current line depth - each line tries to be < 80 total
332335
// returns the total line length if it's short

packages/autofmt/src/indent.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ pub enum IndentType {
88
pub struct IndentOptions {
99
width: usize,
1010
indent_string: String,
11+
split_line_attributes: bool,
1112
}
1213

1314
impl IndentOptions {
14-
pub fn new(typ: IndentType, width: usize) -> Self {
15+
pub fn new(typ: IndentType, width: usize, split_line_attributes: bool) -> Self {
1516
assert_ne!(width, 0, "Cannot have an indent width of 0");
1617
Self {
1718
width,
1819
indent_string: match typ {
1920
IndentType::Tabs => "\t".into(),
2021
IndentType::Spaces => " ".repeat(width),
2122
},
23+
split_line_attributes,
2224
}
2325
}
2426

@@ -62,11 +64,15 @@ impl IndentOptions {
6264
}
6365
indent
6466
}
67+
68+
pub fn split_line_attributes(&self) -> bool {
69+
self.split_line_attributes
70+
}
6571
}
6672

6773
impl Default for IndentOptions {
6874
fn default() -> Self {
69-
Self::new(IndentType::Spaces, 4)
75+
Self::new(IndentType::Spaces, 4, false)
7076
}
7177
}
7278

@@ -77,31 +83,31 @@ mod tests {
7783
#[test]
7884
fn count_indents() {
7985
assert_eq!(
80-
IndentOptions::new(IndentType::Spaces, 4).count_indents("no indentation here!"),
86+
IndentOptions::new(IndentType::Spaces, 4, false).count_indents("no indentation here!"),
8187
0
8288
);
8389
assert_eq!(
84-
IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"),
90+
IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"),
8591
1
8692
);
8793
assert_eq!(
88-
IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"),
94+
IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"),
8995
2
9096
);
9197
assert_eq!(
92-
IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"),
98+
IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"),
9399
2
94100
);
95101
assert_eq!(
96-
IndentOptions::new(IndentType::Spaces, 4).count_indents("\t\tv += 2"),
102+
IndentOptions::new(IndentType::Spaces, 4, false).count_indents("\t\tv += 2"),
97103
2
98104
);
99105
assert_eq!(
100-
IndentOptions::new(IndentType::Spaces, 4).count_indents("\t\t v += 2"),
106+
IndentOptions::new(IndentType::Spaces, 4, false).count_indents("\t\t v += 2"),
101107
2
102108
);
103109
assert_eq!(
104-
IndentOptions::new(IndentType::Spaces, 2).count_indents(" v += 2"),
110+
IndentOptions::new(IndentType::Spaces, 2, false).count_indents(" v += 2"),
105111
2
106112
);
107113
}

packages/autofmt/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ pub fn write_block_out(body: CallBody) -> Option<String> {
140140
}
141141

142142
fn write_body(buf: &mut Writer, body: &CallBody) {
143-
if buf.is_short_children(&body.roots).is_some() {
143+
let is_short = buf.is_short_children(&body.roots).is_some();
144+
let is_empty = buf.is_empty_children(&body.roots);
145+
if (is_short && !buf.out.indent.split_line_attributes()) || is_empty {
144146
// write all the indents with spaces and commas between
145147
for idx in 0..body.roots.len() - 1 {
146148
let ident = &body.roots[idx];

packages/autofmt/tests/wrong.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ macro_rules! twoway {
1818
};
1919
}
2020

21-
twoway!("comments-4sp" => comments_4sp (IndentOptions::new(IndentType::Spaces, 4)));
22-
twoway!("comments-tab" => comments_tab (IndentOptions::new(IndentType::Tabs, 4)));
21+
twoway!("comments-4sp" => comments_4sp (IndentOptions::new(IndentType::Spaces, 4, false)));
22+
twoway!("comments-tab" => comments_tab (IndentOptions::new(IndentType::Tabs, 4, false)));
2323

24-
twoway!("multi-4sp" => multi_4sp (IndentOptions::new(IndentType::Spaces, 4)));
25-
twoway!("multi-tab" => multi_tab (IndentOptions::new(IndentType::Tabs, 4)));
24+
twoway!("multi-4sp" => multi_4sp (IndentOptions::new(IndentType::Spaces, 4, false)));
25+
twoway!("multi-tab" => multi_tab (IndentOptions::new(IndentType::Tabs, 4, false)));
2626

27-
twoway!("multiexpr-4sp" => multiexpr_4sp (IndentOptions::new(IndentType::Spaces, 4)));
28-
twoway!("multiexpr-tab" => multiexpr_tab (IndentOptions::new(IndentType::Tabs, 4)));
27+
twoway!("multiexpr-4sp" => multiexpr_4sp (IndentOptions::new(IndentType::Spaces, 4, false)));
28+
twoway!("multiexpr-tab" => multiexpr_tab (IndentOptions::new(IndentType::Tabs, 4, false)));

packages/cli/src/cli/autoformat.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,33 @@ pub struct Autoformat {
2222
/// Input file
2323
#[clap(short, long)]
2424
pub file: Option<String>,
25+
26+
/// Split attributes in lines or not
27+
#[clap(short, long, default_value = "false")]
28+
pub split_line_attributes: bool,
2529
}
2630

2731
impl Autoformat {
2832
// Todo: autoformat the entire crate
2933
pub async fn autoformat(self) -> Result<()> {
30-
let Autoformat { check, raw, file } = self;
34+
let Autoformat {
35+
check,
36+
raw,
37+
file,
38+
split_line_attributes,
39+
..
40+
} = self;
3141

3242
// Default to formatting the project
3343
if raw.is_none() && file.is_none() {
34-
if let Err(e) = autoformat_project(check).await {
44+
if let Err(e) = autoformat_project(check, split_line_attributes).await {
3545
eprintln!("error formatting project: {}", e);
3646
exit(1);
3747
}
3848
}
3949

4050
if let Some(raw) = raw {
41-
let indent = indentation_for(".")?;
51+
let indent = indentation_for(".", self.split_line_attributes)?;
4252
if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) {
4353
println!("{}", inner);
4454
} else {
@@ -50,15 +60,15 @@ impl Autoformat {
5060

5161
// Format single file
5262
if let Some(file) = file {
53-
refactor_file(file)?;
63+
refactor_file(file, split_line_attributes)?;
5464
}
5565

5666
Ok(())
5767
}
5868
}
5969

60-
fn refactor_file(file: String) -> Result<(), Error> {
61-
let indent = indentation_for(".")?;
70+
fn refactor_file(file: String, split_line_attributes: bool) -> Result<(), Error> {
71+
let indent = indentation_for(".", split_line_attributes)?;
6272
let file_content = if file == "-" {
6373
let mut contents = String::new();
6474
std::io::stdin().read_to_string(&mut contents)?;
@@ -138,7 +148,7 @@ async fn format_file(
138148
/// Runs using Tokio for multithreading, so it should be really really fast
139149
///
140150
/// Doesn't do mod-descending, so it will still try to format unreachable files. TODO.
141-
async fn autoformat_project(check: bool) -> Result<()> {
151+
async fn autoformat_project(check: bool, split_line_attributes: bool) -> Result<()> {
142152
let crate_config = dioxus_cli_config::CrateConfig::new(None)?;
143153

144154
let files_to_format = get_project_files(&crate_config);
@@ -147,7 +157,7 @@ async fn autoformat_project(check: bool) -> Result<()> {
147157
return Ok(());
148158
}
149159

150-
let indent = indentation_for(&files_to_format[0])?;
160+
let indent = indentation_for(&files_to_format[0], split_line_attributes)?;
151161

152162
let counts = files_to_format
153163
.into_iter()
@@ -181,7 +191,10 @@ async fn autoformat_project(check: bool) -> Result<()> {
181191
Ok(())
182192
}
183193

184-
fn indentation_for(file_or_dir: impl AsRef<Path>) -> Result<IndentOptions> {
194+
fn indentation_for(
195+
file_or_dir: impl AsRef<Path>,
196+
split_line_attributes: bool,
197+
) -> Result<IndentOptions> {
185198
let out = std::process::Command::new("cargo")
186199
.args(["fmt", "--", "--print-config", "current"])
187200
.arg(file_or_dir.as_ref())
@@ -221,6 +234,7 @@ fn indentation_for(file_or_dir: impl AsRef<Path>) -> Result<IndentOptions> {
221234
IndentType::Spaces
222235
},
223236
tab_spaces,
237+
split_line_attributes,
224238
))
225239
}
226240

@@ -269,6 +283,7 @@ async fn test_auto_fmt() {
269283
check: false,
270284
raw: Some(test_rsx),
271285
file: None,
286+
split_line_attributes: false,
272287
};
273288

274289
fmt.autoformat().await.unwrap();

packages/extension/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub fn format_rsx(raw: String, use_tabs: bool, indent_size: usize) -> String {
1515
IndentType::Spaces
1616
},
1717
indent_size,
18+
false,
1819
),
1920
);
2021
block.unwrap()
@@ -32,6 +33,7 @@ pub fn format_selection(raw: String, use_tabs: bool, indent_size: usize) -> Stri
3233
IndentType::Spaces
3334
},
3435
indent_size,
36+
false,
3537
),
3638
);
3739
block.unwrap()
@@ -67,6 +69,7 @@ pub fn format_file(contents: String, use_tabs: bool, indent_size: usize) -> Form
6769
IndentType::Spaces
6870
},
6971
indent_size,
72+
false,
7073
),
7174
);
7275
let out = dioxus_autofmt::apply_formats(&contents, _edits.clone());

0 commit comments

Comments
 (0)