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


C# StatementSyntax.FirstAncestorOrSelf方法代码示例

本文整理汇总了C#中StatementSyntax.FirstAncestorOrSelf方法的典型用法代码示例。如果您正苦于以下问题:C# StatementSyntax.FirstAncestorOrSelf方法的具体用法?C# StatementSyntax.FirstAncestorOrSelf怎么用?C# StatementSyntax.FirstAncestorOrSelf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在StatementSyntax的用法示例。


在下文中一共展示了StatementSyntax.FirstAncestorOrSelf方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MakeObjectInitializer

 private static Document MakeObjectInitializer(Document document, SyntaxNode root, StatementSyntax statement, ISymbol variableSymbol, SemanticModel semanticModel)
 {
     var blockParent = statement.FirstAncestorOrSelf<BlockSyntax>();
     var objectCreationExpression = statement.DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Single();
     var newBlockParent = CreateNewBlockParent(statement, semanticModel, objectCreationExpression, variableSymbol);
     var newRoot = root.ReplaceNode(blockParent, newBlockParent);
     var newDocument = document.WithSyntaxRoot(newRoot);
     return newDocument;
 }
开发者ID:nikolasmagno,项目名称:code-cracker,代码行数:9,代码来源:ObjectInitializerCodeFixProvider.cs

示例2: CreateNewBlockParent

 private static BlockSyntax CreateNewBlockParent(StatementSyntax statement, SemanticModel semanticModel, ObjectCreationExpressionSyntax objectCreationExpression, ISymbol variableSymbol)
 {
     var blockParent = statement.FirstAncestorOrSelf<BlockSyntax>();
     var assignmentExpressions = ObjectInitializerAnalyzer.FindAssignmentExpressions(semanticModel, statement, variableSymbol);
     var newBlockParent = SyntaxFactory.Block()
         .WithLeadingTrivia(blockParent.GetLeadingTrivia())
         .WithTrailingTrivia(blockParent.GetTrailingTrivia())
         .WithAdditionalAnnotations(Formatter.Annotation);
     var newAssignmentExpressions = new List<ExpressionStatementSyntax>();
     for (int i = 0; i < blockParent.Statements.Count; i++)
     {
         var blockStatement = blockParent.Statements[i];
         if (blockStatement.Equals(statement))
         {
             var initializationExpressions = new List<AssignmentExpressionSyntax>();
             foreach (var expressionStatement in assignmentExpressions)
             {
                 var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax;
                 var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax;
                 var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax;
                 initializationExpressions.Add(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, propertyIdentifier, assignmentExpression.Right));
             }
             var initializers = SyntaxFactory.SeparatedList<ExpressionSyntax>(initializationExpressions);
             var newObjectCreationExpression = objectCreationExpression.WithInitializer(
                 SyntaxFactory.InitializerExpression(
                     SyntaxKind.ObjectInitializerExpression,
                     SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.OpenBraceToken, SyntaxFactory.ParseTrailingTrivia("\n")),
                     initializers,
                     SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.CloseBraceToken, SyntaxFactory.ParseTrailingTrivia(""))
                 ))
                 .WithLeadingTrivia(objectCreationExpression.GetLeadingTrivia())
                 .WithTrailingTrivia(objectCreationExpression.GetTrailingTrivia())
                 .WithAdditionalAnnotations(Formatter.Annotation);
             if (newObjectCreationExpression.ArgumentList?.Arguments.Count == 0)
             {
                 newObjectCreationExpression = newObjectCreationExpression.WithArgumentList(null);
             }
             var newLocalDeclarationStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression)
                 .WithLeadingTrivia(statement.GetLeadingTrivia())
                 .WithTrailingTrivia(statement.GetTrailingTrivia())
                 .WithAdditionalAnnotations(Formatter.Annotation);
             newBlockParent = newBlockParent.AddStatements(newLocalDeclarationStatement);
             i += initializationExpressions.Count;
         }
         else
         {
             newBlockParent = newBlockParent.AddStatements(blockStatement
                 .WithLeadingTrivia(blockStatement.GetLeadingTrivia())
                 .WithTrailingTrivia(blockStatement.GetTrailingTrivia())
                 .WithAdditionalAnnotations(Formatter.Annotation));
         }
     }
     return newBlockParent;
 }
开发者ID:freneza,项目名称:code-cracker,代码行数:54,代码来源:ObjectInitializerCodeFixProvider.cs

示例3: GetChildStatementsAfter

 private static IList<StatementSyntax> GetChildStatementsAfter(StatementSyntax node)
 {
     var block = node.FirstAncestorOrSelf<BlockSyntax>();
     var statements = block.Statements.Where(s => s.SpanStart > node.SpanStart).ToList();
     return statements;
 }
