Skip to content

Commit

Permalink
Fix dot func call chains with comment after dot (#754)
Browse files Browse the repository at this point in the history
* Add test case

* Fix code

* Update snapshot and changelog
  • Loading branch information
JohnnyMorganz authored Sep 2, 2023
1 parent a0d1b5d commit dd97aa3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed LuaJIT suffixes `LL`/`ULL` causing a panic when running in `--verify` mode ([#750](https://github.com/JohnnyMorganz/StyLua/issues/750))
- Fixed incorrect formatting of conditionals when `collapse_simple_statement` is enabled and the block begins with an empty line ([#744](https://github.com/JohnnyMorganz/StyLua/issues/744))
- Fixed formatting of dot function call chains with comment between dot and names ([#747](https://github.com/JohnnyMorganz/StyLua/issues/747))

## [0.18.1] - 2023-07-15

Expand Down
27 changes: 22 additions & 5 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use crate::{
UpdateTrailingTrivia, UpdateTrivia,
},
trivia_util::{
self, contains_comments, trivia_is_newline, CommentSearch, GetLeadingTrivia,
self, contains_comments, prepend_newline_indent, take_leading_comments,
take_trailing_comments, trivia_is_newline, CommentSearch, GetLeadingTrivia,
GetTrailingTrivia,
},
},
Expand Down Expand Up @@ -425,10 +426,26 @@ pub fn format_index(ctx: &Context, index: &Index, shape: Shape) -> Index {
}
}

Index::Dot { dot, name } => Index::Dot {
dot: format_token_reference(ctx, dot, shape),
name: format_token_reference(ctx, name, shape),
},
Index::Dot { dot, name } => {
// If there are any comments in between the dot and name,
// then taken them out and put them before the dot
let (mut dot, mut dot_comments) =
take_trailing_comments(&format_token_reference(ctx, dot, shape));
let (name, name_comments) =
take_leading_comments(&format_token_reference(ctx, name, shape));

dot_comments.extend(name_comments);

if !dot_comments.is_empty() {
dot = prepend_newline_indent(
ctx,
&dot.update_leading_trivia(FormatTriviaType::Append(dot_comments)),
shape,
);
}

Index::Dot { dot, name }
}
other => panic!("unknown node {:?}", other),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,8 @@ pub fn format_function_call(
let mut must_hang = false;
while let Some(suffix) = peekable_suffixes.next() {
must_hang = suffix.has_leading_comments(CommentSearch::All)
// Check for comment placed inside of suffix
|| matches!(suffix, Suffix::Index(Index::Dot { dot, name }) if dot.has_trailing_comments(CommentSearch::All) || name.has_leading_comments(CommentSearch::All))
// Check for a trailing comment (iff there is still a suffix after this)
|| (peekable_suffixes.peek().is_some()
&& suffix.has_trailing_comments(CommentSearch::All));
Expand Down
6 changes: 6 additions & 0 deletions tests/inputs/hang-call-chain-comments-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/747

obj. --
func(). --
func(). --
func()
14 changes: 14 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: tests/tests.rs
expression: format(&contents)
---
-- https://github.com/JohnnyMorganz/StyLua/issues/747

obj
--
.func()
--
.func()
--
.func()

0 comments on commit dd97aa3

Please sign in to comment.