Skip to content

A Swiss Army knife for developers.

Added "One tab" indentation for SQL Formatter (#522)

D0nKarnag3aa9aa933b2bb
diff --git a/src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Formatter.cs b/src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Formatter.cs
index cdba05b50..a85bec005 100644
--- a/src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Formatter.cs
+++ b/src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Formatter.cs
@@ -29,7 +29,7 @@ namespace DevToys.Helpers.SqlFormatter.Core
/// </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>
@@ -38,7 +38,7 @@ namespace DevToys.Helpers.SqlFormatter.Core
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);
@@ -97,7 +97,7 @@ namespace DevToys.Helpers.SqlFormatter.Core
_previousReservedToken = token;
break;
case TokenType.ReservedTopLevelNoIndent:
- FormatTopLvoidReservedWordNoIndent(token, querySpan);
+ FormatTopLevelReservedWordNoIndent(token, querySpan);
_previousReservedToken = token;
break;
case TokenType.ReservedNewLine:
@@ -160,7 +160,7 @@ namespace DevToys.Helpers.SqlFormatter.Core
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();
diff --git a/src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Indentation.cs b/src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Indentation.cs
index 33bc4eb36..3f5d3b198 100644
--- a/src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Indentation.cs
+++ b/src/dev/impl/DevToys/Helpers/SqlFormatter/Core/Indentation.cs
@@ -1,5 +1,6 @@
#nullable enable
+using System;
using System.Collections.Generic;
namespace DevToys.Helpers.SqlFormatter.Core
@@ -23,11 +24,11 @@ namespace DevToys.Helpers.SqlFormatter.Core
}
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>
@@ -35,7 +36,13 @@ namespace DevToys.Helpers.SqlFormatter.Core
/// </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>
diff --git a/src/dev/impl/DevToys/Helpers/SqlFormatter/SqlFormatterOptions.cs b/src/dev/impl/DevToys/Helpers/SqlFormatter/SqlFormatterOptions.cs
index c7778ab26..a17f53a09 100644
--- a/src/dev/impl/DevToys/Helpers/SqlFormatter/SqlFormatterOptions.cs
+++ b/src/dev/impl/DevToys/Helpers/SqlFormatter/SqlFormatterOptions.cs
@@ -6,7 +6,7 @@ namespace DevToys.Helpers.SqlFormatter
{
internal struct SqlFormatterOptions
{
- public int IndentationSize { get; }
+ public Models.Indentation Indentation { get; }
public bool Uppercase { get; }
@@ -15,12 +15,12 @@ namespace DevToys.Helpers.SqlFormatter
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;
diff --git a/src/dev/impl/DevToys/ViewModels/Tools/Formatters/SqlFormatter/SqlFormatterToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/Formatters/SqlFormatter/SqlFormatterToolViewModel.cs
index 82e032b05..3fd412f5b 100644
--- a/src/dev/impl/DevToys/ViewModels/Tools/Formatters/SqlFormatter/SqlFormatterToolViewModel.cs
+++ b/src/dev/impl/DevToys/ViewModels/Tools/Formatters/SqlFormatter/SqlFormatterToolViewModel.cs
@@ -100,7 +100,8 @@ namespace DevToys.ViewModels.Tools.SqlFormatter
/// </summary>
internal IReadOnlyList<IndentationDisplayPair> Indentations = new ObservableCollection<IndentationDisplayPair> {
Models.IndentationDisplayPair.TwoSpaces,
- Models.IndentationDisplayPair.FourSpaces
+ Models.IndentationDisplayPair.FourSpaces,
+ Models.IndentationDisplayPair.OneTab
};
/// <summary>
@@ -170,6 +171,7 @@ namespace DevToys.ViewModels.Tools.SqlFormatter
{
Models.Indentation.TwoSpaces => 2,
Models.Indentation.FourSpaces => 4,
+ Models.Indentation.OneTab => 1,
_ => throw new NotSupportedException(),
};
@@ -178,7 +180,7 @@ namespace DevToys.ViewModels.Tools.SqlFormatter
text,
SqlLanguageMode.Value,
new SqlFormatterOptions(
- indentationSize,
+ IndentationMode.Value,
uppercase: true,
linesBetweenQueries: 2));
diff --git a/src/tests/DevToys.Tests/Providers/Tools/SqlFormatterTests.cs b/src/tests/DevToys.Tests/Providers/Tools/SqlFormatterTests.cs
index 87612cac5..5c921d6ac 100644
--- a/src/tests/DevToys.Tests/Providers/Tools/SqlFormatterTests.cs
+++ b/src/tests/DevToys.Tests/Providers/Tools/SqlFormatterTests.cs
@@ -1247,7 +1247,7 @@ SELECT count(*)
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
@@ -1264,7 +1264,7 @@ SELECT
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
@@ -1280,7 +1280,20 @@ WHERE
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);
}
@@ -1376,7 +1389,7 @@ FROM
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);
}