mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-29 17:24:34 -05:00
Avoid unnecessary indents for compount boolean expressions (#246)
- Avoid incrementing indents for compound binary boolean expressions. Multiple 'AND x = Y' statements were each indenting instead of having the same indent level. - Fixes https://github.com/Microsoft/vscode-mssql/issues/709
This commit is contained in:
@@ -38,12 +38,15 @@ namespace Microsoft.SqlTools.ServiceLayer.Formatter
|
||||
|
||||
internal override void ProcessPrefixRegion(int startTokenNumber, int firstChildStartTokenNumber)
|
||||
{
|
||||
SpaceSeparatedListFormatter.ProcessPrefixRegion(startTokenNumber, firstChildStartTokenNumber);
|
||||
// Binary boolean expressions
|
||||
bool allowIncrement = !(CodeObject.Parent is SqlBinaryBooleanExpression);
|
||||
SpaceSeparatedListFormatter.ProcessPrefixRegion(startTokenNumber, firstChildStartTokenNumber, allowIncrement);
|
||||
}
|
||||
|
||||
internal override void ProcessSuffixRegion(int lastChildEndTokenNumber, int endTokenNumber)
|
||||
{
|
||||
SpaceSeparatedListFormatter.ProcessSuffixRegion(lastChildEndTokenNumber, endTokenNumber);
|
||||
bool allowDecrement = !(CodeObject.Parent is SqlBinaryBooleanExpression);
|
||||
SpaceSeparatedListFormatter.ProcessSuffixRegion(lastChildEndTokenNumber, endTokenNumber, allowDecrement);
|
||||
}
|
||||
|
||||
internal override void ProcessInterChildRegion(SqlCodeObject previousChild, SqlCodeObject nextChild)
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Formatter
|
||||
/// </summary>
|
||||
internal abstract class WhiteSpaceSeparatedListFormatter : ASTNodeFormatterT<SqlCodeObject>
|
||||
{
|
||||
private bool IncremenetIndentLevelOnPrefixRegion { get; set; }
|
||||
private bool IncrementIndentLevelOnPrefixRegion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// This constructor initalizes the <see cref="Visitor"/> and <see cref="CodeObject"/> properties since the formatter's entry point
|
||||
@@ -25,21 +25,30 @@ namespace Microsoft.SqlTools.ServiceLayer.Formatter
|
||||
internal WhiteSpaceSeparatedListFormatter(FormatterVisitor visitor, SqlCodeObject codeObject, bool incrementIndentLevelOnPrefixRegion)
|
||||
: base(visitor, codeObject)
|
||||
{
|
||||
IncremenetIndentLevelOnPrefixRegion = incrementIndentLevelOnPrefixRegion;
|
||||
IncrementIndentLevelOnPrefixRegion = incrementIndentLevelOnPrefixRegion;
|
||||
}
|
||||
|
||||
internal override void ProcessPrefixRegion(int startTokenNumber, int firstChildStartTokenNumber)
|
||||
internal void ProcessPrefixRegion(int startTokenNumber, int firstChildStartTokenNumber, bool allowIncrement)
|
||||
{
|
||||
if (IncremenetIndentLevelOnPrefixRegion)
|
||||
if (allowIncrement && IncrementIndentLevelOnPrefixRegion)
|
||||
{
|
||||
IncrementIndentLevel();
|
||||
}
|
||||
base.ProcessPrefixRegion(startTokenNumber, firstChildStartTokenNumber);
|
||||
}
|
||||
|
||||
internal override void ProcessPrefixRegion(int startTokenNumber, int firstChildStartTokenNumber)
|
||||
{
|
||||
ProcessPrefixRegion(startTokenNumber, firstChildStartTokenNumber, true);
|
||||
}
|
||||
|
||||
internal override void ProcessSuffixRegion(int lastChildEndTokenNumber, int endTokenNumber)
|
||||
{
|
||||
if (IncremenetIndentLevelOnPrefixRegion)
|
||||
ProcessSuffixRegion(lastChildEndTokenNumber, endTokenNumber, true);
|
||||
}
|
||||
internal void ProcessSuffixRegion(int lastChildEndTokenNumber, int endTokenNumber, bool allowDecrement)
|
||||
{
|
||||
if (allowDecrement && IncrementIndentLevelOnPrefixRegion)
|
||||
{
|
||||
DecrementIndentLevel();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
CREATE PROCEDURE au_info
|
||||
@lastname varchar(40),
|
||||
@firstname varchar(20)
|
||||
AS
|
||||
SELECT au_lname, au_fname, title, pub_name
|
||||
FROM authors a INNER JOIN titleauthor ta
|
||||
ON a.au_id = ta.au_id INNER JOIN titles t
|
||||
ON t.title_id = ta.title_id INNER JOIN publishers p
|
||||
ON t.pub_id = p.pub_id
|
||||
WHERE au_fname = @firstname
|
||||
AND au_lname = @lastname
|
||||
GO
|
||||
@@ -0,0 +1,11 @@
|
||||
select *
|
||||
from myTable
|
||||
where ProcessDate = '20161009'
|
||||
and LoanChargeoffDate is not null --charged-off loans
|
||||
--and LoandChargeoffDate is null and LoanCloseDate IS NOT null
|
||||
and LoanCode IN (0,1) -- open and close ended loans only
|
||||
and LoanBalloonDate IS NULL -- exclude balloon loans
|
||||
and LoanType = 1 -- LoanType NOT IN (10, 51, 97) -- no lines of credit
|
||||
-- and LoanCreditScore > 0
|
||||
and LoanTermMonths > 0
|
||||
order by AddrChanged
|
||||
@@ -0,0 +1,10 @@
|
||||
select * from myTable
|
||||
where ProcessDate = '20161009'
|
||||
and LoanChargeoffDate is not null --charged-off loans
|
||||
--and LoandChargeoffDate is null and LoanCloseDate IS NOT null
|
||||
and LoanCode IN (0,1) -- open and close ended loans only
|
||||
and LoanBalloonDate IS NULL -- exclude balloon loans
|
||||
and LoanType = 1 -- LoanType NOT IN (10, 51, 97) -- no lines of credit
|
||||
-- and LoanCreditScore > 0
|
||||
and LoanTermMonths > 0
|
||||
order by AddrChanged
|
||||
@@ -19,6 +19,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Formatter
|
||||
new FormatOptions() { KeywordCasing = CasingOptions.Uppercase },
|
||||
verifyFormat: true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void KeywordCaseConversionLowercase()
|
||||
{
|
||||
@@ -28,5 +29,25 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Formatter
|
||||
new FormatOptions() { KeywordCasing = CasingOptions.Lowercase },
|
||||
verifyFormat: true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectWithOrderByShouldCorrectlyIndent()
|
||||
{
|
||||
LoadAndFormatAndCompare("SelectWithOrderByShouldCorrectlyIndent",
|
||||
GetInputFile("SelectWithOrderBy.sql"),
|
||||
GetBaselineFile("SelectWithOrderBy_CorrectIndents.sql"),
|
||||
new FormatOptions(),
|
||||
verifyFormat: true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectStatementShouldCorrectlyIndent()
|
||||
{
|
||||
LoadAndFormatAndCompare("SelectStatementShouldCorrectlyIndent",
|
||||
GetInputFile("CreateProcedure.sql"),
|
||||
GetBaselineFile("CreateProcedure_CorrectIndents.sql"),
|
||||
new FormatOptions(),
|
||||
verifyFormat: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user