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


C# StatementSyntax.GetLeadingTrivia方法代码示例

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


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

示例1: TryGetExpression

        private static ExpressionSyntax TryGetExpression(StatementSyntax firstStatement)
        {
            if (firstStatement.Kind() == SyntaxKind.ExpressionStatement)
            {
                return ((ExpressionStatementSyntax)firstStatement).Expression;
            }
            else if (firstStatement.Kind() == SyntaxKind.ReturnStatement)
            {
                var returnStatement = (ReturnStatementSyntax)firstStatement;
                if (returnStatement.Expression != null)
                {
                    // If there are any comments on the return keyword, move them to
                    // the expression.
                    return firstStatement.GetLeadingTrivia().Any(t => t.IsSingleOrMultiLineComment())
                        ? returnStatement.Expression.WithLeadingTrivia(returnStatement.GetLeadingTrivia())
                        : returnStatement.Expression;
                }
            }
            else if (firstStatement.Kind() == SyntaxKind.ThrowStatement)
            {
                var throwStatement = (ThrowStatementSyntax)firstStatement;
                if (throwStatement.Expression != null)
                {
                    return SyntaxFactory.ThrowExpression(throwStatement.ThrowKeyword, throwStatement.Expression);
                }
            }

            return null;
        }
开发者ID:XieShuquan,项目名称:roslyn,代码行数:29,代码来源:BlockSyntaxExtensions.cs

示例2: CreateUsingStatement

 private static UsingStatementSyntax CreateUsingStatement(StatementSyntax statement, BlockSyntax block)
 {
     return SyntaxFactory.UsingStatement(block)
         .WithLeadingTrivia(statement.GetLeadingTrivia())
         .WithTrailingTrivia(statement.GetTrailingTrivia())
         .WithAdditionalAnnotations(Formatter.Annotation);
 }
开发者ID:Cadums01,项目名称:code-cracker,代码行数:7,代码来源:DisposableVariableNotDisposedCodeFixProvider.cs

示例3: 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);
                    }

                    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);
                    }

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

                    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:nikolasmagno,项目名称:code-cracker,代码行数:71,代码来源:ObjectInitializerCodeFixProvider.cs

示例4: 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);
             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:JeanLLopes,项目名称:code-cracker,代码行数:50,代码来源:ObjectInitializerCodeFixProvider.cs

示例5: Trivia

 public static StatementSyntax Trivia(this StatementSyntax node, StatementSyntax that)
 {
     return node.WithLeadingTrivia(that.GetLeadingTrivia()).WithTrailingTrivia(that.GetTrailingTrivia());
 }
开发者ID:marcoparenzan,项目名称:CSharpDay2015,代码行数:4,代码来源:TriviaExtension.cs

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