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


C# ExpressionStatementSyntax.GetLeadingTrivia方法代码示例

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


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

示例1: VisitExpressionStatement

            public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax syntaxNode)
            {
                bool? isAssertTF = IsAssertTrueOrFalse(syntaxNode);
                if (isAssertTF != null)
                {
                    string firstArg = null, secondArg = null;

                    var expr = syntaxNode.Expression as InvocationExpressionSyntax;
                    var firstArgNode = expr.ArgumentList.Arguments.First().Expression;

                    if (firstArgNode.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        // revert True and False
                        string fmt = isAssertTF.Value ? AssertFalseNoMsg : AssertTrueNoMsg;
                        // the first char should be !
                        // the comments associated with this arg will be lost
                        firstArg = firstArgNode.ToString().Trim().Substring(1);
                        if (expr.ArgumentList.Arguments.Count == 2)
                        {
                            secondArg = expr.ArgumentList.Arguments.Last().ToString().Trim();
                            fmt = isAssertTF.Value ? AssertFalse : AssertTrue;
                        }

                        return SyntaxFactory.ParseStatement(syntaxNode.GetLeadingTrivia().ToFullString() +
                            string.Format(fmt, firstArg, secondArg) +
                            syntaxNode.GetTrailingTrivia().ToFullString());
                    }
                    else if (firstArgNode.IsKind(SyntaxKind.EqualsExpression) || firstArgNode.IsKind(SyntaxKind.NotEqualsExpression))
                    {
                        BinaryExpressionSyntax expr2 = firstArgNode as BinaryExpressionSyntax;
                        firstArg = expr2.Left.ToString().Trim();
                        secondArg = expr2.Right.ToString().Trim();

                        bool isEqual = firstArgNode.IsKind(SyntaxKind.EqualsExpression);
                        // Assert.True(a==b) || Assert.False(a!=b)
                        bool positive = isAssertTF.Value && isEqual || !(isAssertTF.Value || isEqual);
                        var fmt = positive ? AssertEqual : AssertNotEqual;

                        // special case
                        if (IsSpecialValue(ref firstArg, ref secondArg, "null"))
                        {
                            // Assert.True(cond ==|!= null) || Assert.False(cond ==|!= null)
                            fmt = positive ? AssertNull : AssertNotNull;
                        }
                        else if (IsSpecialValue(ref firstArg, ref secondArg, "true"))
                        {
                            // Assert.True(cond ==|!= true) || Assert.False(cond ==|!= true)
                            fmt = positive ? AssertTrueNoMsg : AssertFalseNoMsg;
                        }
                        else if (IsSpecialValue(ref firstArg, ref secondArg, "false"))
                        {
                            // Assert.True(cond ==|!= false) || Assert.False(cond ==|!= false)
                            fmt = positive ? AssertFalseNoMsg : AssertTrueNoMsg;
                        }
                        else
                        {
                            int v = 0;
                            // if second is a const (int only for now)
                            if (int.TryParse(secondArg, out v))
                            {
                                // swap
                                string tmp = firstArg;
                                firstArg = secondArg;
                                secondArg = tmp;
                            }
                        }

                        return SyntaxFactory.ParseStatement(
                                syntaxNode.GetLeadingTrivia().ToFullString() +
                                string.Format(fmt, firstArg, secondArg) +
                                syntaxNode.GetTrailingTrivia().ToFullString());
                    }
                }

                return base.VisitExpressionStatement(syntaxNode);
            }
开发者ID:chuck-mitchell,项目名称:codeformatter,代码行数:76,代码来源:TestAssertTrueOrFalseConverter.cs

示例2: VisitExpressionStatement

            public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node)
            {
                SyntaxNode updatedNode = base.VisitExpressionStatement(node);

                if (node.Expression.Kind() == SyntaxKind.AddAssignmentExpression ||
                    node.Expression.Kind() == SyntaxKind.SubtractAssignmentExpression ||
                    node.Expression.Kind() == SyntaxKind.MultiplyAssignmentExpression ||
                    node.Expression.Kind() == SyntaxKind.DivideAssignmentExpression)
                {
                    // Print value of the variable on the 'Left' side of
                    // compound assignment statements encountered.
                    var compoundAssignmentExpression = (AssignmentExpressionSyntax)node.Expression;
                    StatementSyntax consoleWriteLineStatement =
                        SyntaxFactory.ParseStatement(string.Format("System.Console.WriteLine({0});", compoundAssignmentExpression.Left.ToString()));

                    updatedNode =
                        SyntaxFactory.Block(SyntaxFactory.List<StatementSyntax>(
                                                new StatementSyntax[]
                                                {
                                                    node.WithLeadingTrivia().WithTrailingTrivia(), // Remove leading and trailing trivia.
                                                    consoleWriteLineStatement
                                                }))
                            .WithLeadingTrivia(node.GetLeadingTrivia())        // Attach leading trivia from original node.
                            .WithTrailingTrivia(node.GetTrailingTrivia());     // Attach trailing trivia from original node.
                }

                return updatedNode;
            }
