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

Code cleanup attempt #2 #2946

Merged
merged 11 commits into from
May 11, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private static SyntaxNode ReplaceHeader(Document document, SyntaxNode root, Styl
// Does the copyright element have leading whitespace? If so remove it.
if ((copyrightTriviaIndex.Value > 0) && trivia[copyrightTriviaIndex.Value - 1].IsKind(SyntaxKind.WhitespaceTrivia))
{
copyrightTriviaIndex = copyrightTriviaIndex - 1;
copyrightTriviaIndex--;
trivia = trivia.RemoveAt(copyrightTriviaIndex.Value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,36 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
DocumentationResources.InheritdocCodeFix,
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, root, identifierToken, cancellationToken),
cancellationToken => GetTransformedDocumentAsync(context.Document, root, identifierToken, cancellationToken),
nameof(InheritdocCodeFixProvider)),
diagnostic);
}
}

private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, SyntaxNode root, SyntaxToken identifierToken, CancellationToken cancellationToken)
private static async Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken identifierToken, CancellationToken cancellationToken)
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
switch (identifierToken.Parent.Kind())
{
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.EventDeclaration:
return GetTransformedDocumentForBasePropertyDeclaration(document, diagnostic, root, semanticModel, (BasePropertyDeclarationSyntax)identifierToken.Parent, cancellationToken);
return GetTransformedDocumentForBasePropertyDeclaration(document, root, semanticModel, (BasePropertyDeclarationSyntax)identifierToken.Parent, cancellationToken);

case SyntaxKind.MethodDeclaration:
return GetTransformedDocumentForMethodDeclaration(document, diagnostic, root, semanticModel, (MethodDeclarationSyntax)identifierToken.Parent, cancellationToken);
return GetTransformedDocumentForMethodDeclaration(document, root, semanticModel, (MethodDeclarationSyntax)identifierToken.Parent, cancellationToken);

case SyntaxKind.VariableDeclarator:
return GetTransformedDocumentForEventFieldDeclaration(document, diagnostic, root, semanticModel, (EventFieldDeclarationSyntax)identifierToken.Parent.Parent.Parent, cancellationToken);
return GetTransformedDocumentForEventFieldDeclaration(document, root, semanticModel, (EventFieldDeclarationSyntax)identifierToken.Parent.Parent.Parent, cancellationToken);

case SyntaxKind.IndexerDeclaration:
return GetTransformedDocumentForIndexerDeclaration(document, diagnostic, root, semanticModel, (IndexerDeclarationSyntax)identifierToken.Parent, cancellationToken);
return GetTransformedDocumentForIndexerDeclaration(document, root, semanticModel, (IndexerDeclarationSyntax)identifierToken.Parent, cancellationToken);

default:
return document;
}
}

private static Document GetTransformedDocumentForBasePropertyDeclaration(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, BasePropertyDeclarationSyntax basePropertyDeclaration, CancellationToken cancellationToken)
private static Document GetTransformedDocumentForBasePropertyDeclaration(Document document, SyntaxNode root, SemanticModel semanticModel, BasePropertyDeclarationSyntax basePropertyDeclaration, CancellationToken cancellationToken)
{
if (basePropertyDeclaration.ExplicitInterfaceSpecifier == null && !basePropertyDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
Expand All @@ -130,10 +130,10 @@ private static Document GetTransformedDocumentForBasePropertyDeclaration(Documen
}
}

return InsertInheritdocComment(document, diagnostic, root, basePropertyDeclaration, cancellationToken);
return InsertInheritdocComment(document, root, basePropertyDeclaration, cancellationToken);
}

