Example #1
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            var document    = context.Document;
            var root        = (CompilationUnitSyntax)(await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false));
            var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <TypeDeclarationSyntax>().Single();

            if (GetDocument(document.Project, declaration) != null)
            {
                context.RegisterFix(
                    CodeAction.Create("Integrate type into existing file", c => MoveTypeAsync(document, root, declaration, c)),
                    diagnostic);
            }
            else if (DocumentContainsMultipleTypeDeclarations(root))
            {
                context.RegisterFix(
                    CodeAction.Create("Extract type to file", c => ExtractTypeToNewDocumentAsync(document, root, declaration, c)),
                    diagnostic);
            }
            else
            {
                context.RegisterFix(
                    CodeAction.Create("Rename file to match type name", c => RenameFileAsync(document, declaration, c)),
                    diagnostic);
            }
        }
 public sealed override async Task ComputeFixesAsync(CodeFixContext context)
 {
     var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
     var diagnostic = context.Diagnostics.First();
     var diagnosticSpan = diagnostic.Location.SourceSpan;
     var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<PropertyDeclarationSyntax>().First();
     context.RegisterFix(CodeAction.Create("Change property to 'private set'", c => ChangePropertySetAsync(context.Document, declaration, c, FixType.PrivateFix)), diagnostic);
     context.RegisterFix(CodeAction.Create("Change property to 'protected set'", c => ChangePropertySetAsync(context.Document, declaration, c, FixType.ProtectedFix)), diagnostic);
 }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var declaration    = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <ThrowStatementSyntax>().First();

            context.RegisterFix(CodeAction.Create("Rethrow as inner exception", c => MakeThrowAsInnerAsync(context.Document, declaration, c)), diagnostic);
            context.RegisterFix(CodeAction.Create("Throw original exception", c => MakeThrowAsync(context.Document, declaration, c)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var declaration    = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <CatchClauseSyntax>().First();

            context.RegisterFix(CodeAction.Create("Remove Empty Catch Block", c => RemoveEmptyCatchBlockAsync(context.Document, declaration, c)), diagnostic);
            context.RegisterFix(CodeAction.Create("Remove Empty Catch Block and Put a Documentation Link about Try...Catch use", c => RemoveEmptyCatchBlockPutCommentAsync(context.Document, declaration, c)), diagnostic);
            context.RegisterFix(CodeAction.Create("Insert Exception class to Catch", c => InsertExceptionClassCommentAsync(context.Document, declaration, c)), diagnostic);
        }
Example #5
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            var token = root.FindToken(diagnosticSpan.Start);

            if (token.IsKind(SyntaxKind.CatchKeyword))
            {
                var catchBlock = (CatchClauseSyntax)token.Parent;
                var tryStmt    = (TryStatementSyntax)catchBlock.Parent;

                var throwStatement = SyntaxFactory.ThrowStatement();
                var newStatements  = new SyntaxList <StatementSyntax>().Add(throwStatement);
                var newBlock       = SyntaxFactory.Block().WithStatements(newStatements);
                var newCatchBlock  = SyntaxFactory.CatchClause().WithBlock(newBlock).WithAdditionalAnnotations(Formatter.Annotation);

                var newRoot = root.ReplaceNode(catchBlock, newCatchBlock);

                context.RegisterFix(
                    CodeAction.Create("throw", context.Document.WithSyntaxRoot(newRoot)),
                    diagnostic);
            }
        }
Example #6
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic          = context.Diagnostics.First();
            var diagnosticSpan      = diagnostic.Location.SourceSpan;
            var expressionStatement = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <ExpressionStatementSyntax>().FirstOrDefault();
            var localDeclaration    = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <LocalDeclarationStatementSyntax>().FirstOrDefault();

            if (expressionStatement != null)
            {
                context.RegisterFix(CodeAction.Create("Use object initializer", c => MakeObjectInitializerAsync(context.Document, expressionStatement, c)), diagnostic);
            }
            else if (localDeclaration != null)
            {
                context.RegisterFix(CodeAction.Create("Use object initializer", c => MakeObjectInitializerAsync(context.Document, localDeclaration, c)), diagnostic);
            }
        }
