public override SyntaxNode VisitUsingStatement(UsingStatementSyntax node) { node = (UsingStatementSyntax)base.VisitUsingStatement(node); if (SimplifyNestedUsingStatementAnalyzer.ContainsEmbeddableUsingStatement(node)) { var block = (BlockSyntax)node.Statement; SyntaxToken closeParen = node.CloseParenToken; SyntaxTriviaList trailing = closeParen.TrailingTrivia; if (!trailing.Any(SyntaxKind.EndOfLineTrivia)) { trailing = trailing.EmptyIfWhitespace().AddRange(block.OpenBraceToken.TrailingTrivia); closeParen = closeParen.WithTrailingTrivia(trailing); } node = node.Update( usingKeyword: node.UsingKeyword, openParenToken: node.OpenParenToken, declaration: node.Declaration, expression: node.Expression, closeParenToken: closeParen, statement: block.Statements[0]); } return(node); }
public override SyntaxNode VisitUsingStatement(UsingStatementSyntax node) { if (SimplifyNestedUsingStatementAnalyzer.ContainsEmbeddableUsingStatement(node)) { var block = (BlockSyntax)node.Statement; node = node.WithStatement(block.Statements[0]); } return(base.VisitUsingStatement(node)); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out UsingStatementSyntax usingStatement)) { return; } bool fMultiple = usingStatement.Statement .DescendantNodes() .Any(f => f.IsKind(SyntaxKind.UsingStatement) && SimplifyNestedUsingStatementAnalyzer.ContainsEmbeddableUsingStatement((UsingStatementSyntax)f)); CodeAction codeAction = CodeAction.Create( "Remove braces", cancellationToken => SimplifyNestedUsingStatementRefactoring.RefactorAsync(context.Document, usingStatement, cancellationToken), GetEquivalenceKey(DiagnosticIdentifiers.SimplifyNestedUsingStatement)); context.RegisterCodeFix(codeAction, context.Diagnostics); }