Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into bugfix/3279-handl…
Browse files Browse the repository at this point in the history
…e-optional-named-args-correctly
  • Loading branch information
wdolek committed Jun 20, 2021
2 parents 9348eae + 82c2d90 commit 7e8eb7d
Show file tree
Hide file tree
Showing 70 changed files with 1,717 additions and 349 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ indent_size = 4

# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = false
csharp_using_directive_placement = inside_namespace:none

# Always use "this." and "Me." when applicable; let StyleCop Analyzers provide the warning and fix
dotnet_style_qualification_for_field = true:none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
private static async Task<SyntaxNode> GetTransformedSyntaxRootAsync(Document document, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(cancellationToken);
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(root.SyntaxTree, cancellationToken);

var fileHeader = FileHeaderHelpers.ParseFileHeader(root);
SyntaxNode newSyntaxRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
internal static ImmutableArray<string> GenerateStandardText(Document document, BaseMethodDeclarationSyntax methodDeclaration, BaseTypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
{
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(cancellationToken);
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(methodDeclaration.SyntaxTree, cancellationToken);
var culture = new CultureInfo(settings.DocumentationRules.DocumentationCulture);
var resourceManager = DocumentationResources.ResourceManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var braceToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
var tokenReplacements = GenerateBraceFixes(settings.Indentation, ImmutableArray.Create(braceToken));
var tokenReplacements = GenerateBraceFixes(settings, ImmutableArray.Create(braceToken));

var newSyntaxRoot = syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
return document.WithSyntaxRoot(newSyntaxRoot);
}

private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(IndentationSettings indentationSettings, ImmutableArray<SyntaxToken> braceTokens)
private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(StyleCopSettings settings, ImmutableArray<SyntaxToken> braceTokens)
{
var tokenReplacements = new Dictionary<SyntaxToken, SyntaxToken>();

Expand All @@ -72,7 +72,7 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Indentati
var braceLine = LocationHelpers.GetLineSpan(braceToken).StartLinePosition.Line;
var braceReplacementToken = braceToken;

var indentationSteps = DetermineIndentationSteps(indentationSettings, braceToken);
var indentationSteps = DetermineIndentationSteps(settings.Indentation, braceToken);

var previousToken = braceToken.GetPreviousToken();

Expand Down Expand Up @@ -102,19 +102,23 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Indentati
AddReplacement(tokenReplacements, previousToken, previousToken.WithTrailingTrivia(previousTokenNewTrailingTrivia));
}

braceReplacementToken = braceReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationSettings, indentationSteps));
braceReplacementToken = braceReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, indentationSteps));
}

// Check if we need to apply a fix after the brace. No fix is needed when:
// - The closing brace is followed by a semi-colon or closing paren
// - The closing brace is the last token in the file
// - The closing brace is followed by the while expression of a do/while loop and the
// allowDoWhileOnClosingBrace setting is enabled.
var nextToken = braceToken.GetNextToken();
var nextTokenLine = nextToken.IsKind(SyntaxKind.None) ? -1 : LocationHelpers.GetLineSpan(nextToken).StartLinePosition.Line;
var isMultiDimensionArrayInitializer = braceToken.IsKind(SyntaxKind.OpenBraceToken) && braceToken.Parent.IsKind(SyntaxKind.ArrayInitializerExpression) && braceToken.Parent.Parent.IsKind(SyntaxKind.ArrayInitializerExpression);
var allowDoWhileOnClosingBrace = settings.LayoutRules.AllowDoWhileOnClosingBrace && nextToken.IsKind(SyntaxKind.WhileKeyword) && (braceToken.Parent?.IsKind(SyntaxKind.Block) ?? false) && (braceToken.Parent.Parent?.IsKind(SyntaxKind.DoStatement) ?? false);

if ((nextTokenLine == braceLine) &&
(!braceToken.IsKind(SyntaxKind.CloseBraceToken) || !IsValidFollowingToken(nextToken)) &&
!isMultiDimensionArrayInitializer)
!isMultiDimensionArrayInitializer &&
!allowDoWhileOnClosingBrace)
{
var sharedTrivia = nextToken.LeadingTrivia.WithoutTrailingWhitespace();
var newTrailingTrivia = braceReplacementToken.TrailingTrivia
Expand All @@ -135,7 +139,7 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Indentati
newIndentationSteps = Math.Max(0, newIndentationSteps - 1);
}

AddReplacement(tokenReplacements, nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationSettings, newIndentationSteps)));
AddReplacement(tokenReplacements, nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, newIndentationSteps)));
}

