diff --git a/CHANGELOG.md b/CHANGELOG.md index fc7b62c4..38a46521 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/formatters/expression.rs b/src/formatters/expression.rs index 5f42aa8a..960a512e 100644 --- a/src/formatters/expression.rs +++ b/src/formatters/expression.rs @@ -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, }, }, @@ -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), } } diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index 440cb3a2..c00818c4 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -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)); diff --git a/tests/inputs/hang-call-chain-comments-2.lua b/tests/inputs/hang-call-chain-comments-2.lua new file mode 100644 index 00000000..47433884 --- /dev/null +++ b/tests/inputs/hang-call-chain-comments-2.lua @@ -0,0 +1,6 @@ +-- https://github.com/JohnnyMorganz/StyLua/issues/747 + +obj. -- +func(). -- +func(). -- +func() diff --git a/tests/snapshots/tests__standard@hang-call-chain-comments-2.lua.snap b/tests/snapshots/tests__standard@hang-call-chain-comments-2.lua.snap new file mode 100644 index 00000000..6aaec50a --- /dev/null +++ b/tests/snapshots/tests__standard@hang-call-chain-comments-2.lua.snap @@ -0,0 +1,14 @@ +--- +source: tests/tests.rs +expression: format(&contents) +--- +-- https://github.com/JohnnyMorganz/StyLua/issues/747 + +obj + -- + .func() + -- + .func() + -- + .func() +