Skip to content

Commit ea1611c

Browse files
feat(config): expose all width heurstic options
1 parent 0269eb1 commit ea1611c

File tree

3 files changed

+151
-22
lines changed

3 files changed

+151
-22
lines changed

Diff for: src/config/config_type.rs

+99-11
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,15 @@ macro_rules! create_config {
9595
pub fn $i(&mut self, value: $ty) {
9696
(self.0).$i.2 = value;
9797
match stringify!($i) {
98-
"max_width" | "use_small_heuristics" => self.0.set_heuristics(),
98+
"max_width"
99+
| "use_small_heuristics"
100+
| "fn_call_width"
101+
| "single_line_if_else_max_width"
102+
| "attr_fn_like_width"
103+
| "struct_lit_width"
104+
| "struct_variant_width"
105+
| "array_width"
106+
| "chain_width" => self.0.set_heuristics(),
99107
"license_template_path" => self.0.set_license_template(),
100108
"merge_imports" => self.0.set_merge_imports(),
101109
&_ => (),
@@ -230,7 +238,15 @@ macro_rules! create_config {
230238
}
231239

232240
match key {
233-
"max_width" | "use_small_heuristics" => self.set_heuristics(),
241+
"max_width"
242+
| "use_small_heuristics"
243+
| "fn_call_width"
244+
| "single_line_if_else_max_width"
245+
| "attr_fn_like_width"
246+
| "struct_lit_width"
247+
| "struct_variant_width"
248+
| "array_width"
249+
| "chain_width" => self.set_heuristics(),
234250
"license_template_path" => self.set_license_template(),
235251
"merge_imports" => self.set_merge_imports(),
236252
&_ => (),
@@ -281,16 +297,88 @@ macro_rules! create_config {
281297
)+
282298
}
283299

300+
fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) {
301+
let max_width = self.max_width.2;
302+
let get_width_value = |
303+
was_set: bool,
304+
override_value: usize,
305+
heuristic_value: usize,
306+
config_key: &str,
307+
| -> usize {
308+
if !was_set {
309+
return heuristic_value;
310+
}
311+
if override_value > max_width {
312+
panic!("`{}` cannot have a value that exceeds `max_width`", config_key);
313+
}
314+
override_value
315+
};
316+
317+
let fn_call_width = get_width_value(
318+
self.was_set().fn_call_width(),
319+
self.fn_call_width.2,
320+
heuristics.fn_call_width,
321+
"fn_call_width",
322+
);
323+
self.fn_call_width.2 = fn_call_width;
324+
325+
let attr_fn_like_width = get_width_value(
326+
self.was_set().attr_fn_like_width(),
327+
self.attr_fn_like_width.2,
328+
heuristics.attr_fn_like_width,
329+
"attr_fn_like_width",
330+
);
331+
self.attr_fn_like_width.2 = attr_fn_like_width;
332+
333+
let struct_lit_width = get_width_value(
334+
self.was_set().struct_lit_width(),
335+
self.struct_lit_width.2,
336+
heuristics.struct_lit_width,
337+
"struct_lit_width",
338+
);
339+
self.struct_lit_width.2 = struct_lit_width;
340+
341+
let struct_variant_width = get_width_value(
342+
self.was_set().struct_variant_width(),
343+
self.struct_variant_width.2,
344+
heuristics.struct_variant_width,
345+
"struct_variant_width",
346+
);
347+
self.struct_variant_width.2 = struct_variant_width;
348+
349+
let array_width = get_width_value(
350+
self.was_set().array_width(),
351+
self.array_width.2,
352+
heuristics.array_width,
353+
"array_width",
354+
);
355+
self.array_width.2 = array_width;
356+
357+
let chain_width = get_width_value(
358+
self.was_set().chain_width(),
359+
self.chain_width.2,
360+
heuristics.chain_width,
361+
"chain_width",
362+
);
363+
self.chain_width.2 = chain_width;
364+
365+
let single_line_if_else_max_width = get_width_value(
366+
self.was_set().single_line_if_else_max_width(),
367+
self.single_line_if_else_max_width.2,
368+
heuristics.single_line_if_else_max_width,
369+
"single_line_if_else_max_width",
370+
);
371+
self.single_line_if_else_max_width.2 = single_line_if_else_max_width;
372+
}
373+
284374
fn set_heuristics(&mut self) {
285-
if self.use_small_heuristics.2 == Heuristics::Default {
286-
let max_width = self.max_width.2;
287-
self.set().width_heuristics(WidthHeuristics::scaled(max_width));
288-
} else if self.use_small_heuristics.2 == Heuristics::Max {
289-
let max_width = self.max_width.2;
290-
self.set().width_heuristics(WidthHeuristics::set(max_width));
291-
} else {
292-
self.set().width_heuristics(WidthHeuristics::null());
293-
}
375+
let max_width = self.max_width.2;
376+
match self.use_small_heuristics.2 {
377+
Heuristics::Default =>
378+
self.set_width_heuristics(WidthHeuristics::scaled(max_width)),
379+
Heuristics::Max => self.set_width_heuristics(WidthHeuristics::set(max_width)),
380+
Heuristics::Off => self.set_width_heuristics(WidthHeuristics::null()),
381+
};
294382
}
295383

296384
fn set_license_template(&mut self) {

Diff for: src/config/mod.rs

+48-9
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,26 @@ create_config! {
3535
hard_tabs: bool, false, true, "Use tab characters for indentation, spaces for alignment";
3636
tab_spaces: usize, 4, true, "Number of spaces per tab";
3737
newline_style: NewlineStyle, NewlineStyle::Auto, true, "Unix or Windows line endings";
38+
indent_style: IndentStyle, IndentStyle::Block, false, "How do we indent expressions or items";
39+
40+
// Width Heuristics
3841
use_small_heuristics: Heuristics, Heuristics::Default, true, "Whether to use different \
3942
formatting for items and expressions if they satisfy a heuristic notion of 'small'";
40-
indent_style: IndentStyle, IndentStyle::Block, false, "How do we indent expressions or items";
43+
width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false,
44+
"'small' heuristic values";
45+
fn_call_width: usize, 60, true, "Maximum width of the args of a function call before \
46+
falling back to vertical formatting.";
47+
attr_fn_like_width: usize, 70, true, "Maximum width of the args of a function-like \
48+
attributes before falling back to vertical formatting.";
49+
struct_lit_width: usize, 18, true, "Maximum width in the body of a struct lit before \
50+
falling back to vertical formatting.";
51+
struct_variant_width: usize, 35, true, "Maximum width in the body of a struct variant before \
52+
falling back to vertical formatting.";
53+
array_width: usize, 60, true, "Maximum width of an array literal before falling \
54+
back to vertical formatting.";
55+
chain_width: usize, 60, true, "Maximum length of a chain to fit on a single line.";
56+
single_line_if_else_max_width: usize, 50, true, "Maximum line length for single line if-else \
57+
expressions. A value of zero means always break if-else expressions.";
4158

4259
// Comments. macros, and strings
4360
wrap_comments: bool, false, false, "Break comments to fit on the line";
@@ -154,8 +171,6 @@ create_config! {
154171
file_lines: FileLines, FileLines::all(), false,
155172
"Lines to format; this is not supported in rustfmt.toml, and can only be specified \
156173
via the --file-lines option";
157-
width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false,
158-
"'small' heuristic values";
159174
emit_mode: EmitMode, EmitMode::Files, false,
160175
"What emit Mode to use when none is supplied";
161176
make_backup: bool, false, false, "Backup changed files";
@@ -394,9 +409,6 @@ mod test {
394409
create_config! {
395410
// Options that are used by the generated functions
396411
max_width: usize, 100, true, "Maximum width of each line";
397-
use_small_heuristics: Heuristics, Heuristics::Default, true,
398-
"Whether to use different formatting for items and \
399-
expressions if they satisfy a heuristic notion of 'small'.";
400412
license_template_path: String, String::default(), false,
401413
"Beginning of file must match license template";
402414
required_version: String, env!("CARGO_PKG_VERSION").to_owned(), false,
@@ -408,13 +420,33 @@ mod test {
408420
file_lines: FileLines, FileLines::all(), false,
409421
"Lines to format; this is not supported in rustfmt.toml, and can only be specified \
410422
via the --file-lines option";
411-
width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false,
412-
"'small' heuristic values";
423+
413424
// merge_imports deprecation
414425
imports_granularity: ImportGranularity, ImportGranularity::Preserve, false,
415426
"Merge imports";
416427
merge_imports: bool, false, false, "(deprecated: use imports_granularity instead)";
417428

429+
// Width Heuristics
430+
use_small_heuristics: Heuristics, Heuristics::Default, true,
431+
"Whether to use different formatting for items and \
432+
expressions if they satisfy a heuristic notion of 'small'.";
433+
width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false,
434+
"'small' heuristic values";
435+
436+
fn_call_width: usize, 60, true, "Maximum width of the args of a function call before \
437+
falling back to vertical formatting.";
438+
attr_fn_like_width: usize, 70, true, "Maximum width of the args of a function-like \
439+
attributes before falling back to vertical formatting.";
440+
struct_lit_width: usize, 18, true, "Maximum width in the body of a struct lit before \
441+
falling back to vertical formatting.";
442+
struct_variant_width: usize, 35, true, "Maximum width in the body of a struct \
443+
variant before falling back to vertical formatting.";
444+
array_width: usize, 60, true, "Maximum width of an array literal before falling \
445+
back to vertical formatting.";
446+
chain_width: usize, 60, true, "Maximum length of a chain to fit on a single line.";
447+
single_line_if_else_max_width: usize, 50, true, "Maximum line length for single \
448+
line if-else expressions. A value of zero means always break if-else expressions.";
449+
418450
// Options that are used by the tests
419451
stable_option: bool, false, true, "A stable option";
420452
unstable_option: bool, false, false, "An unstable option";
@@ -519,8 +551,15 @@ mod test {
519551
hard_tabs = false
520552
tab_spaces = 4
521553
newline_style = "Auto"
522-
use_small_heuristics = "Default"
523554
indent_style = "Block"
555+
use_small_heuristics = "Default"
556+
fn_call_width = 60
557+
attr_fn_like_width = 70
558+
struct_lit_width = 18
559+
struct_variant_width = 35
560+
array_width = 60
561+
chain_width = 60
562+
single_line_if_else_max_width = 50
524563
wrap_comments = false
525564
format_code_in_doc_comments = false
526565
comment_width = 80

Diff for: src/config/options.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ pub enum TypeDensity {
7979
}
8080

8181
#[config_type]
82-
/// To what extent does rustfmt pursue its heuristics?
82+
/// Heuristic settings that can be used to simply
83+
/// the configuration of the granular width configurations
84+
/// like `struct_lit_width`, `array_width`, etc.
8385
pub enum Heuristics {
8486
/// Turn off any heuristics
8587
Off,
8688
/// Turn on max heuristics
8789
Max,
88-
/// Use Rustfmt's defaults
90+
/// Use scaled values based on the value of `max_width`
8991
Default,
9092
}
9193

0 commit comments

Comments
 (0)