private static Document GetTransformedDocumentForMethodDeclaration(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
private static Document GetTransformedDocumentForMethodDeclaration(Document document, SyntaxNode root, SemanticModel semanticModel, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
{
if (methodDeclaration.ExplicitInterfaceSpecifier == null && !methodDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
Expand All @@ -144,10 +144,10 @@ private static Document GetTransformedDocumentForMethodDeclaration(Document docu
}
}

return InsertInheritdocComment(document, diagnostic, root, methodDeclaration, cancellationToken);
return InsertInheritdocComment(document, root, methodDeclaration, cancellationToken);
}

private static Document GetTransformedDocumentForEventFieldDeclaration(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, EventFieldDeclarationSyntax eventFieldDeclaration, CancellationToken cancellationToken)
private static Document GetTransformedDocumentForEventFieldDeclaration(Document document, SyntaxNode root, SemanticModel semanticModel, EventFieldDeclarationSyntax eventFieldDeclaration, CancellationToken cancellationToken)
{
if (!eventFieldDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
Expand All @@ -164,10 +164,10 @@ private static Document GetTransformedDocumentForEventFieldDeclaration(Document
}
}

return InsertInheritdocComment(document, diagnostic, root, eventFieldDeclaration, cancellationToken);
return InsertInheritdocComment(document, root, eventFieldDeclaration, cancellationToken);
}

private static Document GetTransformedDocumentForIndexerDeclaration(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, IndexerDeclarationSyntax indexerDeclaration, CancellationToken cancellationToken)
private static Document GetTransformedDocumentForIndexerDeclaration(Document document, SyntaxNode root, SemanticModel semanticModel, IndexerDeclarationSyntax indexerDeclaration, CancellationToken cancellationToken)
{
if (indexerDeclaration.ExplicitInterfaceSpecifier == null && !indexerDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
Expand All @@ -178,11 +178,14 @@ private static Document GetTransformedDocumentForIndexerDeclaration(Document doc
}
}

return InsertInheritdocComment(document, diagnostic, root, indexerDeclaration, cancellationToken);
return InsertInheritdocComment(document, root, indexerDeclaration, cancellationToken);
}

private static Document InsertInheritdocComment(Document document, Diagnostic diagnostic, SyntaxNode root, SyntaxNode syntaxNode, CancellationToken cancellationToken)
private static Document InsertInheritdocComment(Document document, SyntaxNode root, SyntaxNode syntaxNode, CancellationToken cancellationToken)
{
// Currently unused
_ = cancellationToken;

SyntaxTriviaList leadingTrivia = syntaxNode.GetLeadingTrivia();
int insertionIndex = leadingTrivia.Count;
while (insertionIndex > 0 && !leadingTrivia[insertionIndex - 1].HasBuiltinEndLine())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
DocumentationResources.ConstructorDocumentationCodeFix,
cancellationToken => GetConstructorOrDestructorDocumentationTransformedDocumentAsync(context.Document, diagnostic, root, (BaseMethodDeclarationSyntax)identifierToken.Parent, cancellationToken),
cancellationToken => GetConstructorOrDestructorDocumentationTransformedDocumentAsync(context.Document, root, (BaseMethodDeclarationSyntax)identifierToken.Parent, cancellationToken),
nameof(SA1600CodeFixProvider)),
diagnostic);
break;
Expand All @@ -72,7 +72,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
DocumentationResources.MethodDocumentationCodeFix,
cancellationToken => GetMethodDocumentationTransformedDocumentAsync(context.Document, diagnostic, root, semanticModel, (MethodDeclarationSyntax)identifierToken.Parent, cancellationToken),
cancellationToken => GetMethodDocumentationTransformedDocumentAsync(context.Document, root, semanticModel, (MethodDeclarationSyntax)identifierToken.Parent, cancellationToken),
nameof(SA1600CodeFixProvider)),
diagnostic);
}
Expand All @@ -98,7 +98,7 @@ private static bool IsCoveredByInheritDoc(SemanticModel semanticModel, MethodDec
return (declaredSymbol != null) && NamedTypeHelpers.IsImplementingAnInterfaceMember(declaredSymbol);
}

private static Task<Document> GetConstructorOrDestructorDocumentationTransformedDocumentAsync(Document document, Diagnostic diagnostic, SyntaxNode root, BaseMethodDeclarationSyntax declaration, CancellationToken cancellationToken)
private static Task<Document> GetConstructorOrDestructorDocumentationTransformedDocumentAsync(Document document, SyntaxNode root, BaseMethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxTriviaList leadingTrivia = declaration.GetLeadingTrivia();
int insertionIndex = GetInsertionIndex(ref leadingTrivia);
Expand Down Expand Up @@ -136,7 +136,7 @@ private static Task<Document> GetConstructorOrDestructorDocumentationTransformed
return Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(declaration, newElement)));
}

