public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var result = await context.TryGetSyntaxNode <MethodDeclarationSyntax>(AsyncDiagnosticIdentifiers.NonAsyncMethodNamesShouldNotBeSuffixedWithAsync);

            if (!result.success)
            {
                return;
            }

            var methodDeclaration = result.syntaxNode;
            var diagnostic        = result.diagnostic;
            var root = result.root;

            var solution      = context.Document.Project.Solution;
            var semanticModel = await context.Document.GetSemanticModelAsync().ConfigureAwait(false);

            var methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken) as IMethodSymbol;
            var oldName      = methodDeclaration.Identifier.ValueText;
            var newName      = oldName.Remove(oldName.Length - AsyncConstants.AsyncSuffix.Length);

            var codeAction = CodeAction.Create(
                title: string.Format(Title.ToString(), oldName, newName),
                createChangedSolution: cancellationToken => Renamer.RenameSymbolAsync(solution, methodSymbol, newName, default(OptionSet), cancellationToken),
                equivalenceKey: EquivalenceKey);

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Example #2
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var result = await context.TryGetSyntaxNode <ClassDeclarationSyntax>(DiagnosticId);

            if (!result.success)
            {
                return;
            }

            var classDeclaration = result.syntaxNode;
            var diagnostic       = result.diagnostic;
            var root             = result.root;

            var codeAction = CodeAction.Create(
                title: Title.ToString(),
                createChangedDocument: cancellationToken => AddSealedModifierToClass(context, root, classDeclaration),
                equivalenceKey: EquivalenceKey);

            context.RegisterCodeFix(codeAction, diagnostic);
        }
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var result = await context.TryGetSyntaxNode <SwitchStatementSyntax>(EnumDiagnosticIdentifiers.DefaultLabelShouldBeTheLast);

            if (!result.success)
            {
                return;
            }

            var switchStatement = result.syntaxNode;
            var diagnostic      = result.diagnostic;
            var root            = result.root;

            var codeAction = CodeAction.Create(
                title: Title.ToString(),
                createChangedDocument: cancellationToken => MoveDefaultLabelToLastInSwitchStatement(context, root, switchStatement),
                equivalenceKey: EquivalenceKey);

            context.RegisterCodeFix(codeAction, diagnostic);
        }
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var result = await context.TryGetSyntaxNode <MethodDeclarationSyntax>(AsyncDiagnosticIdentifiers.AvoidAsyncVoidMethods);

            if (!result.success)
            {
                return;
            }

            var methodDeclaration = result.syntaxNode;
            var diagnostic        = result.diagnostic;
            var root = result.root;

            var codeAction = CodeAction.Create(
                title: Title.ToString(),
                createChangedDocument: cancellationToken => ChangeReturnTypeToTask(context, root, methodDeclaration),
                equivalenceKey: EquivalenceKey);

            context.RegisterCodeFix(codeAction, diagnostic);
        }
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var result = await context.TryGetSyntaxNode <AwaitExpressionSyntax>(AsyncDiagnosticIdentifiers.UseConfigureAwaitFalse);

            if (!result.success)
            {
                return;
            }

            var awaitExpression = result.syntaxNode;
            var diagnostic      = result.diagnostic;
            var root            = result.root;

            var codeAction = CodeAction.Create(
                title: Title.ToString(),
                createChangedDocument: cancellationToken => AddConfigureAwaitFalse(context, root, awaitExpression),
                equivalenceKey: EquivalenceKey);

            context.RegisterCodeFix(codeAction, diagnostic);
        }