private static SyntaxNode GetContainingBlock(SyntaxNode node) { if (node.IsKind(SyntaxKind.ElseClause)) { return(IfElseAnalysis.GetTopmostIf((ElseClauseSyntax)node)?.Parent); } else { return(node.Parent); } }
private static IfStatementSyntax GetTopmostIf(BlockSyntax block) { SyntaxNode parent = block.Parent; switch (parent?.Kind()) { case SyntaxKind.IfStatement: return(IfElseAnalysis.GetTopmostIf((IfStatementSyntax)parent)); case SyntaxKind.ElseClause: return(IfElseAnalysis.GetTopmostIf((ElseClauseSyntax)parent)); default: return(null); } }
private static SyntaxNode GetNewRoot(StatementSyntax statement, SyntaxNode oldRoot, IEnumerable <StatementSyntax> newNodes) { if (statement.Parent.IsKind(SyntaxKind.ElseClause)) { IfStatementSyntax ifStatement = IfElseAnalysis.GetTopmostIf((ElseClauseSyntax)statement.Parent); var block = (BlockSyntax)ifStatement.Parent; int index = block.Statements.IndexOf(ifStatement); BlockSyntax newBlock = block.RemoveNode(statement.Parent, SyntaxRemoveOptions.KeepNoTrivia); newBlock = newBlock.WithStatements(newBlock.Statements.InsertRange(index + 1, newNodes)); return(oldRoot.ReplaceNode(block, newBlock)); } else { return(oldRoot.ReplaceNode(statement.Parent, newNodes)); } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); IfStatementSyntax ifStatement = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <IfStatementSyntax>(); if (ifStatement == null) { return; } ifStatement = IfElseAnalysis.GetTopmostIf(ifStatement); CodeAction codeAction = CodeAction.Create( "Add braces to if-else", cancellationToken => AddBracesToIfElseRefactoring.RefactorAsync(context.Document, ifStatement, cancellationToken), DiagnosticIdentifiers.AddBracesToIfElse + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, context.Diagnostics); }
private static IfStatementSyntax GetTopmostIf(StatementSyntax statement) { SyntaxNode parent = statement.Parent; if (parent != null) { if (parent.IsKind(SyntaxKind.ElseClause)) { return(IfElseAnalysis.GetTopmostIf((ElseClauseSyntax)parent)); } else { var parentStatement = parent as IfStatementSyntax; if (parentStatement != null) { return(IfElseAnalysis.GetTopmostIf(parentStatement)); } } } return(null); }
private static StatementSyntax GetStatement( RefactoringContext context, BlockSyntax block, SyntaxNode parent) { switch (parent?.Kind()) { case SyntaxKind.WhileStatement: case SyntaxKind.DoStatement: case SyntaxKind.ForStatement: case SyntaxKind.ForEachStatement: case SyntaxKind.FixedStatement: case SyntaxKind.CheckedStatement: case SyntaxKind.UncheckedStatement: case SyntaxKind.UnsafeStatement: case SyntaxKind.UsingStatement: case SyntaxKind.LockStatement: { if (block.OpenBraceToken.Span.Contains(context.Span) || block.CloseBraceToken.Span.Contains(context.Span)) { if (parent.IsKind(SyntaxKind.UsingStatement)) { var usingStatement = (UsingStatementSyntax)parent; while (usingStatement.Parent?.IsKind(SyntaxKind.UsingStatement) == true) { usingStatement = (UsingStatementSyntax)usingStatement.Parent; } return(usingStatement); } return((StatementSyntax)parent); } break; } case SyntaxKind.TryStatement: { var tryStatement = (TryStatementSyntax)parent; if (tryStatement.Block?.OpenBraceToken.Span.Contains(context.Span) == true) { return((StatementSyntax)parent); } break; } case SyntaxKind.IfStatement: { var ifStatement = (IfStatementSyntax)parent; if (IfElseAnalysis.IsTopmostIf(ifStatement) && block.OpenBraceToken.Span.Contains(context.Span)) { return(ifStatement); } if (ifStatement.Else == null && block.CloseBraceToken.Span.Contains(context.Span)) { return(ifStatement); } break; } case SyntaxKind.ElseClause: { var elseClause = (ElseClauseSyntax)parent; if (block.CloseBraceToken.Span.Contains(context.Span)) { return(IfElseAnalysis.GetTopmostIf(elseClause)); } break; } case SyntaxKind.CatchClause: { var catchClause = (CatchClauseSyntax)parent; if (catchClause.Parent?.IsKind(SyntaxKind.TryStatement) == true) { var tryStatement = (TryStatementSyntax)catchClause.Parent; if (tryStatement.Finally == null && catchClause.Block?.CloseBraceToken.Span.Contains(context.Span) == true) { return(tryStatement); } } break; } case SyntaxKind.FinallyClause: { var finallyClause = (FinallyClauseSyntax)parent; if (finallyClause.Parent?.IsKind(SyntaxKind.TryStatement) == true) { var tryStatement = (TryStatementSyntax)finallyClause.Parent; if (finallyClause.Block?.CloseBraceToken.Span.Contains(context.Span) == true) { return(tryStatement); } } break; } } return(null); }