Example #1
0
        public static void ChangeType(
            CodeFixContext context,
            Diagnostic diagnostic,
            TypeSyntax type,
            ITypeSymbol newTypeSymbol,
            SemanticModel semanticModel,
            string additionalKey = null)
        {
            string typeName = SymbolDisplay.GetMinimalString(newTypeSymbol, semanticModel, type.SpanStart);

            string title = $"Change type to '{typeName}'";

            Document document = context.Document;

            CodeAction codeAction = CodeAction.Create(
                title,
                cancellationToken =>
            {
                TypeSyntax newType = newTypeSymbol.ToMinimalTypeSyntax(semanticModel, type.SpanStart).WithTriviaFrom(type);

                return(document.ReplaceNodeAsync(type, newType, cancellationToken));
            },
                EquivalenceKeyProvider.GetEquivalenceKey(diagnostic, additionalKey));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Example #2
0
        public static void ReplaceNullWithDefaultValue(
            CodeFixContext context,
            Diagnostic diagnostic,
            ExpressionSyntax expression,
            SemanticModel semanticModel,
            string additionalKey = null)
        {
            ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(expression, context.CancellationToken).ConvertedType;

            if (typeSymbol?.SupportsExplicitDeclaration() != true)
            {
                return;
            }

            Document document = context.Document;

            CodeAction codeAction = CodeAction.Create(
                "Replace 'null' with default value",
                cancellationToken =>
            {
                ExpressionSyntax newNode = typeSymbol.ToDefaultValueSyntax(semanticModel, expression.SpanStart);

                return(document.ReplaceNodeAsync(expression, newNode, cancellationToken));
            },
                EquivalenceKeyProvider.GetEquivalenceKey(diagnostic, additionalKey));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.DefineMatchingOperator))
            {
                return;
            }

            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out OperatorDeclarationSyntax operatorDeclaration))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case CompilerDiagnosticIdentifiers.OperatorRequiresMatchingOperatorToAlsoBeDefined:
                {
                    SyntaxToken token = operatorDeclaration.OperatorToken;

                    SyntaxKind matchingKind = GetMatchingOperatorToken(token);

                    if (matchingKind == SyntaxKind.None)
                    {
                        break;
                    }

                    SyntaxToken newToken = SyntaxFactory.Token(token.LeadingTrivia, matchingKind, token.TrailingTrivia);

                    if (operatorDeclaration.BodyOrExpressionBody() == null)
                    {
                        break;
                    }

                    if (!(operatorDeclaration.Parent is TypeDeclarationSyntax typeDeclaration))
                    {
                        break;
                    }

                    CodeAction codeAction = CodeAction.Create(
                        $"Generate {newToken} operator",
                        cancellationToken =>
                        {
                            OperatorDeclarationSyntax newNode = operatorDeclaration.WithOperatorToken(newToken);

                            return(context.Document.InsertNodeAfterAsync(operatorDeclaration, newNode, cancellationToken));
                        },
                        EquivalenceKeyProvider.GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Example #4
0
        public static void RemoveMember(CodeFixContext context, Diagnostic diagnostic, MemberDeclarationSyntax memberDeclaration, string additionalKey = null)
        {
            Document document = context.Document;

            CodeAction codeAction = CodeAction.Create(
                $"Remove {memberDeclaration.GetTitle()}",
                cancellationToken => document.RemoveMemberAsync(memberDeclaration, cancellationToken),
                EquivalenceKeyProvider.GetEquivalenceKey(diagnostic, additionalKey));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Example #5
0
        public static void AddCastExpression(
            CodeFixContext context,
            Diagnostic diagnostic,
            ExpressionSyntax expression,
            ITypeSymbol destinationType,
            SemanticModel semanticModel)
        {
            string typeName = SymbolDisplay.GetMinimalString(destinationType, semanticModel, expression.SpanStart);

            TypeSyntax newType = SyntaxFactory.ParseTypeName(typeName);

            CodeAction codeAction = CodeAction.Create(
                $"Cast to '{typeName}'",
                cancellationToken => AddCastExpressionRefactoring.RefactorAsync(context.Document, expression, newType, cancellationToken),
                EquivalenceKeyProvider.GetEquivalenceKey(diagnostic, CodeFixIdentifiers.AddCastExpression));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Example #6
0
        public static void ChangeTypeToVar(
            CodeFixContext context,
            Diagnostic diagnostic,
            TypeSyntax type,
            string additionalKey = null)
        {
            Document document = context.Document;

            CodeAction codeAction = CodeAction.Create(
                "Change type to 'var'",
                cancellationToken =>
            {
                TypeSyntax newType = CSharpFactory.VarType().WithTriviaFrom(type);

                return(document.ReplaceNodeAsync(type, newType, cancellationToken));
            },
                EquivalenceKeyProvider.GetEquivalenceKey(diagnostic, additionalKey));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Example #7
0
        public static void RemoveStatement(
            CodeFixContext context,
            Diagnostic diagnostic,
            StatementSyntax statement,
            string title         = null,
            string additionalKey = null)
        {
            if (statement.IsEmbedded())
            {
                return;
            }

            Document document = context.Document;

            CodeAction codeAction = CodeAction.Create(
                title ?? $"Remove {statement.GetTitle()}",
                cancellationToken => document.RemoveStatementAsync(statement, cancellationToken),
                EquivalenceKeyProvider.GetEquivalenceKey(diagnostic, additionalKey));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Example #8
0
 private static string GetEquivalenceKey(Diagnostic diagnostic, string additionalKey)
 {
     return(EquivalenceKeyProvider.GetEquivalenceKey(diagnostic, additionalKey));
 }
Example #9
0
 protected static string GetEquivalenceKey(string key, string additionalKey = null)
 {
     return(EquivalenceKeyProvider.GetEquivalenceKey(key, additionalKey));
 }
Example #10
0
 protected static string GetEquivalenceKey(Diagnostic diagnostic, string additionalKey = null)
 {
     return(EquivalenceKeyProvider.GetEquivalenceKey(diagnostic, additionalKey));
 }