当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxNode.FindNode方法代码示例

本文整理汇总了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);
        }
开发者ID:jkotas,项目名称:roslyn,代码行数:35,代码来源:InvokeDelegateWithConditionalAccessCodeFixProvider.cs

示例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;
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:30,代码来源:CSharpRemoveUnnecessaryImportsService.cs

示例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);
        }
开发者ID:elemk0vv,项目名称:roslyn-1,代码行数:24,代码来源:SyntaxDiffingTests.cs

示例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);
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:35,代码来源:InvokeDelegateWithConditionalAccessCodeFixProvider.cs

示例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);
 }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:9,代码来源:BatchSimplificationFixAllProvider.cs

示例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);
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:21,代码来源:SyntaxDiffingTests.cs


注:本文中的SyntaxNode.FindNode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。