braceReplacementToken = braceReplacementToken.WithTrailingTrivia(newTrailingTrivia);
Expand Down Expand Up @@ -282,9 +286,9 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
.OrderBy(token => token.SpanStart)
.ToImmutableArray();

var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);

var tokenReplacements = GenerateBraceFixes(settings.Indentation, tokens);
var tokenReplacements = GenerateBraceFixes(settings, tokens);

return syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
if (!(syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is StatementSyntax statement))
{
return document;
Expand Down Expand Up @@ -301,8 +301,8 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
}

var tokenReplaceMap = new Dictionary<SyntaxToken, SyntaxToken>();
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);

foreach (var diagnostic in diagnostics.Sort(DiagnosticComparer.Instance))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var newDocument = this.CreateCodeFix(document, settings.Indentation, diagnostic, syntaxRoot);

return newDocument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static bool IsAllowedTrivia(SyntaxTrivia trivia)
private static async Task<Document> GetTransformedDocumentForSingleLineAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);

var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
var accessorList = GetAccessorList(node);
Expand Down Expand Up @@ -188,7 +188,7 @@ private static BlockSyntax ReformatBodyAsSingleLine(BlockSyntax body)
private static async Task<Document> GetTransformedDocumentForMutipleLinesAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);

var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
var accessorList = GetAccessorList(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.LayoutRules
{
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand All @@ -27,7 +28,7 @@ internal class SA1506CodeFixProvider : CodeFixProvider
/// <inheritdoc/>
public override FixAllProvider GetFixAllProvider()
{
return CustomFixAllProviders.BatchFixer;
return FixAll.Instance;
}

/// <inheritdoc/>
Expand All @@ -48,53 +49,72 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)

private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
return document.WithSyntaxRoot(newRoot);
}

var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
var triviaList = token.LeadingTrivia;
private static async Task<SyntaxNode> GetTransformedDocumentAsync(Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
return syntaxRoot.ReplaceTokens(
diagnostics.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start)),
(originalToken, rewrittenToken) =>
{
var triviaList = rewrittenToken.LeadingTrivia;

var index = triviaList.IndexOf(SyntaxKind.SingleLineDocumentationCommentTrivia);
var index = triviaList.IndexOf(SyntaxKind.SingleLineDocumentationCommentTrivia);

int currentLineStart = index + 1;
bool onBlankLine = true;
for (int currentIndex = currentLineStart; currentIndex < triviaList.Count; currentIndex++)
{
switch (triviaList[currentIndex].Kind())
{
case SyntaxKind.EndOfLineTrivia:
if (onBlankLine)
int currentLineStart = index + 1;
bool onBlankLine = true;
for (int currentIndex = currentLineStart; currentIndex < triviaList.Count; currentIndex++)
{
triviaList = triviaList.RemoveRange(currentLineStart, currentIndex - currentLineStart + 1);
currentIndex = currentLineStart - 1;
continue;
}
else
{
currentLineStart = currentIndex + 1;
onBlankLine = true;
break;
}
switch (triviaList[currentIndex].Kind())
{
case SyntaxKind.EndOfLineTrivia:
if (onBlankLine)
{
triviaList = triviaList.RemoveRange(currentLineStart, currentIndex - currentLineStart + 1);
currentIndex = currentLineStart - 1;
continue;
}
else
{
currentLineStart = currentIndex + 1;
onBlankLine = true;
break;
}

case SyntaxKind.WhitespaceTrivia:
break;
case SyntaxKind.WhitespaceTrivia:
break;

default:
if (triviaList[currentIndex].HasBuiltinEndLine())
{
currentLineStart = currentIndex + 1;
onBlankLine = true;
break;
}
else
{
onBlankLine = false;
break;
default:
if (triviaList[currentIndex].HasBuiltinEndLine())
{
currentLineStart = currentIndex + 1;
onBlankLine = true;
break;
}
else
{
onBlankLine = false;
break;
}
}
}
}
}

var newSyntaxRoot = syntaxRoot.ReplaceToken(token, token.WithLeadingTrivia(triviaList));
return document.WithSyntaxRoot(newSyntaxRoot);
return rewrittenToken.WithLeadingTrivia(triviaList);
});
}

private class FixAll : DocumentBasedFixAllProvider
{
public static FixAllProvider Instance { get; } =
new FixAll();

protected override string CodeActionTitle => LayoutResources.SA1506CodeFix;

protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
=> await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
}
}
}
Loading

0 comments on commit 7e8eb7d

Please sign in to comment.