Skip to content
Prev Previous commit
Next Next commit
Use cheaper expression inline comment detection
  • Loading branch information
JohnnyMorganz committed Sep 23, 2022
commit 7e41915acad92fc332317d5c1ed1cc31aff55bf1
6 changes: 1 addition & 5 deletions src/formatters/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,7 @@ pub fn format_assignment_no_trivia(
// Check if the assignment expressions or equal token contain comments. If they do, we bail out of determining any tactics
// and format multiline
let contains_comments = trivia_util::token_contains_comments(assignment.equal_token())
|| assignment.expressions().pairs().any(|pair| {
pair.punctuation()
.map_or(false, trivia_util::token_contains_comments)
|| trivia_util::expression_contains_inline_comments(pair.value())
});
|| trivia_util::punctuated_expression_inline_comments(assignment.expressions());

// Firstly attempt to format the assignment onto a single line, using an infinite column width shape
let mut var_list = try_format_punctuated(
Expand Down
5 changes: 1 addition & 4 deletions src/formatters/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ pub fn format_return(ctx: &Context, return_node: &Return, shape: Shape) -> Retur
);

let contains_comments = return_token_trailing_comments
|| (returns.len() > 1
&& trivia_util::contains_comments(
returns.update_trailing_trivia(FormatTriviaType::Replace(Vec::new())), // We can ignore trailing trivia, as that won't affect anything
));
|| trivia_util::punctuated_expression_inline_comments(returns);

// See if we need to format multiline
// If we contain comments, we immediately force multiline, and return an empty Punctuated sequence as a placeholder (it will never be used)
Expand Down
41 changes: 40 additions & 1 deletion src/formatters/trivia_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,46 @@ pub fn table_field_trailing_trivia(field: &Field) -> Vec<Token> {
// This can only happen if the expression is a BinOp
// We should ignore any comments which are trailing for the whole expression, as they are not inline
pub fn expression_contains_inline_comments(expression: &Expression) -> bool {
contains_comments(expression.update_trailing_trivia(FormatTriviaType::Replace(vec![])))
match expression {
Expression::BinaryOperator { lhs, binop, rhs } => {
contains_comments(binop)
|| contains_comments(lhs)
|| expression_contains_inline_comments(rhs)
}
Expression::UnaryOperator { unop, expression } => {
let op_contains_comments = match unop {
UnOp::Minus(token) | UnOp::Not(token) | UnOp::Hash(token) => {
contains_comments(token)
}
#[cfg(feature = "lua53")]
UnOp::Tilde(token) => contains_comments(token),
other => panic!("unknown node {:?}", other),
};
op_contains_comments || expression_contains_inline_comments(expression)
}
Expression::Parentheses {
contained,
expression,
} => {
token_contains_trailing_comments(contained.tokens().0)
|| token_contains_leading_comments(contained.tokens().1)
|| contains_comments(expression)
}
Expression::Value { value, .. } => match &**value {
Value::ParenthesesExpression(expression) => {
expression_contains_inline_comments(expression)
}
_ => false,
},
_ => false,
}
}

pub fn punctuated_expression_inline_comments(punctuated: &Punctuated<Expression>) -> bool {
punctuated.pairs().any(|pair| {
pair.punctuation().map_or(false, token_contains_comments)
|| expression_contains_inline_comments(pair.value())
})
}

// Commonly, we update trivia to add in a newline and indent trivia to the leading trivia of a token/node.
Expand Down