Skip to content

Commit

Permalink
Merge pull request #118 from LorettaDevs/bugfix/117
Browse files Browse the repository at this point in the history
Fix single line comments not getting line breaks after them when normalizing whitespace
  • Loading branch information
GGG-KILLER authored Jun 24, 2023
2 parents 1be4e12 + 5e1319e commit 8289950
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Fixed
- Fixed `LuaSyntaxOptions.AcceptInvalidEscapes` not suppressing errors in cases where `LuaSyntaxOptions.{AcceptWhitespaceEscape,AcceptHexEscapesInStrings,AcceptUnicodeEscape}` were `false` by @TheGreatSageEqualToHeaven in https://github.com/LorettaDevs/Loretta/pull/116.
- Fixed single line comments not getting a line break added after them in `NormalizeWhitespace` by @GGG-KILLER in https://github.com/LorettaDevs/Loretta/pull/118.

### Removed
- Removed `LanguageNames.{CSharp,FSharp,VisualBasic}` by @GGG-KILLER in https://github.com/LorettaDevs/Loretta/pull/115.
Expand Down
26 changes: 16 additions & 10 deletions src/Compilers/Lua/Portable/Syntax/SyntaxNormalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,11 @@ private SyntaxTriviaList RewriteTrivia(
currentTriviaList.Add(trivia);
}

if (NeedsLineBreakAfter(trivia))
if (NeedsLineBreakAfter(trivia, isTrailing))
{
if (!isTrailing)
{
currentTriviaList.Add(GetEndOfLine());
_afterLineBreak = true;
_afterIndentation = false;
}
currentTriviaList.Add(GetEndOfLine());
_afterLineBreak = true;
_afterIndentation = false;
}
else
{
Expand All @@ -254,7 +251,8 @@ private SyntaxTriviaList RewriteTrivia(
_afterIndentation = false;
}
}
else if (mustHaveSeparator)
else if (mustHaveSeparator
&& (!currentTriviaList.Any() || currentTriviaList.Last().Kind() is not (SyntaxKind.WhitespaceTrivia or SyntaxKind.EndOfLineTrivia)))
{
currentTriviaList.Add(GetSpace());
_afterLineBreak = false;
Expand Down Expand Up @@ -317,8 +315,16 @@ private static bool NeedsLineBreakBetween(SyntaxTrivia trivia, SyntaxTrivia next
};
}

private static bool NeedsLineBreakAfter(SyntaxTrivia trivia) =>
trivia.IsKind(SyntaxKind.SingleLineCommentTrivia);
private static bool NeedsLineBreakAfter(SyntaxTrivia trivia, bool isTrailingTrivia)
{
var kind = trivia.Kind();
return kind switch
{
SyntaxKind.SingleLineCommentTrivia => true,
SyntaxKind.MultiLineCommentTrivia => !isTrailingTrivia,
_ => false
};
}

#pragma warning disable IDE0079 // Remove unnecessary suppression
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Will be used when we have doc comments.")]
Expand Down
73 changes: 73 additions & 0 deletions src/Compilers/Lua/Test/Portable/Syntax/SyntaxNormalizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,79 @@ public void SyntaxNormalizer_CorrectlyInsertsExpressionSpaces()
AssertNormalizeCore(root, "print(1, 2)");
}

[Theory]
[WorkItem(117, "https://github.com/LorettaDevs/Loretta/issues/117")]
[InlineData("""
string_format(
"%s %s",
"test", -- comment here
"test2"
)
""",
"""
string_format("%s %s", "test", -- comment here
"test2")
""")]
[InlineData("""
string_format(
"test", -- comment here
"%s %s",
"test2"
)
""",
"""
string_format("test", -- comment here
"%s %s", "test2")
""")]
[InlineData("""
string_format(
"%s %s",
"test2",
"test" -- comment here
)
""",
"""
string_format("%s %s", "test2", "test" -- comment here
)
""")]
[InlineData("""
string_format(
"%s %s",
"test", --[[ comment here ]]
"test2"
)
""",
"""
string_format("%s %s", "test", --[[ comment here ]] "test2")
""")]
[InlineData("""
string_format(
"test", --[[ comment here ]]
"%s %s",
"test2"
)
""",
"""
string_format("test", --[[ comment here ]] "%s %s", "test2")
""")]
[InlineData("""
string_format(
"%s %s",
"test2",
"test" --[[ comment here ]]
)
""",
"""
string_format("%s %s", "test2", "test" --[[ comment here ]])
""")]
public void SyntaxNormalizer_CorrectlyAddsLineBreaksAfterSingleLineComments(string input, string expected)
{
var tree = ParseAndValidate(input, s_luaParseOptions);
var root = tree.GetRoot();

AssertNormalizeCore(root, expected);
}

#region Class Implementation Details

private static void AssertNormalizeCore(SyntaxNode node, string expected)
Expand Down

0 comments on commit 8289950

Please sign in to comment.