private static Task<Document> GetMethodDocumentationTransformedDocumentAsync(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
private static Task<Document> GetMethodDocumentationTransformedDocumentAsync(Document document, SyntaxNode root, SemanticModel semanticModel, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
{
SyntaxTriviaList leadingTrivia = methodDeclaration.GetLeadingTrivia();
int insertionIndex = GetInsertionIndex(ref leadingTrivia);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private bool TryRemoveSummaryPrefix(ref SyntaxList<XmlNodeSyntax> summaryContent
}

// Find the token containing the prefix, such as "Gets or sets "
SyntaxToken prefixToken = default(SyntaxToken);
SyntaxToken prefixToken = default;
foreach (SyntaxToken textToken in firstText.TextTokens)
{
if (textToken.IsMissing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)

private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

TextChange textChange = new TextChange(new TextSpan(diagnostic.Location.SourceSpan.Start, 1), string.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn

string trailingString = string.Empty;

var newContent = RemoveMalformattedStandardText(node.Content, typeDeclaration.Identifier, standardText[0], standardText[1], ref trailingString);
var newContent = RemoveMalformattedStandardText(node.Content, standardText[0], standardText[1], ref trailingString);

if (newContent.Count == 1 && newContent[0] is XmlTextSyntax xmlText)
{
Expand All @@ -174,8 +174,6 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlEmptyElementSyntax node)
{
var typeDeclaration = node.FirstAncestorOrSelf<BaseTypeDeclarationSyntax>();
var declarationSyntax = node.FirstAncestorOrSelf<BaseMethodDeclarationSyntax>();
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);

TypeParameterListSyntax typeParameterList;
if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
Expand All @@ -194,7 +192,7 @@ private static Task<Document> GetTransformedDocumentAsync(Document document, Syn
return Task.FromResult(newDocument);
}

private static SyntaxList<XmlNodeSyntax> RemoveMalformattedStandardText(SyntaxList<XmlNodeSyntax> content, SyntaxToken identifier, string preText, string postText, ref string trailingString)
private static SyntaxList<XmlNodeSyntax> RemoveMalformattedStandardText(SyntaxList<XmlNodeSyntax> content, string preText, string postText, ref string trailingString)
{
var regex = new Regex(@"^\s*" + Regex.Escape(preText) + "[^ ]+" + Regex.Escape(postText));
var item = content.OfType<XmlTextSyntax>().FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static SyntaxTrivia WithoutFormatting(this SyntaxTrivia trivia)
}
}

return WithoutFormattingImpl(trivia);
return WithoutFormattingImpl(result);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ public static SyntaxNode GetParentDeclaration(SyntaxToken token)
return parent;

default:
var declarationParent = parent as MemberDeclarationSyntax;
if (declarationParent != null)
if (parent is MemberDeclarationSyntax declarationParent)
{
return declarationParent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen

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

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

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

Expand Down Expand Up @@ -284,7 +284,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi

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

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

return syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ private SyntaxNode RegisterLocalFunctionStatementCodeFix(SyntaxNode syntaxRoot,
return this.ReformatElement(syntaxRoot, node, node.Body.OpenBraceToken, node.Body.CloseBraceToken, indentationSettings);
}

private SyntaxNode RegisterEnumDeclarationCodeFix(SyntaxNode syntaxRoot, EnumDeclarationSyntax node, IndentationSettings indentationSettings)
{
return this.ReformatElement(syntaxRoot, node, node.OpenBraceToken, node.CloseBraceToken, indentationSettings);
}

private SyntaxNode RegisterNamespaceDeclarationCodeFix(SyntaxNode syntaxRoot, NamespaceDeclarationSyntax node, IndentationSettings indentationSettings)
{
return this.ReformatElement(syntaxRoot, node, node.OpenBraceToken, node.CloseBraceToken, indentationSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, StatementSyntax node, CancellationToken cancellationToken)
{
// Currently unused
_ = cancellationToken;

var newSyntaxRoot = root.ReplaceNode(node, SyntaxFactory.Block(node));
return Task.FromResult(document.WithSyntaxRoot(newSyntaxRoot));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode syntaxRoot, Diagnostic diagnostic, bool insertBlankLine, CancellationToken cancellationToken)
{
// Currently unused
_ = cancellationToken;

var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
node = GetRelevantNode(node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
nodes.Add(node);
}

return root.ReplaceNodes(nodes, (originalNode, rewrittenNode) => AddParentheses(originalNode, rewrittenNode));
return root.ReplaceNodes(nodes, (originalNode, rewrittenNode) => AddParentheses(rewrittenNode));
}

private static SyntaxNode AddParentheses(SyntaxNode originalNode, SyntaxNode rewrittenNode)
private static SyntaxNode AddParentheses(SyntaxNode node)
{
if (!(rewrittenNode is BinaryExpressionSyntax syntax))
if (!(node is BinaryExpressionSyntax syntax))
{
return rewrittenNode;
return node;
}

BinaryExpressionSyntax trimmedSyntax = syntax.WithoutTrivia();
Expand Down
Loading