Skip to content

Commit

Permalink
Use simple heuristics to reduce expontential blowup
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Jun 26, 2022
1 parent c46b521 commit 4dfcd0e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [**Luau**] Increased the shape size of the expression in a type assertion so that it will correctly hang if over width ([#466](https://github.com/JohnnyMorganz/StyLua/issues/466))
- Fixed binary expression in a table field containing a comment being collapsed leading to malformed formatted ([#471](https://github.com/JohnnyMorganz/StyLua/issues/471))
- Fixed end parentheses of a function call with a multiline comment internally being expanded onto a new line unnecessarily ([#473](https://github.com/JohnnyMorganz/StyLua/issues/473))
- Fixed severe performance regression with complex nested function calls ([#477](https://github.com/JohnnyMorganz/StyLua/issues/477))

## [0.13.1] - 2022-04-11
### Fixed
Expand Down
14 changes: 11 additions & 3 deletions src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,17 @@ fn function_args_multiline_heuristic(

// Format all the arguments on an infinite width, so that we can prepare them and check to see whether they
// need expanding. We will ignore punctuation for now
let first_iter_formatted_arguments = arguments
.iter()
.map(|argument| format_expression(ctx, argument, shape.with_infinite_width()));
let first_iter_formatted_arguments = arguments.iter().map(|argument| {
if shape.using_simple_heuristics() {
argument.to_owned()
} else {
format_expression(
ctx,
argument,
shape.with_simple_heuristics().with_infinite_width(),
)
}
});

// Apply some heuristics to determine whether we should expand the function call
let mut singleline_shape = shape + PAREN_LEN;
Expand Down
17 changes: 17 additions & 0 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub struct Shape {
offset: usize,
/// The maximum number of characters we want to fit on a line. This is inferred from the configuration
column_width: usize,
/// Whether we should use simple heuristic checking.
/// This is enabled when we are calling within a heuristic itself, to reduce the exponential blowup
simple_heuristics: bool,
}

impl Shape {
Expand All @@ -111,6 +114,7 @@ impl Shape {
indent: Indent::new(ctx),
offset: 0,
column_width: ctx.config().column_width,
simple_heuristics: false,
}
}

Expand Down Expand Up @@ -179,6 +183,19 @@ impl Shape {
}
}

/// Whether simple heuristics should be used when calculating formatting shape
/// This is to reduce the expontential blowup of discarded test formatting
pub fn using_simple_heuristics(&self) -> bool {
self.simple_heuristics
}

pub fn with_simple_heuristics(&self) -> Shape {
Self {
simple_heuristics: true,
..*self
}
}

/// Resets the offset for the shape
pub fn reset(&self) -> Shape {
Self { offset: 0, ..*self }
Expand Down

0 comments on commit 4dfcd0e

Please sign in to comment.