本文整理汇总了C#中IfStatementSyntax.GetLeadingTrivia方法的典型用法代码示例。如果您正苦于以下问题:C# IfStatementSyntax.GetLeadingTrivia方法的具体用法?C# IfStatementSyntax.GetLeadingTrivia怎么用?C# IfStatementSyntax.GetLeadingTrivia使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IfStatementSyntax
的用法示例。
在下文中一共展示了IfStatementSyntax.GetLeadingTrivia方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UseExistenceOperatorAsyncWithReturnAsync
private static async Task<Document> UseExistenceOperatorAsyncWithReturnAsync(Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken, ReturnStatementSyntax returnIf)
{
var newMemberAccess = ((MemberAccessExpressionSyntax)returnIf.Expression).ToConditionalAccessExpression();
var newReturn = SyntaxFactory.ReturnStatement(newMemberAccess)
.WithLeadingTrivia(ifStatement.GetLeadingTrivia())
.WithTrailingTrivia(ifStatement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
var root = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = root.ReplaceNode(ifStatement, newReturn);
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument;
}
示例2: MergeIfs
private static SyntaxNode MergeIfs(IfStatementSyntax ifStatement, SyntaxNode root)
{
var nestedIf = (IfStatementSyntax)ifStatement.Statement.GetSingleStatementFromPossibleBlock();
var newIf = ifStatement
.WithCondition(SyntaxFactory.BinaryExpression(SyntaxKind.LogicalAndExpression, ifStatement.Condition, nestedIf.Condition))
.WithStatement(nestedIf.Statement)
.WithLeadingTrivia(ifStatement.GetLeadingTrivia().AddRange(nestedIf.GetLeadingTrivia()))
.WithAdditionalAnnotations(Formatter.Annotation);
if (ifStatement.HasTrailingTrivia && nestedIf.HasTrailingTrivia && !ifStatement.GetTrailingTrivia().Equals(nestedIf.GetTrailingTrivia()))
newIf = newIf.WithTrailingTrivia(ifStatement.GetTrailingTrivia().AddRange(nestedIf.GetTrailingTrivia()));
var newRoot = root.ReplaceNode(ifStatement, newIf);
return newRoot;
}
示例3: UseExistenceOperatorAsyncWithAssignmentAsync
private static async Task<Document> UseExistenceOperatorAsyncWithAssignmentAsync(Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken, ExpressionStatementSyntax expressionIf)
{
var memberAccessAssignment = (AssignmentExpressionSyntax)expressionIf.Expression;
var newMemberAccess = ((MemberAccessExpressionSyntax)memberAccessAssignment.Right).ToConditionalAccessExpression();
var newExpressionStatement = SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, memberAccessAssignment.Left, newMemberAccess))
.WithLeadingTrivia(ifStatement.GetLeadingTrivia())
.WithTrailingTrivia(ifStatement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
var root = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = root.ReplaceNode(ifStatement, newExpressionStatement);
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument;
}
示例4: TraverseIfStatements
private Decisions TraverseIfStatements(IfStatementSyntax iss, ref int exitPoints, bool nested = false)
{
Decisions retDecision = new Decisions();
IfStatement ifStm = new IfStatement();
if (iss.HasLeadingTrivia)
{
SetOuterComments(ifStm, iss.GetLeadingTrivia().ToFullString());
}
if (iss.HasTrailingTrivia)
{
SetInnerComments(ifStm, iss.GetTrailingTrivia().ToFullString());
}
ifStm.IsNested = nested;
//public int ExpressionListEndLn { get; set; }
//public int ExpressionListStartLn { get; set; }
//public bool IsNested { get; set; }
ExpressionSyntax es = iss.Condition;
var binaryExpressions = from aBinaryExpression in iss.Condition.DescendantNodesAndSelf().OfType<BinaryExpressionSyntax>() select aBinaryExpression;
ifStm.ConditionCount = binaryExpressions.Count();
//This area is for the conditions i.e. the stuff in the () of if ()
foreach (BinaryExpressionSyntax bes in binaryExpressions)
{
Method tempMethod = TraverseBinaryExpression(bes);
ifStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
ifStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
}
//TODO handle catches (if there are even catches inside if statements)
var catches = from aCatch in iss.ChildNodes().OfType<CatchClauseSyntax>() select aCatch;
foreach (CatchClauseSyntax ccs in catches)
{
Decisions tempCatch = TraverseCatchClauses(ccs, ref exitPoints, true);
ifStm.Nested.AddRange(tempCatch.Catches);
}
//TODO need to handle else clauses
var elses = from aElse in iss.ChildNodes().OfType<ElseClauseSyntax>() select aElse;
foreach (ElseClauseSyntax ecs in elses)
{
//TODO
//Traverse ElseClauseSyntax
Decisions tempElse = TraverseElseClauses(ecs, ref exitPoints, true);
ifStm.Nested.AddRange(tempElse.ElseStatements);
#region old code with method return
//ifStm.InvokedMethods.AddRange(tempElse.InvokedMethods);
//ifStm.AccessedVars.AddRange(tempElse.AccessedVars);
//ifStm.Nested.Add(tempElse);
//foreach (BaseDecisions bd in ifStm.Nested)
//{
// bd.IsNested = true;
//}
#endregion
}
//TODO need to find a way to return both nested
//and invoked methods / accessedvars to the parent method
var statements = from aStatement in iss.Statement.ChildNodes().OfType<StatementSyntax>() select aStatement;
List<BaseDecisions> retBd = new List<BaseDecisions>();
List<InvokedMethod> retIm = new List<InvokedMethod>();
#region Nested and Invoked Methods and accessed vars
foreach (StatementSyntax ss in statements)
{
//if (ss is BreakStatementSyntax)
//{
//}
//else if (ss is CheckedStatementSyntax)
//{
//}
//else if (ss is ContinueStatementSyntax)
//{
//}
if (ss is DoStatementSyntax)
{
Decisions dwl = TraverseDoStatements(ss as DoStatementSyntax, ref exitPoints, true);
ifStm.Nested.AddRange(dwl.DoWhileLoops);
//dwl.IsNested = true;
//ifStm.Nested.Add(dwl);
}
//else if (ss is EmptyStatementSyntax)
//{
//}
else if (ss is ExpressionStatementSyntax)
{
Method tempMethod = TraverseExpressionStatementSyntax(ss as ExpressionStatementSyntax);
ifStm.AccessedVars.AddRange(tempMethod.AccessedVariables);
ifStm.InvokedMethods.AddRange(tempMethod.InvokedMethods);
}
//else if (ss is FixedStatementSyntax)
//{
//}
else if (ss is ForEachStatementSyntax)
{
Decisions fes = TraverseForEachStatements(ss as ForEachStatementSyntax, ref exitPoints, true);
ifStm.Nested.AddRange(fes.ForEachStatements);
//fes.IsNested = true;
//ifStm.Nested.Add(fes);
}
else if (ss is ForStatementSyntax)
{
Decisions fs = TraverseForStatements(ss as ForStatementSyntax, ref exitPoints, true);
ifStm.Nested.AddRange(fs.ForStatements);
//.........这里部分代码省略.........