Example #7
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var sourceSpan = diagnostic.Location.SourceSpan;
            var type       = root.FindToken(sourceSpan.Start).Parent.AncestorsAndSelf().OfType <ClassDeclarationSyntax>().First();

            context.RegisterFix(CodeAction.Create("Mark as sealed", ct => MarkClassAsSealed(context.Document, type, ct)), diagnostic);
        }
Example #8
0
        public override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            var declaration = root.FindToken(diagnosticSpan.Start).Parent
                              .FirstAncestorOrSelf <AwaitExpressionSyntax>();

            context.RegisterFix(
                CodeAction.Create("Add ConfigureAwait(false)", cancellationToken =>
                                  AddConfigureAwait(context.Document, declaration, false, cancellationToken)),
                diagnostic);
            context.RegisterFix(
                CodeAction.Create("Add ConfigureAwait(true)", cancellationToken =>
                                  AddConfigureAwait(context.Document, declaration, true, cancellationToken)),
                diagnostic);
        }
Example #9
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var comparison     = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <BinaryExpressionSyntax>().First();

            context.RegisterFix(CodeAction.Create("Removes redundant comparision", c => RemoveRedundantComparisonAsync(context.Document, comparison, c)), diagnostic);
        }
Example #10
0
        public sealed override FixAllProvider GetFixAllProvider() => null; //todo: allow for a fixall but only if we can fix the clash on the builder name in a nice way

        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic           = context.Diagnostics.First();
            var diagnosticSpan       = diagnostic.Location.SourceSpan;
            var assignmentExpression = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <AssignmentExpressionSyntax>().First();

            context.RegisterFix(CodeAction.Create($"Use StringBuilder to create a value for '{assignmentExpression.Left.ToString()}'", c => UseStringBuilderAsync(context.Document, assignmentExpression, c)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var declaration    = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <CatchClauseSyntax>().First();

            context.RegisterFix(CodeAction.Create("Add an Exception class", c => MakeCatchEmptyAsync(context.Document, declaration, c)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var trivia     = root.FindTrivia(diagnostic.Location.SourceSpan.End - 1);
            var newRoot    = RemoveTrailingWhiteSpace(root, trivia);

            context.RegisterFix(CodeAction.Create("Remove trailing whitespace", context.Document.WithSyntaxRoot(newRoot)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var @for           = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <ForStatementSyntax>().First();

            context.RegisterFix(CodeAction.Create("Invert For Loop.", c => InvertForAsync(context.Document, @for, c)), diagnostic);
        }
Example #14
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var parameter      = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <ParameterSyntax>().First();

            context.RegisterFix(CodeAction.Create($"Remove unused parameter: '{parameter.Identifier.ValueText}'", c => RemoveParameter(root, context.Document, parameter, c)), diagnostic);
        }
        public override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = context.Diagnostics.First().Location.SourceSpan;
            var declaration    = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <TypeDeclarationSyntax>().First();

            context.RegisterFix(CodeAction.Create("Ensure type ends in 'Controller'", c => MakeEndInControllerAsync(context.Document, declaration, c)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var declaration    = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <MethodDeclarationSyntax>().First();

            context.RegisterFix(CodeAction.Create("Change method name including 'Async'.", c => ChangeMethodNameAsync(context.Document, declaration, c)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var invocation     = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <InvocationExpressionSyntax>().First();

            context.RegisterFix(CodeAction.Create("Change to string interpolation", c => MakeStringInterpolationAsync(context.Document, invocation, c)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var sourceSpan = diagnostic.Location.SourceSpan;
            var method     = root.FindToken(sourceSpan.Start).Parent.AncestorsAndSelf().OfType <MethodDeclarationSyntax>().First();

            context.RegisterFix(
                CodeAction.Create("Call GC.SuppressFinalize", ct => RemoveThrow(context.Document, method, ct)), diagnostic);
        }
Example #19
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var sourceSpan = diagnostic.Location.SourceSpan;
            var @throw     = root.FindToken(sourceSpan.Start).Parent.AncestorsAndSelf().OfType <ThrowStatementSyntax>().First();

            context.RegisterFix(
                CodeAction.Create("Remove this exception", ct => RemoveThrow(context.Document, @throw, ct)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var          diagnostic       = context.Diagnostics.First();
            var          diagnosticSpan   = diagnostic.Location.SourceSpan;
            var          localDeclaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <LocalDeclarationStatementSyntax>().First();
            const string message          = "Make constant";

            context.RegisterFix(CodeAction.Create(message, c => MakeConstantAsync(context.Document, localDeclaration, c)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            var ctor = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <ConstructorDeclarationSyntax>().First();

            context.RegisterFix(CodeAction.Create("Use 'protected' instead of 'public'", c => ReplacePublicWithProtectedAsync(context.Document, ctor, c)), diagnostic);
        }
Example #22
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var ifStatement    = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <IfStatementSyntax>().First();
            var message        = "Convert to 'switch'";

            context.RegisterFix(CodeAction.Create(message, c => ConvertToSwitchAsync(context.Document, ifStatement, c)), diagnostic);
        }
Example #23
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var sourceSpan = diagnostic.Location.SourceSpan;
            var invocation = root.FindToken(sourceSpan.Start).Parent.AncestorsAndSelf().OfType <InvocationExpressionSyntax>().First();

            context.RegisterFix(
                CodeAction.Create("Use ?.Invoke operator and method to fire an event.", ct => UseInvoke(context.Document, invocation, ct)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var          diagnostic     = context.Diagnostics.First();
            var          diagnosticSpan = diagnostic.Location.SourceSpan;
            const string message        = "Convert to an expression bodied member.";

            var methodDeclaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <BaseMethodDeclarationSyntax>().FirstOrDefault();

            if (methodDeclaration != null)
            {
                context.RegisterFix(CodeAction.Create(message, c => ConvertToExpressionBodiedMemberAsync(context.Document, methodDeclaration, c)), diagnostic);
            }
            else
            {
                var basePropertyDeclaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <BasePropertyDeclarationSyntax>().First();
                context.RegisterFix(CodeAction.Create(message, c => ConvertToExpressionBodiedMemberAsync(context.Document, basePropertyDeclaration, c)), diagnostic);
            }
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic       = context.Diagnostics.First();
            var diagnosticSpan   = diagnostic.Location.SourceSpan;
            var whereInvoke      = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <InvocationExpressionSyntax>().First();
            var nextMethodInvoke = whereInvoke.Parent.FirstAncestorOrSelf <InvocationExpressionSyntax>();
            var message          = "Remove 'Where' moving predicate to '" + RemoveWhereWhenItIsPossibleAnalyzer.GetNameOfTheInvokedMethod(nextMethodInvoke) + "'";

            context.RegisterFix(CodeAction.Create(message, c => RemoveWhereAsync(context.Document, whereInvoke, nextMethodInvoke, c)), diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var start      = diagnostic.Location.SourceSpan.Start;

            context.RegisterFix(CodeAction.Create(
                                    "Remove commented code.",
                                    c => RemoveCommentedCodeAsync(context.Document, start, c)),
                                diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var declaration    = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <IfStatementSyntax>().First();

            context.RegisterFix(
                CodeAction.Create("Replace with conditional operator", c => MakeConditionalAsync(context.Document, declaration, c)),
                diagnostic);
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic         = context.Diagnostics.First();
            var diagnosticSpan     = diagnostic.Location.SourceSpan;
            var variableDeclarator = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <VariableDeclaratorSyntax>().FirstOrDefault();

            if (variableDeclarator != null)
            {
                context.RegisterFix(CodeAction.Create($"Make readonly: '{variableDeclarator.Identifier.Text}'", c => MakeFieldReadonlyAsync(context.Document, variableDeclarator, c)), diagnostic);
            }
        }
Example #29
0
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var objectCreation = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <ObjectCreationExpressionSyntax>().FirstOrDefault();

            if (objectCreation != null)
            {
                context.RegisterFix(CodeAction.Create($"Dispose object: '{objectCreation.Type.ToString()}'", c => CreateUsingAsync(context.Document, objectCreation, c)), diagnostic);
            }
        }
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic     = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;
            var stringLiteral  = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <LiteralExpressionSyntax>().FirstOrDefault();

            if (stringLiteral != null)
            {
                context.RegisterFix(CodeAction.Create("Use nameof()", c => MakeNameOfAsync(context.Document, stringLiteral, c)), diagnostic);
            }
        }