public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); UsingStatementSyntax usingStatement = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <UsingStatementSyntax>(); if (usingStatement == null) { return; } bool isMultiple = usingStatement.Statement .DescendantNodes() .Any(f => f.IsKind(SyntaxKind.UsingStatement) && UsingStatementAnalysis.ContainsEmbeddableUsingStatement((UsingStatementSyntax)f)); CodeAction codeAction = CodeAction.Create( "Remove braces from nested using statement" + ((isMultiple) ? "s" : ""), cancellationToken => { return(RemoveBracesFromNestedUsingStatementRefactoring.RefactorAsync( context.Document, usingStatement, cancellationToken)); }, DiagnosticIdentifiers.SimplifyNestedUsingStatement + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, context.Diagnostics); }
private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var usingStatement = (UsingStatementSyntax)context.Node; if (RemoveBracesFromNestedUsingStatementRefactoring.CanRefactor(usingStatement)) { var block = (BlockSyntax)usingStatement.Statement; context.ReportDiagnostic( DiagnosticDescriptors.SimplifyNestedUsingStatement, block.GetLocation()); DiagnosticHelper.FadeOutBraces(context, block, DiagnosticDescriptors.SimplifyNestedUsingStatementFadeOut); } }