开发者ID:Rickinio,项目名称:roslyn,代码行数:28,代码来源:FAQ.cs

示例3: TraverseExpressionStatementSyntax

        //private Method TraverseStatementSyntax(StatementSyntax ss)
        //{
        //    Method retMethod = new Method();
        //    //TODO
        //    //Get catches for decisions
        //    if (ss is DoStatementSyntax)
        //    {
        //        DoWhileLoop dwl = TraverseDoStatements(ss as DoStatementSyntax);
        //        dwl.IsNested = true;
        //        retMethod.Decisions.DoWhileLoops.Add(dwl);
        //    }
        //    else if (ss is ExpressionStatementSyntax)
        //    {
        //        Method tempMethod = TraverseExpressionStatementSyntax(ss as ExpressionStatementSyntax);
        //        retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
        //        retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
        //    }
        //    else if (ss is ForEachStatementSyntax)
        //    {
        //        ForEachStatement fes = TraverseForEachStatements(ss as ForEachStatementSyntax);
        //        fes.IsNested = true;
        //        retMethod.Decisions.ForEachStatements.Add(fes);
        //    }
        //    else if (ss is ForStatementSyntax)
        //    {
        //        ForStatement fs = TraverseForStatements(ss as ForStatementSyntax);
        //        fs.IsNested = true;
        //        retMethod.Decisions.ForStatements.Add(fs);
        //    }
        //    else if (ss is IfStatementSyntax)
        //    {
        //        IfStatement ifstm = TraverseIfStatements(ss as IfStatementSyntax);
        //        ifstm.IsNested = true;
        //        retMethod.Decisions.IfStatements.Add(ifstm);
        //    }
        //    else if (ss is LocalDeclarationStatementSyntax)
        //    {
        //        Model.Type tempType = new Model.Type();
        //        LocalDeclarationStatementSyntax ldss = ss as LocalDeclarationStatementSyntax;
        //        if (ldss.Declaration != null)
        //        {
        //            VariableDeclarationSyntax vds = ldss.Declaration;
        //            tempType.Name = vds.Type.ToString();
        //            tempType.IsKnownType = true;
        //            tempType.IsNotUserDefined = true;
        //        }
        //        Method tempMethod = TransverseAccessVars(ss as LocalDeclarationStatementSyntax);
        //        //NOT SURE if this will work but here goes
        //        tempMethod.AccessedVariables[0].Type = tempType;
        //        retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
        //        retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
        //    }
        //    else if (ss is SwitchStatementSyntax)
        //    {
        //        SwitchStatement switchStm = TraverseSwitchStatements(ss as SwitchStatementSyntax);
        //        switchStm.IsNested = true;
        //        retMethod.Decisions.SwitchStatements.Add(switchStm);
        //    }
        //    else if (ss is WhileStatementSyntax)
        //    {
        //        WhileLoop wl = TraverseWhileLoops(ss as WhileStatementSyntax);
        //        wl.IsNested = true;
        //        retMethod.Decisions.WhileLoops.Add(wl);
        //    }
        //    return retMethod;
        //}
        private Method TraverseExpressionStatementSyntax(ExpressionStatementSyntax ess)
        {
            Method retMethod = new Method();

            if (ess.HasLeadingTrivia)
            {
                SetOuterComments(retMethod, ess.GetLeadingTrivia().ToFullString());
            }

            if (ess.HasTrailingTrivia)
            {
                SetInnerComments(retMethod, ess.GetTrailingTrivia().ToFullString());
            }

            var vars = from aVar in ess.ChildNodes().OfType<BinaryExpressionSyntax>() select aVar;
            foreach (BinaryExpressionSyntax bes in vars)
            {
                Method tempMethod = TraverseBinaryExpression(bes);
                retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
                retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
            }
            var invokedMethods = from aInvokedMethod in ess.ChildNodes().OfType<InvocationExpressionSyntax>() select aInvokedMethod;
            foreach (InvocationExpressionSyntax ies in invokedMethods)
            {
                Method tempMethod = TraverseInvocationExpression(ies);
                retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
            }
            var postfixUnaryExpressions = from aPostfixUnaryExpression in ess.ChildNodes().OfType<PostfixUnaryExpressionSyntax>() select aPostfixUnaryExpression;
            foreach (PostfixUnaryExpressionSyntax pues in postfixUnaryExpressions)
            {
                Method tempMethod = TraversePostfixUnaryExpressions(pues);
                retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
                retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
//.........这里部分代码省略.........
开发者ID:Zenoware,项目名称:RoslynParser,代码行数:101,代码来源:RoslynCSharpParser.cs


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