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

[WIP] Implement SA1138: Indent elements correctly #2034

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
6485abc
Implement SA1137 (ElementsShouldHaveTheSameIndentation)
sharwell Jan 5, 2016
196a70e
Add handlers for field declarations and initializer expressions
sharwell Jan 5, 2016
0281a31
Expand SA1137 support for additional syntax kinds
sharwell Jan 5, 2016
0cdf3ec
Add IndentationCodeFixProvider
sharwell Jan 5, 2016
505b920
Add test for block statements and fix bugs
sharwell Jan 5, 2016
20ac696
Add test for switch statements
sharwell Jan 5, 2016
a0967d0
Add tests for initializer expressions
sharwell Jan 5, 2016
b4610a1
Additional tests for valid initializer expressions and fix bugs
sharwell Jan 5, 2016
0b976ba
Add tests for base type declarations and fix bugs
sharwell Jan 5, 2016
f5f7d2d
Add tests for constraint clauses in type declarations
sharwell Jan 5, 2016
38ab7d3
Add edge cases and fix bugs
sharwell Jan 5, 2016
c3ae121
Avoid mutations if the result will match the input
sharwell Jan 5, 2016
b84892b
Implement support for anonymous object creation expressions
sharwell Jan 5, 2016
c19a1fe
Add tests for type parameter lists
sharwell Jan 5, 2016
2e34ba2
Add test for enum declarations
sharwell Jan 5, 2016
26ee139
Add tests for method declarations
sharwell Jan 5, 2016
18d33e4
Test field, event field, and variable declarations
sharwell Jan 5, 2016
b879caf
Add tests for attribute lists
sharwell Jan 5, 2016
e2e7ed3
Add attribute argument list tests
sharwell Jan 5, 2016
c9506cf
Add accessor list tests
sharwell Jan 5, 2016
d94c666
Add parameter list tests
sharwell Jan 5, 2016
59fccc3
Add argument list tests
sharwell Jan 5, 2016
29208d0
Add documentation for SA1137
sharwell Jan 5, 2016
0eef077
Enable SA1137 by default
sharwell Jan 5, 2016
9ac894c
Add a test for documentation comment behavior
sharwell Jan 6, 2016
89d3e31
Add test for members of a type declaration
sharwell Jan 6, 2016
b660fa6
Rename TestBaseTypeDeclarationAsync to TestNamespaceDeclarationAsync
sharwell Jan 6, 2016
990b6bd
For SA1137, first in line means whitespace only
sharwell Jan 14, 2016
aac8844
Initial implementation of SA1138 (Indent elements correctly)
sharwell Jan 7, 2016
5f33d62
Fix indentation handling of base method and property declarations
sharwell Jan 9, 2016
9860c7e
Fix indentation handling for blocks inside else clauses
sharwell Jan 9, 2016
23417d6
Fix handling of curly braces and statements without braces
sharwell Jan 9, 2016
3549add
Fix errors running the SA1137 unit tests
sharwell Jan 9, 2016
4082322
Add tests for SA1138
sharwell Jan 9, 2016
a50fe42
Add tests for additional method and statement kinds
sharwell Jan 10, 2016
b221643
Add tests for assembly attribute lists
sharwell Jan 11, 2016
6ed11d0
Fix indentation of attributes in an attribute list
sharwell Jan 11, 2016
deefc6f
Add tests for control flow statements with expressions
sharwell Jan 11, 2016
2e04003
Expand test cases for nested statements
sharwell Jan 11, 2016
77a1416
Remove unnecessary null checks
sharwell Jan 11, 2016
398f25a
Refactor a bunch of test cases to properties
sharwell Jan 11, 2016
ce0ee05
Check while token of do statement with a non-block body
sharwell Jan 11, 2016
0a08feb
Fix indentation of 'else if'
sharwell Jan 11, 2016
aa57c35
Fix indentation of consecutive using statements
sharwell Jan 11, 2016
2dac7e2
Make sure token alignment considers non-whitespace leading trivia
sharwell Jan 15, 2016
2bd4822
Rename GenerateIndentationString to GenerateIndentationStringForSteps
sharwell Jan 17, 2016
d95082a
Update indentation code fix provider to adjust an entire element
sharwell Jan 17, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace StyleCop.Analyzers.Helpers
{
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Settings.ObjectModel;
Expand Down Expand Up @@ -65,7 +66,7 @@ public static int GetIndentationSteps(IndentationSettings indentationSettings, S
/// <param name="indentationSettings">The indentation settings to use.</param>
/// <param name="indentationSteps">The number of indentation steps.</param>
/// <returns>A string containing the amount of whitespace needed for the given indentation steps.</returns>
public static string GenerateIndentationString(IndentationSettings indentationSettings, int indentationSteps)
public static string GenerateIndentationStringForSteps(IndentationSettings indentationSettings, int indentationSteps)
{
string result;
var indentationCount = indentationSteps * indentationSettings.IndentationSize;
Expand All @@ -83,6 +84,42 @@ public static string GenerateIndentationString(IndentationSettings indentationSe
return result;
}

/// <summary>
/// Generate a new indentation string for the given indentation width.
/// </summary>
/// <param name="indentationSettings">The indentation settings to use.</param>
/// <param name="indentationWidth">The width of the indentation string.</param>
/// <returns>A string containing the whitespace needed to indent code to the specified width.</returns>
public static string GenerateIndentationString(IndentationSettings indentationSettings, int indentationWidth) =>
GenerateIndentationString(indentationSettings, indentationWidth, 0);

/// <summary>
/// Generate a new indentation string for the given indentation width.
/// </summary>
/// <param name="indentationSettings">The indentation settings to use.</param>
/// <param name="indentationWidth">The width of the indentation string.</param>
/// <param name="startColumn">The starting column for the indentation.</param>
/// <returns>A string containing the whitespace needed to indent code to the specified width.</returns>
public static string GenerateIndentationString(IndentationSettings indentationSettings, int indentationWidth, int startColumn)
{
if (!indentationSettings.UseTabs)
{
return new string(' ', indentationWidth);
}

// Adjust the indentation width so a narrower first tab doesn't affect the outcome
indentationWidth += startColumn % indentationSettings.TabSize;

int tabCount = indentationWidth / indentationSettings.TabSize;
int spaceCount = indentationWidth - (tabCount * indentationSettings.TabSize);

StringBuilder builder = StringBuilderPool.Allocate();
builder.EnsureCapacity(tabCount + spaceCount);
builder.Append('\t', tabCount);
builder.Append(' ', spaceCount);
return StringBuilderPool.ReturnAndFree(builder);
}

/// <summary>
/// Generates a whitespace trivia with the requested indentation.
/// </summary>
Expand All @@ -91,7 +128,7 @@ public static string GenerateIndentationString(IndentationSettings indentationSe
/// <returns>A <see cref="SyntaxTrivia"/> containing the indentation whitespace.</returns>
public static SyntaxTrivia GenerateWhitespaceTrivia(IndentationSettings indentationSettings, int indentationSteps)
{
return SyntaxFactory.Whitespace(GenerateIndentationString(indentationSettings, indentationSteps));
return SyntaxFactory.Whitespace(GenerateIndentationStringForSteps(indentationSettings, indentationSteps));
}

private static int GetIndentationSteps(IndentationSettings indentationSettings, SyntaxTree syntaxTree, SyntaxTriviaList leadingTrivia)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private static SyntaxNode ReformatStatementAndParent(Document document, Indentat
if (nextTokenLine == statementCloseLine)
{
var parentIndentationLevel = IndentationHelper.GetIndentationSteps(indentationSettings, GetStatementParent(statement.Parent));
var indentationString = IndentationHelper.GenerateIndentationString(indentationSettings, parentIndentationLevel);
var indentationString = IndentationHelper.GenerateIndentationStringForSteps(indentationSettings, parentIndentationLevel);
newParentNextToken = newParentNextToken.WithLeadingTrivia(SyntaxFactory.Whitespace(indentationString));
}

Expand Down Expand Up @@ -179,8 +179,8 @@ private static BlockSyntax ReformatBlock(Document document, IndentationSettings
break;
}

var indentationString = IndentationHelper.GenerateIndentationString(indentationSettings, parentIndentationLevel);
var statementIndentationString = IndentationHelper.GenerateIndentationString(indentationSettings, parentIndentationLevel + 1);
var indentationString = IndentationHelper.GenerateIndentationStringForSteps(indentationSettings, parentIndentationLevel);
var statementIndentationString = IndentationHelper.GenerateIndentationStringForSteps(indentationSettings, parentIndentationLevel + 1);

var newOpenBraceLeadingTrivia = block.OpenBraceToken.LeadingTrivia
.WithoutTrailingWhitespace()
Expand Down Expand Up @@ -258,7 +258,7 @@ private static StatementSyntax ReformatStatement(Document document, IndentationS
break;
}

var statementIndentationString = IndentationHelper.GenerateIndentationString(indentationSettings, parentIndentationLevel + 1);
var statementIndentationString = IndentationHelper.GenerateIndentationStringForSteps(indentationSettings, parentIndentationLevel + 1);

var newFirstTokenLeadingTrivia = statement.GetFirstToken().LeadingTrivia
.WithoutTrailingWhitespace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ private SyntaxNode ReformatElement(SyntaxNode syntaxRoot, SyntaxNode element, Sy
}

var parentIndentationLevel = IndentationHelper.GetIndentationSteps(indentationSettings, element);
var indentationString = IndentationHelper.GenerateIndentationString(indentationSettings, parentIndentationLevel);
var contentIndentationString = IndentationHelper.GenerateIndentationString(indentationSettings, parentIndentationLevel + 1);
var indentationString = IndentationHelper.GenerateIndentationStringForSteps(indentationSettings, parentIndentationLevel);
var contentIndentationString = IndentationHelper.GenerateIndentationStringForSteps(indentationSettings, parentIndentationLevel + 1);

// reformat opening brace
tokenSubstitutions.Add(openBraceToken, this.FormatBraceToken(openBraceToken, indentationString));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static SyntaxNode MoveMember(SyntaxNode syntaxRoot, MemberDeclarationSyn
if (!memberToMove.HasLeadingTrivia)
{
var targetIndentationLevel = IndentationHelper.GetIndentationSteps(indentationSettings, targetMember);
var indentationString = IndentationHelper.GenerateIndentationString(indentationSettings, targetIndentationLevel);
var indentationString = IndentationHelper.GenerateIndentationStringForSteps(indentationSettings, targetIndentationLevel);
memberToMove = memberToMove.WithLeadingTrivia(SyntaxFactory.Whitespace(indentationString));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
{
var rootNamespace = compilationUnit.Members.OfType<NamespaceDeclarationSyntax>().First();
var indentationLevel = IndentationHelper.GetIndentationSteps(settings.Indentation, rootNamespace);
usingsIndentation = IndentationHelper.GenerateIndentationString(settings.Indentation, indentationLevel + 1);
usingsIndentation = IndentationHelper.GenerateIndentationStringForSteps(settings.Indentation, indentationLevel + 1);
}
else
{
Expand Down Expand Up @@ -203,7 +203,7 @@ private static void BuildReplaceMapForNamespaces(UsingsHelper usingsHelper, Dict
indentationSteps++;
}

var indentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationSteps);
var indentation = IndentationHelper.GenerateIndentationStringForSteps(indentationSettings, indentationSteps);

var modifiedUsings = usingsHelper.GenerateGroupedUsings(usingList, indentation, false, qualifyNames);

Expand Down Expand Up @@ -231,7 +231,7 @@ private static void BuildReplaceMapForConditionalDirectives(UsingsHelper usingsH
indentationSteps++;
}

var indentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationSteps);
var indentation = IndentationHelper.GenerateIndentationStringForSteps(indentationSettings, indentationSteps);

var modifiedUsings = usingsHelper.GenerateGroupedUsings(childSpan, indentation, false, qualifyNames: false);

Expand Down
Loading