Skip to content

Commit

Permalink
Fix collapsing when varexpr prefix has trailing comments (#511)
Browse files Browse the repository at this point in the history
* Add test case

* Check for trailing comments on varexpr prefix for hang

* Update snapshot

* Update changelog
  • Loading branch information
JohnnyMorganz authored Jul 26, 2022
1 parent 4f8afdf commit 51fdff4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Fixed var expression with trailing comments on initial prefix being collapsed leading to malformed formatting ([#509](https://github.com/JohnnyMorganz/StyLua/issues/509))

## [0.14.1] - 2022-07-21
### Changed
- Chained var expression formatting will now follow the exact same steps as chained function call formatting
Expand Down
5 changes: 4 additions & 1 deletion src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,10 @@ pub fn format_function_call(
let num_suffixes = function_call.suffixes().count();

// If there are comments within the chain, then we must hang the chain otherwise it can lead to an issue
let must_hang = {
let must_hang = trivia_util::trivia_contains_comments(
trivia_util::prefix_trailing_trivia(function_call.prefix()).iter(),
trivia_util::CommentSearch::Single,
) || {
let mut peekable_suffixes = function_call.suffixes().peekable();
let mut must_hang = false;
while let Some(suffix) = peekable_suffixes.next() {
Expand Down
8 changes: 8 additions & 0 deletions src/formatters/trivia_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ fn function_args_trailing_trivia(function_args: &FunctionArgs) -> Vec<Token> {
}
}

pub fn prefix_trailing_trivia(prefix: &Prefix) -> Vec<Token> {
match prefix {
Prefix::Name(name) => name.trailing_trivia().cloned().collect(),
Prefix::Expression(expression) => get_expression_trailing_trivia(expression),
other => panic!("unknown node {:?}", other),
}
}

pub fn suffix_leading_trivia(suffix: &Suffix) -> impl Iterator<Item = &Token> {
match suffix {
Suffix::Index(index) => match index {
Expand Down
3 changes: 3 additions & 0 deletions tests/inputs/var-expression-comments-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/509
local foo = bar -- comment after bar
.fizz -- comment after fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/tests.rs
expression: format(&contents)
---
-- https://github.com/JohnnyMorganz/StyLua/issues/509
local foo = bar -- comment after bar
.fizz -- comment after fizz

0 comments on commit 51fdff4

Please sign in to comment.