本文整理汇总了C#中IfStatementSyntax.TrackNodes方法的典型用法代码示例。如果您正苦于以下问题:C# IfStatementSyntax.TrackNodes方法的具体用法?C# IfStatementSyntax.TrackNodes怎么用?C# IfStatementSyntax.TrackNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IfStatementSyntax
的用法示例。
在下文中一共展示了IfStatementSyntax.TrackNodes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ScanIfElse
internal static IEnumerable<CodeAction> ScanIfElse(SemanticModel ctx, Document document, SyntaxNode root, IfStatementSyntax ifElseStatement, BinaryExpressionSyntax isExpression, out int foundCastCount)
{
foundCastCount = 0;
var innerCondition = ifElseStatement.Condition.SkipParens();
if (innerCondition != null && innerCondition.IsKind(SyntaxKind.LogicalNotExpression))
{
var c2 = ((PrefixUnaryExpressionSyntax)innerCondition).Operand.SkipParens();
if (c2.IsKind(SyntaxKind.IsExpression))
{
return HandleNegatedCase(ctx, document, root, ifElseStatement, ifElseStatement.Condition, isExpression, out foundCastCount);
}
return Enumerable.Empty<CodeAction>();
}
var castToType = isExpression.Right;
var embeddedStatment = ifElseStatement.Statement;
var rr = ctx.GetTypeInfo(castToType);
if (rr.Type == null || !rr.Type.IsReferenceType)
return Enumerable.Empty<CodeAction>();
List<SyntaxNode> foundCasts;
foundCasts = embeddedStatment.DescendantNodesAndSelf(n => !IsCast(ctx, n, rr.Type)).Where(arg => IsCast(ctx, arg, rr.Type)).ToList();
foundCasts.AddRange(ifElseStatement.Condition.DescendantNodesAndSelf(n => !IsCast(ctx, n, rr.Type)).Where(arg => arg.SpanStart > isExpression.Span.End && IsCast(ctx, arg, rr.Type)));
foundCastCount = foundCasts.Count;
return new[] {
CodeActionFactory.Create(
isExpression.Span,
DiagnosticSeverity.Info,
GettextCatalog.GetString ("Use 'as' and check for null"),
t2 => {
var varName = ReplaceAutoPropertyWithPropertyAndBackingFieldCodeRefactoringProvider.GetNameProposal(RefactoringHelpers.GuessNameFromType(rr.Type), ctx, isExpression);
var varDec = SyntaxFactory.LocalDeclarationStatement(
SyntaxFactory.VariableDeclaration(
SyntaxFactory.ParseTypeName("var"),
SyntaxFactory.SeparatedList(new [] {
SyntaxFactory.VariableDeclarator(varName)
.WithInitializer(SyntaxFactory.EqualsValueClause(
SyntaxFactory.BinaryExpression(SyntaxKind.AsExpression, isExpression.Left, isExpression.Right)
))
})
));
var outerIs = isExpression.AncestorsAndSelf().FirstOrDefault(e => !(e.Parent is ParenthesizedExpressionSyntax));
var binaryOperatorExpression = SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, SyntaxFactory.IdentifierName(varName), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression));
SyntaxNode newRoot;
if (IsEmbeddedStatement(ifElseStatement)) {
foundCasts = ifElseStatement.DescendantNodesAndSelf(n => !IsCast(ctx, n, rr.Type)).Where(arg => IsCast(ctx, arg, rr.Type)).ToList();
var newIf = ifElseStatement.TrackNodes(foundCasts.Concat(new [] { outerIs }));
newIf = newIf.ReplaceNode((SyntaxNode)newIf.GetCurrentNode(outerIs), binaryOperatorExpression.WithAdditionalAnnotations(Formatter.Annotation));
foreach (var c in foundCasts) {
newIf = newIf.ReplaceNode((SyntaxNode)newIf.GetCurrentNode(c), SyntaxFactory.IdentifierName(varName).WithAdditionalAnnotations(Formatter.Annotation));
}
var block = SyntaxFactory.Block(new StatementSyntax[] {
varDec,
newIf
});
newRoot = root.ReplaceNode((SyntaxNode)ifElseStatement, block.WithAdditionalAnnotations(Formatter.Annotation));
} else {
newRoot = root.TrackNodes(foundCasts.Concat(new SyntaxNode[] { ifElseStatement, outerIs }) );
newRoot = newRoot.InsertNodesBefore(newRoot.GetCurrentNode(ifElseStatement), new [] { varDec.WithAdditionalAnnotations(Formatter.Annotation) });
newRoot = newRoot.ReplaceNode((SyntaxNode)newRoot.GetCurrentNode(outerIs), binaryOperatorExpression.WithAdditionalAnnotations(Formatter.Annotation));
foreach (var c in foundCasts) {
newRoot = newRoot.ReplaceNode((SyntaxNode)newRoot.GetCurrentNode(c), SyntaxFactory.IdentifierName(varName).WithAdditionalAnnotations(Formatter.Annotation));
}
}
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
)
};
}