Skip to content

Commit

Permalink
Added "One tab" indentation for SQL Formatter (#522)
Browse files Browse the repository at this point in the history
* Updated SQL Formatter to support OneTab indentation

* Fixed typo in method name

* Updated unit test
  • Loading branch information
D0nKarnag3 authored May 14, 2022
1 parent 51fbc0c commit aa9aa93
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal abstract class Formatter
/// </summary>
internal string Format(string query)
{
return Format(query, new SqlFormatterOptions(indentationSize: 2, uppercase: false));
return Format(query, new SqlFormatterOptions(indentation: Models.Indentation.TwoSpaces, uppercase: false));
}

/// <summary>
Expand All @@ -38,7 +38,7 @@ internal string Format(string query)
internal string Format(string query, SqlFormatterOptions options)
{
_options = options;
_indentation = new Indentation(options.IndentationSize);
_indentation = new Indentation(options.Indentation);
_params = new Params(options.PlaceholderParameters);

_tokens = GetTokenizer().Tokenize(query);
Expand Down Expand Up @@ -97,7 +97,7 @@ private void SetFormattedQueryFromTokens(ReadOnlySpan<char> querySpan)
_previousReservedToken = token;
break;
case TokenType.ReservedTopLevelNoIndent:
FormatTopLvoidReservedWordNoIndent(token, querySpan);
FormatTopLevelReservedWordNoIndent(token, querySpan);
_previousReservedToken = token;
break;
case TokenType.ReservedNewLine:
Expand Down Expand Up @@ -160,7 +160,7 @@ private string IndentComment(ReadOnlySpan<char> comment)
return CommentWhitespacesRegex.Replace(comment.ToString(), $"\n{_indentation!.GetIndent()} ");
}

private void FormatTopLvoidReservedWordNoIndent(Token token, ReadOnlySpan<char> querySpan)
private void FormatTopLevelReservedWordNoIndent(Token token, ReadOnlySpan<char> querySpan)
{
Assumes.NotNull(_indentation, nameof(_indentation));
_indentation!.DecreaseTopLevel();
Expand Down
15 changes: 11 additions & 4 deletions src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Indentation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable

using System;
using System.Collections.Generic;

namespace DevToys.Helpers.SqlFormatter.Core
Expand All @@ -23,19 +24,25 @@ private enum IndentationType
}

private readonly Stack<IndentationType> _indentationTypes = new();
private readonly int _indentationSize;
private readonly Models.Indentation _indentation;

public Indentation(int indentationSize)
public Indentation(Models.Indentation indentation)
{
_indentationSize = indentationSize;
_indentation = indentation;
}

/// <summary>
/// Returns current indentation string.
/// </summary>
internal string GetIndent()
{
return new string(' ', _indentationSize * _indentationTypes.Count);
return _indentation switch
{
Models.Indentation.TwoSpaces => new string(' ', 2 * _indentationTypes.Count),
Models.Indentation.FourSpaces => new string(' ', 4 * _indentationTypes.Count),
Models.Indentation.OneTab => new string('\t', 1 * _indentationTypes.Count),
_ => throw new NotSupportedException(),
};
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace DevToys.Helpers.SqlFormatter
{
internal struct SqlFormatterOptions
{
public int IndentationSize { get; }
public Models.Indentation Indentation { get; }

public bool Uppercase { get; }

Expand All @@ -15,12 +15,12 @@ internal struct SqlFormatterOptions
public IReadOnlyDictionary<string, string>? PlaceholderParameters { get; }

public SqlFormatterOptions(
int indentationSize,
Models.Indentation indentation,
bool uppercase,
int linesBetweenQueries = 1,
IReadOnlyDictionary<string, string>? placeholderParameters = null)
{
IndentationSize = indentationSize;
Indentation = indentation;
Uppercase = uppercase;
LinesBetweenQueries = linesBetweenQueries;
PlaceholderParameters = placeholderParameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ internal SqlLanguageDisplayPair SqlLanguageMode
/// </summary>
internal IReadOnlyList<IndentationDisplayPair> Indentations = new ObservableCollection<IndentationDisplayPair> {
Models.IndentationDisplayPair.TwoSpaces,
Models.IndentationDisplayPair.FourSpaces
Models.IndentationDisplayPair.FourSpaces,
Models.IndentationDisplayPair.OneTab
};

/// <summary>
Expand Down Expand Up @@ -170,6 +171,7 @@ private async Task TreatQueueAsync()
{
Models.Indentation.TwoSpaces => 2,
Models.Indentation.FourSpaces => 4,
Models.Indentation.OneTab => 1,
_ => throw new NotSupportedException(),
};

Expand All @@ -178,7 +180,7 @@ private async Task TreatQueueAsync()
text,
SqlLanguageMode.Value,
new SqlFormatterOptions(
indentationSize,
IndentationMode.Value,
uppercase: true,
linesBetweenQueries: 2));

Expand Down
21 changes: 17 additions & 4 deletions src/tests/DevToys.Tests/Providers/Tools/SqlFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ private void SupportsConfigOptions(Formatter formatter)
FROM
Table1;";

string output = formatter.Format(input, new SqlFormatterOptions(indentationSize: 4, uppercase: false));
string output = formatter.Format(input, new SqlFormatterOptions(indentation: Models.Indentation.FourSpaces, uppercase: false));
Assert.AreEqual(expectedResult, output);

// supports linesBetweenQueries option
Expand All @@ -1264,7 +1264,7 @@ private void SupportsConfigOptions(Formatter formatter)
FROM
bar;";

output = formatter.Format(input, new SqlFormatterOptions(indentationSize: 2, uppercase: false, linesBetweenQueries: 2));
output = formatter.Format(input, new SqlFormatterOptions(indentation: Models.Indentation.TwoSpaces, uppercase: false, linesBetweenQueries: 2));
Assert.AreEqual(expectedResult, output);

// supports uppercase option
Expand All @@ -1280,7 +1280,20 @@ LEFT JOIN bar
cola > 1
AND colb = 3";

output = formatter.Format(input, new SqlFormatterOptions(indentationSize: 2, uppercase: true));
output = formatter.Format(input, new SqlFormatterOptions(indentation: Models.Indentation.TwoSpaces, uppercase: true));
Assert.AreEqual(expectedResult, output);

// supports indent option one tab
input = @"SELECT count(*),Column1 FROM Table1;";

expectedResult =
string.Format(@"SELECT
{0}count(*),
{1}Column1
FROM
{2}Table1;", "\t", "\t", "\t");

output = formatter.Format(input, new SqlFormatterOptions(indentation: Models.Indentation.OneTab, uppercase: false));
Assert.AreEqual(expectedResult, output);
}

Expand Down Expand Up @@ -1376,7 +1389,7 @@ ELSE 3
WHEN 'three' THEN 3
ELSE 4
END;";
string output = formatter.Format(input, new SqlFormatterOptions(indentationSize: 2, uppercase: true));
string output = formatter.Format(input, new SqlFormatterOptions(indentation: Models.Indentation.TwoSpaces, uppercase: true));
Assert.AreEqual(expectedResult, output);
}

Expand Down

0 comments on commit aa9aa93

Please sign in to comment.