本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.ArgumentSyntax.Ancestors方法的典型用法代码示例。如果您正苦于以下问题:C# ArgumentSyntax.Ancestors方法的具体用法?C# ArgumentSyntax.Ancestors怎么用?C# ArgumentSyntax.Ancestors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.Syntax.ArgumentSyntax
的用法示例。
在下文中一共展示了ArgumentSyntax.Ancestors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FormatXmlAsync
private async Task<Document> FormatXmlAsync(Document document, ArgumentSyntax argSntax, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
var tree = root.SyntaxTree;
//Get the character position of the ArgumentSyntax
FileLinePositionSpan position = tree.GetLineSpan(argSntax.Span);
int cSpace = position.StartLinePosition.Character;
//Get the parent VariableDeclaration to figure out the preceeding trivia since we can't
//get the column position from GetLineSpan (bug?)
var parent = argSntax.Ancestors().Where(t => t.IsKind(SyntaxKind.VariableDeclaration)).FirstOrDefault();
var parentTrivia = parent.GetLeadingTrivia().ToFullString();
var xml = argSntax.Expression.GetFirstToken().ValueText;
var newXml = FormatXml(xml);
//Process each line of the formatted XML and prepend the parent trivia & spaces
string[] xmlLines = newXml.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
for (int i = 1; i < xmlLines.Length; i++)
{
xmlLines[i] = parentTrivia + new String(' ', (cSpace - 14)) + xmlLines[i];
}
newXml = String.Join("\r\n", xmlLines);
newXml = "@\"" + newXml + "\"";
var newNode = SyntaxFactory.Argument(null, SyntaxFactory.Token(SyntaxKind.None), SyntaxFactory.IdentifierName(newXml));
var newRoot = root.ReplaceNode(argSntax, newNode);
return document.WithSyntaxRoot(newRoot);
}
示例2: FlowsOut
private static bool FlowsOut(ExpressionSyntax dataFlowParent, ArgumentSyntax decl, IdentifierNameSyntax[] references)
{
ForStatementSyntax forStatement;
if ((forStatement = decl.Ancestors().OfType<ForStatementSyntax>().FirstOrDefault()) != null &&
forStatement.Incrementors.Span.Contains(decl.Position) &&
forStatement.Statement.DescendantNodes().OfType<ForStatementSyntax>().Any(f => f.Condition == null))
{
return false;
}
var containingStatement = decl.Ancestors().OfType<StatementSyntax>().FirstOrDefault();
var containingReturnOrThrow = containingStatement as ReturnStatementSyntax ?? (StatementSyntax)(containingStatement as ThrowStatementSyntax);
MethodDeclarationSyntax methodDeclParent;
if (containingReturnOrThrow != null && decl.Identifier.ValueText == "x1" &&
((methodDeclParent = containingReturnOrThrow.Parent.Parent as MethodDeclarationSyntax) == null ||
methodDeclParent.Body.Statements.First() != containingReturnOrThrow))
{
return false;
}
foreach (var reference in references)
{
if (!dataFlowParent.Span.Contains(reference.Span) &&
(containingReturnOrThrow == null || containingReturnOrThrow.Span.Contains(reference.SpanStart)) &&
(reference.SpanStart > decl.SpanStart ||
(containingReturnOrThrow == null &&
reference.Ancestors().OfType<DoStatementSyntax>().Join(
decl.Ancestors().OfType<DoStatementSyntax>(), d => d, d => d, (d1, d2) => true).Any())))
{
if (IsRead(reference))
{
return true;
}
}
}
return false;
}