本文整理汇总了C#中SyntaxNode.FindNode方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.FindNode方法的具体用法?C# SyntaxNode.FindNode怎么用?C# SyntaxNode.FindNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.FindNode方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleSingleIfStatementForm
private Document HandleSingleIfStatementForm(Document document, SyntaxNode root, Diagnostic diagnostic)
{
var ifStatementLocation = diagnostic.AdditionalLocations[0];
var expressionStatementLocation = diagnostic.AdditionalLocations[1];
var ifStatement = (IfStatementSyntax)root.FindNode(ifStatementLocation.SourceSpan);
var expressionStatement = (ExpressionStatementSyntax)root.FindNode(expressionStatementLocation.SourceSpan);
var invocationExpression = (InvocationExpressionSyntax)expressionStatement.Expression;
StatementSyntax newStatement = expressionStatement.WithExpression(
SyntaxFactory.ConditionalAccessExpression(
invocationExpression.Expression,
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberBindingExpression(SyntaxFactory.IdentifierName(nameof(Action.Invoke))), invocationExpression.ArgumentList)));
newStatement = newStatement.WithPrependedLeadingTrivia(ifStatement.GetLeadingTrivia());
if (ifStatement.Parent.IsKind(SyntaxKind.ElseClause) && ifStatement.Statement.IsKind(SyntaxKind.Block))
{
newStatement = ((BlockSyntax)ifStatement.Statement).WithStatements(SyntaxFactory.SingletonList(newStatement));
}
newStatement = newStatement.WithAdditionalAnnotations(Formatter.Annotation);
var newRoot = root.ReplaceNode(ifStatement, newStatement);
return document.WithSyntaxRoot(newRoot);
}
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:26,代码来源:InvokeDelegateWithConditionalAccessCodeFixProvider.cs
示例2: HandleSingleIfStatementForm
private void HandleSingleIfStatementForm(
SyntaxNode root,
SyntaxEditor editor,
Diagnostic diagnostic,
CancellationToken cancellationToken)
{
var ifStatementLocation = diagnostic.AdditionalLocations[0];
var expressionStatementLocation = diagnostic.AdditionalLocations[1];
var ifStatement = (IfStatementSyntax)root.FindNode(ifStatementLocation.SourceSpan);
cancellationToken.ThrowIfCancellationRequested();
var expressionStatement = (ExpressionStatementSyntax)root.FindNode(expressionStatementLocation.SourceSpan);
cancellationToken.ThrowIfCancellationRequested();
var invocationExpression = (InvocationExpressionSyntax)expressionStatement.Expression;
cancellationToken.ThrowIfCancellationRequested();
StatementSyntax newStatement = expressionStatement.WithExpression(
SyntaxFactory.ConditionalAccessExpression(
invocationExpression.Expression,
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberBindingExpression(SyntaxFactory.IdentifierName(nameof(Action.Invoke))), invocationExpression.ArgumentList)));
newStatement = newStatement.WithPrependedLeadingTrivia(ifStatement.GetLeadingTrivia());
if (ifStatement.Parent.IsKind(SyntaxKind.ElseClause) && ifStatement.Statement.IsKind(SyntaxKind.Block))
{
newStatement = ((BlockSyntax)ifStatement.Statement).WithStatements(SyntaxFactory.SingletonList(newStatement));
}
newStatement = newStatement.WithAdditionalAnnotations(Formatter.Annotation);
cancellationToken.ThrowIfCancellationRequested();
editor.ReplaceNode(ifStatement, newStatement);
}
示例3: GetUnnecessaryImports
public static IEnumerable<SyntaxNode> GetUnnecessaryImports(SemanticModel semanticModel, SyntaxNode root, CancellationToken cancellationToken)
{
var diagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken);
if (!diagnostics.Any())
{
return null;
}
var unnecessaryImports = new HashSet<UsingDirectiveSyntax>();
foreach (var diagnostic in diagnostics)
{
if (diagnostic.Id == "CS8019")
{
var node = root.FindNode(diagnostic.Location.SourceSpan) as UsingDirectiveSyntax;
if (node != null)
{
unnecessaryImports.Add(node);
}
}
}
if (cancellationToken.IsCancellationRequested || !unnecessaryImports.Any())
{
return null;
}
return unnecessaryImports;
}
示例4: TestQualifyWithThisCore
private void TestQualifyWithThisCore(SyntaxNode root, int index)
{
var oldTree = root.SyntaxTree;
var span = new TextSpan(index, 4);
var node = root.FindNode(span, getInnermostNodeForTie: true) as SimpleNameSyntax;
Assert.NotNull(node);
Assert.Equal("Sign", node.Identifier.ValueText);
var leadingTrivia = node.GetLeadingTrivia();
var newNode = SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.ThisExpression(),
node.WithoutLeadingTrivia())
.WithLeadingTrivia(leadingTrivia);
var newRoot = root.ReplaceNode(node, newNode);
var newTree = newRoot.SyntaxTree;
var changes = newTree.GetChanges(oldTree);
Assert.NotNull(changes);
Assert.Equal(1, changes.Count);
Assert.Equal("this.", changes[0].NewText);
}
示例5: HandVariableAndIfStatementFormAsync
private static Document HandVariableAndIfStatementFormAsync(
Document document, SyntaxNode root, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var localDeclarationLocation = diagnostic.AdditionalLocations[0];
var ifStatementLocation = diagnostic.AdditionalLocations[1];
var expressionStatementLocation = diagnostic.AdditionalLocations[2];
var localDeclarationStatement = (LocalDeclarationStatementSyntax)root.FindNode(localDeclarationLocation.SourceSpan);
cancellationToken.ThrowIfCancellationRequested();
var ifStatement = (IfStatementSyntax)root.FindNode(ifStatementLocation.SourceSpan);
cancellationToken.ThrowIfCancellationRequested();
var expressionStatement = (ExpressionStatementSyntax)root.FindNode(expressionStatementLocation.SourceSpan);
cancellationToken.ThrowIfCancellationRequested();
var invocationExpression = (InvocationExpressionSyntax)expressionStatement.Expression;
var parentBlock = (BlockSyntax)localDeclarationStatement.Parent;
var newStatement = expressionStatement.WithExpression(
SyntaxFactory.ConditionalAccessExpression(
localDeclarationStatement.Declaration.Variables[0].Initializer.Value.Parenthesize(),
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberBindingExpression(SyntaxFactory.IdentifierName(nameof(Action.Invoke))), invocationExpression.ArgumentList)));
newStatement = newStatement.WithAdditionalAnnotations(Formatter.Annotation);
var editor = new SyntaxEditor(root, document.Project.Solution.Workspace);
editor.ReplaceNode(ifStatement, newStatement);
editor.RemoveNode(localDeclarationStatement, SyntaxRemoveOptions.KeepLeadingTrivia | SyntaxRemoveOptions.AddElasticMarker);
cancellationToken.ThrowIfCancellationRequested();
var newRoot = editor.GetChangedRoot();
return document.WithSyntaxRoot(newRoot);
}
示例6: GetNodeToSimplify
/// <summary>
/// Get node on which to add simplifier and formatter annotation for fixing the given diagnostic.
/// </summary>
protected virtual SyntaxNode GetNodeToSimplify(SyntaxNode root, SemanticModel model, Diagnostic diagnostic, Workspace workspace, out string codeActionEquivalenceKey, CancellationToken cancellationToken)
{
codeActionEquivalenceKey = null;
var span = diagnostic.Location.SourceSpan;
return root.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true);
}
示例7: TestReplaceWithBuiltInTypeCore
private void TestReplaceWithBuiltInTypeCore(SyntaxNode root, int index)
{
var oldTree = root.SyntaxTree;
var span = new TextSpan(index, 6);
var node = root.FindNode(span, getInnermostNodeForTie: true) as SimpleNameSyntax;
Assert.NotNull(node);
Assert.Equal("Object", node.Identifier.ValueText);
var leadingTrivia = node.GetLeadingTrivia();
var newNode = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ObjectKeyword))
.WithLeadingTrivia(leadingTrivia);
var newRoot = root.ReplaceNode(node, newNode);
var newTree = newRoot.SyntaxTree;
var changes = newTree.GetChanges(oldTree);
Assert.NotNull(changes);
Assert.Equal(1, changes.Count);
Assert.Equal("o", changes[0].NewText);
}