开发者ID:Cadums01,项目名称:code-cracker,代码行数:6,代码来源:DisposableVariableNotDisposedCodeFixProvider.cs

示例4: GetStatementsInBlocksAfter

 private static List<List<StatementSyntax>> GetStatementsInBlocksAfter(StatementSyntax node)
 {
     var collectionOfStatements = new List<List<StatementSyntax>>();
     var method = node.FirstAncestorOrSelf<MethodDeclarationSyntax>();
     if (method?.Body == null) return collectionOfStatements;
     var currentBlock = node.FirstAncestorOfType<BlockSyntax>();
     while (currentBlock != null)
     {
         var statements = new List<StatementSyntax>();
         foreach (var statement in currentBlock.Statements)
             if (statement.SpanStart > node.SpanStart)
                 statements.Add(statement);
         if (statements.Any()) collectionOfStatements.Add(statements);
         if (method.Body.Equals(currentBlock)) break;
         currentBlock = currentBlock.FirstAncestorOfType<BlockSyntax>();
     }
     return collectionOfStatements;
 }
开发者ID:Cadums01,项目名称:code-cracker,代码行数:18,代码来源:DisposableVariableNotDisposedCodeFixProvider.cs

示例5: UsedOutsideParentBlock

 private static bool UsedOutsideParentBlock(SemanticModel semanticModel, StatementSyntax expressionStatement, ISymbol identitySymbol)
 {
     var block = expressionStatement.FirstAncestorOrSelf<BlockSyntax>();
     var method = expressionStatement.FirstAncestorOrSelf<MethodDeclarationSyntax>();
     var methodBlock = method.Body;
     if (methodBlock.Equals(block)) return false;
     var collectionOfStatementsAfter = GetStatementsInBlocksAfter(block);
     foreach (var allStatementsAfterBlock in collectionOfStatementsAfter)
     {
         if (!allStatementsAfterBlock.Any()) continue;
         var dataFlowAnalysis = semanticModel.AnalyzeDataFlow(allStatementsAfterBlock.First(), allStatementsAfterBlock.Last());
         if (!dataFlowAnalysis.Succeeded) continue;
         var isUsed = dataFlowAnalysis.ReadInside.Contains(identitySymbol)
             || dataFlowAnalysis.WrittenInside.Contains(identitySymbol);
         if (isUsed) return true;
     }
     return false;
 }
开发者ID:Cadums01,项目名称:code-cracker,代码行数:18,代码来源:DisposableVariableNotDisposedCodeFixProvider.cs

示例6: IsDisposedOrAssigned

 private static bool IsDisposedOrAssigned(SemanticModel semanticModel, StatementSyntax statement, ILocalSymbol identitySymbol)
 {
     var method = statement.FirstAncestorOrSelf<MethodDeclarationSyntax>();
     if (method == null) return false;
     if (IsReturned(method, statement, semanticModel, identitySymbol)) return true;
     foreach (var childStatements in method.Body.DescendantNodes().OfType<StatementSyntax>())
     {
         if (childStatements.SpanStart > statement.SpanStart
         && (IsCorrectDispose(childStatements as ExpressionStatementSyntax, semanticModel, identitySymbol)
         || IsAssignedToField(childStatements as ExpressionStatementSyntax, semanticModel, identitySymbol)))
             return true;
     }
     return false;
 }
开发者ID:haroldhues,项目名称:code-cracker,代码行数:14,代码来源:DisposableVariableNotDisposedAnalyzer.cs

示例7: FindAssignmentExpressions

 public static List<ExpressionStatementSyntax> FindAssignmentExpressions(SemanticModel semanticModel, StatementSyntax statement, ISymbol variableSymbol)
 {
     var blockParent = statement.FirstAncestorOrSelf<BlockSyntax>();
     var isBefore = true;
     var assignmentExpressions = new List<ExpressionStatementSyntax>();
     foreach (var blockStatement in blockParent.Statements)
     {
         if (isBefore)
         {
             if (blockStatement.Equals(statement)) isBefore = false;
         }
         else
         {
             var expressionStatement = blockStatement as ExpressionStatementSyntax;
             if (expressionStatement == null) break;
             var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax;
             if (assignmentExpression == null || !assignmentExpression.IsKind(SyntaxKind.SimpleAssignmentExpression)) break;
             var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax;
             if (memberAccess == null || !memberAccess.IsKind(SyntaxKind.SimpleMemberAccessExpression)) break;
             var memberIdentifier = memberAccess.Expression as IdentifierNameSyntax;
             if (memberIdentifier == null) break;
             var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax;
             if (propertyIdentifier == null) break;
             if (!semanticModel.GetSymbolInfo(memberIdentifier).Symbol.Equals(variableSymbol)) break;
             assignmentExpressions.Add(expressionStatement);
         }
     }
     return assignmentExpressions;
 }
