Skip to content

Commit 7d2b155

Browse files
committed
Fixes adjustment of max_width within macros
1 parent 6ac1fcd commit 7d2b155

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/config/config_type.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,63 @@ macro_rules! create_config {
423423
)+
424424
}
425425

426+
/// Reduces the maximum width of the configuration.
427+
///
428+
/// This method is intended to be used when creating a forked
429+
/// configuration for a particular formatting context in which the
430+
/// total available width needs to be reduced. This reduces the
431+
/// size of max_width, and all other properties affected by small
432+
/// heuristics, without treating those adjustments as overrides.
433+
///
434+
/// As an example use case, this method is used when formatting
435+
/// arms within a declarative macro.
436+
#[allow(unreachable_pub)]
437+
pub fn reduce_max_width(&mut self, delta: usize) {
438+
self.adjust_max_width(-1 * delta as isize);
439+
}
440+
441+
/// Increases the maximum width of the configuration.
442+
///
443+
/// This method is intended to be used when creating a forked
444+
/// configuration for a particular formatting context in which the
445+
/// total available width needs to be reduced. This reduces the
446+
/// size of max_width, and all other properties affected by small
447+
/// heuristics, without treating those adjustments as overrides.
448+
///
449+
/// As an example use case, this method is used when formatting
450+
/// arms within a declarative macro.
451+
#[allow(unreachable_pub)]
452+
pub fn increase_max_width(&mut self, delta: usize) {
453+
self.adjust_max_width(delta as isize);
454+
}
455+
456+
/// Adjusts the maximum width of the configuration.
457+
///
458+
/// This method is intended to be used when creating a forked
459+
/// configuration for a particular formatting context in which the
460+
/// total available width needs to be reduced. This reduces the
461+
/// size of max_width, and all other properties affected by small
462+
/// heuristics, without treating those adjustments as overrides.
463+
///
464+
/// As an example use case, this method is used when formatting
465+
/// arms within a declarative macro.
466+
fn adjust_max_width(&mut self, delta: isize) {
467+
let adjust = |value: usize| (value as isize + delta) as usize;
468+
469+
self.array_width.2 = adjust(self.array_width.2);
470+
self.attr_fn_like_width.2 = adjust(self.attr_fn_like_width.2);
471+
self.chain_width.2 = adjust(self.chain_width.2);
472+
self.fn_call_width.2 = adjust(self.fn_call_width.2);
473+
self.single_line_if_else_max_width.2 = adjust(self.single_line_if_else_max_width.2);
474+
self.single_line_let_else_max_width.2 =
475+
adjust(self.single_line_let_else_max_width.2);
476+
self.struct_lit_width.2 = adjust(self.struct_lit_width.2);
477+
self.struct_variant_width.2 = adjust(self.struct_variant_width.2);
478+
479+
self.max_width.2 = adjust(self.max_width.2);
480+
self.set_heuristics();
481+
}
482+
426483
fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) {
427484
let max_width = self.max_width.2;
428485
let get_width_value = |

src/macros.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,15 +1321,13 @@ impl MacroBranch {
13211321
} else {
13221322
shape.indent.block_indent(&config)
13231323
};
1324-
let new_width = config.max_width() - body_indent.width();
1325-
config.set().max_width(new_width);
1324+
config.reduce_max_width(body_indent.width());
13261325

13271326
// First try to format as items, then as statements.
13281327
let new_body_snippet = match crate::format_snippet(&body_str, &config, true) {
13291328
Some(new_body) => new_body,
13301329
None => {
1331-
let new_width = new_width + config.tab_spaces();
1332-
config.set().max_width(new_width);
1330+
config.increase_max_width(config.tab_spaces());
13331331
match crate::format_code_block(&body_str, &config, true) {
13341332
Some(new_body) => new_body,
13351333
None => {

0 commit comments

Comments
 (0)