Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing bug in sql formatter where values provided after reserved 'VALUES' keyword were not being indented properly #786

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ private void FormatOpeningParentheses(Token token, ReadOnlySpan<char> querySpan)
{
Token? behindToken = TokenLookBehind();

if (behindToken is not { Type: TokenType.OpenParen or TokenType.LineComment or TokenType.Operator })
if (behindToken is not { Type: TokenType.OpenParen or TokenType.LineComment or TokenType.Operator }
&& !behindToken?.IsValues(querySpan.Slice(behindToken!.Value)) == true)
{
_queryBuilder.TrimSpaceEnd();
}
Expand Down
5 changes: 5 additions & 0 deletions src/dev/impl/DevToys/Helpers/SqlFormatter/Core/TokenHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ internal static bool IsEnd(this Token token, ReadOnlySpan<char> tokenValueSpan)
return IsToken(token.Type, TokenType.CloseParen, tokenValueSpan, "END".AsSpan());
}

internal static bool IsValues(this Token token, ReadOnlySpan<char> tokenValueSpan)
{
return IsToken(token.Type, TokenType.ReservedTopLevel, tokenValueSpan, "VALUES".AsSpan());
}

private static bool IsToken(TokenType type, TokenType otherType,
ReadOnlySpan<char> tokenValueSpan, ReadOnlySpan<char> otherSpan)
{
Expand Down
16 changes: 16 additions & 0 deletions src/tests/DevToys.Tests/Providers/Tools/SqlFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,22 @@ UNION ALL
FROM
tbl2;";
AssertFormat(formatter, input, expectedResult);

// correctly formats hardcoded values in from statement
input =
@"SELECT Id FROM (values(1),(2),(3), (4)) as b (id)";
expectedResult =
@"SELECT
Id
FROM
(
values
(1),
(2),
(3),
(4)
) as b (id)";
AssertFormat(formatter, input, expectedResult);
}

/// <summary>
Expand Down