开发者ID:JeanLLopes,项目名称:code-cracker,代码行数:29,代码来源:ObjectInitializerAnalyzer.cs

示例8: CreateNewBlockParent

        private static BlockSyntax CreateNewBlockParent(StatementSyntax statement, SemanticModel semanticModel, ObjectCreationExpressionSyntax objectCreationExpression, ISymbol variableSymbol)
        {
            var blockParent = statement.FirstAncestorOrSelf<BlockSyntax>();
            var assignmentExpressions = ObjectInitializerAnalyzer.FindAssignmentExpressions(semanticModel, statement, variableSymbol);
            var newBlockParent = SyntaxFactory.Block()
                .WithLeadingTrivia(blockParent.GetLeadingTrivia())
                .WithTrailingTrivia(blockParent.GetTrailingTrivia())
                .WithAdditionalAnnotations(Formatter.Annotation);

            for (int i = 0; i < blockParent.Statements.Count; i++)
            {
                var blockStatement = blockParent.Statements[i];
                if (blockStatement.Equals(statement))
                {
                    var initializationExpressions = new List<AssignmentExpressionSyntax>();
                    foreach (var expressionStatement in assignmentExpressions)
                    {
                        var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax;
                        var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax;
                        var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax;
                        var newAssignmentExpression = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, propertyIdentifier, assignmentExpression.Right);
                        initializationExpressions.Add(newAssignmentExpression.WithTriviaFrom(expressionStatement));
                    }

                    if (objectCreationExpression.Initializer != null)
                    {
                        var existentInitilizers = objectCreationExpression.Initializer.Expressions.Cast<AssignmentExpressionSyntax>()
                            .Where(ae =>
                            {
                                var propertyIdentifier = ae.Left.ToFullString().Trim();
                                return initializationExpressions.All(ie => ie.Left.ToFullString().Trim() != propertyIdentifier);
                            })
                            .Select(ae => ae.WithoutTrivia())
                            .ToList();
                        initializationExpressions.InsertRange(0, existentInitilizers);
                    }

                    // Trailing trivia will be added before the separator if a simple separator list is used.  This builds the separator token for expression
                    // such that the trailing trivia from the original expression is added after the comma on the same line.
                    var initializerSeparators = initializationExpressions.Take(initializationExpressions.Count - 1).Select(expr => SyntaxFactory.Token(SyntaxKind.CommaToken).WithTrailingTrivia(expr.GetTrailingTrivia())).ToList();
                    var lastInitializer = initializationExpressions.Last(); // Preserve the last initializer before rebuilding the list.
                    // Get all but the last initializer without the trailing trivia.  Trivia will be added after the separator from the list above.
                    initializationExpressions = initializationExpressions.Take(initializationExpressions.Count - 1).Select(expr => expr.WithoutTrailingTrivia()).ToList();
                    initializationExpressions.Add(lastInitializer); // Add the last initializer with all of its trivia.

                    var initializers = SyntaxFactory.SeparatedList<ExpressionSyntax>(initializationExpressions, initializerSeparators);

                    var newObjectCreationExpression = objectCreationExpression.WithInitializer(
                        SyntaxFactory.InitializerExpression(
                            SyntaxKind.ObjectInitializerExpression,
                            SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.OpenBraceToken, SyntaxFactory.ParseTrailingTrivia(Environment.NewLine)),
                            initializers,
                            SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.CloseBraceToken, SyntaxFactory.ParseTrailingTrivia(""))
                        ))
                        .WithLeadingTrivia(objectCreationExpression.GetLeadingTrivia())
                        .WithTrailingTrivia(objectCreationExpression.GetTrailingTrivia())
                        .WithAdditionalAnnotations(Formatter.Annotation);
                    if (newObjectCreationExpression.ArgumentList?.Arguments.Count == 0)
                    {
                        newObjectCreationExpression = newObjectCreationExpression.WithArgumentList(null);
                    }
                    var newLocalDeclarationStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression)
                        .WithLeadingTrivia(statement.GetLeadingTrivia())
                        .WithTrailingTrivia(statement.GetTrailingTrivia())
                        .WithAdditionalAnnotations(Formatter.Annotation);
                    newBlockParent = newBlockParent.AddStatements(newLocalDeclarationStatement);
                    i += initializationExpressions.Count;
                }
                else
                {
                    newBlockParent = newBlockParent.AddStatements(blockStatement
                        .WithLeadingTrivia(blockStatement.GetLeadingTrivia())
                        .WithTrailingTrivia(blockStatement.GetTrailingTrivia())
                        .WithAdditionalAnnotations(Formatter.Annotation));
                }
            }
            return newBlockParent;
        }
开发者ID:haroldhues,项目名称:code-cracker,代码行数:78,代码来源:ObjectInitializerCodeFixProvider.cs


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