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); }
public static bool CanRefactor(UsingStatementSyntax usingStatement) { return(UsingStatementAnalysis.ContainsEmbeddableUsingStatement(usingStatement) && !usingStatement .Ancestors() .Any(f => f.IsKind(SyntaxKind.UsingStatement) && UsingStatementAnalysis.ContainsEmbeddableUsingStatement((UsingStatementSyntax)f))); }
public override SyntaxNode VisitUsingStatement(UsingStatementSyntax node) { if (UsingStatementAnalysis.ContainsEmbeddableUsingStatement(node)) { var block = (BlockSyntax)node.Statement; node = node.WithStatement(block.Statements[0]); } return(base.VisitUsingStatement(node)); }