Ejemplo n.º 1
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);
            }
        }
        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);
        }
Ejemplo n.º 3
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 declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<LocalDeclarationStatementSyntax>().First();

            context.RegisterFix(CodeAction.Create("Make constant", c => MakeConstAsync(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;

			// Find the type declaration identified by the diagnostic.
			var declaration = root.FindToken(diagnosticSpan.Start).Parent;

			// Register a code action that will invoke the fix.
			context.RegisterFix(
				CodeAction.Create("Make lowercase", c => MakeLowercaseAsync(context.Document, declaration, c)),
				diagnostic);
		}
        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            // TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            // Find the type declaration identified by the diagnostic.
            var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<TypeDeclarationSyntax>().First();

            // Register a code action that will invoke the fix.
            context.RegisterFix(
                CodeAction.Create("Make uppercase", c => MakeUppercaseAsync(context.Document, declaration, c)),
                diagnostic);
        }
Ejemplo n.º 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;

            // Find the type declaration identified by the diagnostic.
            var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ConstructorDeclarationSyntax>().First();

            var previousWhiteSpacesToken = SyntaxFactory.Token(declaration.GetLeadingTrivia(), SyntaxKind.StringLiteralToken, SyntaxTriviaList.Empty);
            var newList = SyntaxTokenList.Create(previousWhiteSpacesToken);

            var newDeclaration = declaration.WithModifiers(newList).
                AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword)).
                AddModifiers(SyntaxFactory.Token(SyntaxTriviaList.Create(SyntaxFactory.Space), SyntaxKind.StringLiteralToken, SyntaxTriviaList.Empty));

            // Register a code action that will invoke the fix.
                context.RegisterFix(
                CodeAction.Create("Make constructor protected", c => ReplaceConstructorInDocumentAsync(context.Document, declaration, newDeclaration, c)),
                diagnostic);
        }