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 @@ -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 @@ -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 Down Expand Up @@ -192,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 @@ -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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -146,27 +146,27 @@ private static void BuildDirectiveTriviaMaps(CompilationUnitSyntax compilationUn
switch (directiveTrivia.Kind())
{
case SyntaxKind.IfDirectiveTrivia:
AddNewDirectiveTriviaSpan(conditionalBuilder, conditionalStack, directiveTrivia);
AddNewDirectiveTriviaSpan(conditionalStack, directiveTrivia);
break;

case SyntaxKind.ElifDirectiveTrivia:
case SyntaxKind.ElseDirectiveTrivia:
var previousSpan = conditionalStack.Pop();
previousSpan.SetEnd(directiveTrivia.FullSpan.Start);

AddNewDirectiveTriviaSpan(conditionalBuilder, conditionalStack, directiveTrivia);
AddNewDirectiveTriviaSpan(conditionalStack, directiveTrivia);
break;

case SyntaxKind.EndIfDirectiveTrivia:
CloseDirectiveTriviaSpan(conditionalBuilder, conditionalStack, directiveTrivia);
CloseDirectiveTriviaSpan(conditionalStack, directiveTrivia);
break;

case SyntaxKind.RegionDirectiveTrivia:
AddNewDirectiveTriviaSpan(regionBuilder, regionStack, directiveTrivia);
AddNewDirectiveTriviaSpan(regionStack, directiveTrivia);
break;

case SyntaxKind.EndRegionDirectiveTrivia:
CloseDirectiveTriviaSpan(regionBuilder, regionStack, directiveTrivia);
CloseDirectiveTriviaSpan(regionStack, directiveTrivia);
break;

case SyntaxKind.PragmaWarningDirectiveTrivia:
Expand All @@ -192,14 +192,14 @@ private static TreeTextSpan.Builder SetupBuilder(CompilationUnitSyntax compilati
return rootBuilder;
}

private static void AddNewDirectiveTriviaSpan(TreeTextSpan.Builder spanBuilder, Stack<TreeTextSpan.Builder> spanStack, DirectiveTriviaSyntax directiveTrivia)
private static void AddNewDirectiveTriviaSpan(Stack<TreeTextSpan.Builder> spanStack, DirectiveTriviaSyntax directiveTrivia)
{
var parent = spanStack.Peek();
var newDirectiveSpan = parent.AddChild(directiveTrivia.FullSpan.Start);
spanStack.Push(newDirectiveSpan);
}

private static void CloseDirectiveTriviaSpan(TreeTextSpan.Builder spanBuilder, Stack<TreeTextSpan.Builder> spanStack, DirectiveTriviaSyntax directiveTrivia)
private static void CloseDirectiveTriviaSpan(Stack<TreeTextSpan.Builder> spanStack, DirectiveTriviaSyntax directiveTrivia)
{
var previousSpan = spanStack.Pop();
previousSpan.SetEnd(directiveTrivia.FullSpan.End);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
SettingsResources.SettingsFileCodeFix,
cancellationToken => GetTransformedSolutionAsync(context.Document, diagnostic, cancellationToken),
cancellationToken => GetTransformedSolutionAsync(context.Document, cancellationToken),
nameof(SettingsFileCodeFixProvider)),
diagnostic);
}
Expand All @@ -94,8 +94,11 @@ public override FixAllProvider GetFixAllProvider()
return null;
}

private static Task<Solution> GetTransformedSolutionAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
private static Task<Solution> GetTransformedSolutionAsync(Document document, CancellationToken cancellationToken)
{
// Currently unused
_ = cancellationToken;

var project = document.Project;
var solution = project.Solution;

Expand Down
Loading