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


C# ExpressionSyntax.GetLeadingTrivia方法代码示例

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


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

示例1: TrySimplifyMemberAccessOrQualifiedName

        // Note: The caller needs to verify that replacement doesn't change semantics of the original expression.
        private static bool TrySimplifyMemberAccessOrQualifiedName(
        ExpressionSyntax left,
            ExpressionSyntax right,
            SemanticModel semanticModel,
            OptionSet optionSet,
            out ExpressionSyntax replacementNode,
            out TextSpan issueSpan)
        {
            replacementNode = null;
            issueSpan = default(TextSpan);

            if (left != null && right != null)
            {
                var leftSymbol = SimplificationHelpers.GetOriginalSymbolInfo(semanticModel, left);
                if (leftSymbol != null && (leftSymbol.Kind == SymbolKind.NamedType))
                {
                    var rightSymbol = SimplificationHelpers.GetOriginalSymbolInfo(semanticModel, right);
                    if (rightSymbol != null && (rightSymbol.IsStatic || rightSymbol.Kind == SymbolKind.NamedType))
                    {
                        // Static member access or nested type member access.
                        INamedTypeSymbol containingType = rightSymbol.ContainingType;

                        var enclosingSymbol = semanticModel.GetEnclosingSymbol(left.SpanStart);
                        List<ISymbol> enclosingTypeParametersInsideOut = new List<ISymbol>();

                        while (enclosingSymbol != null)
                        {
                            if (enclosingSymbol is IMethodSymbol)
                            {
                                var methodSymbol = (IMethodSymbol)enclosingSymbol;
                                if (methodSymbol.TypeArguments.Length != 0)
                                {
                                    enclosingTypeParametersInsideOut.AddRange(methodSymbol.TypeArguments);
                                }
                            }

                            if (enclosingSymbol is INamedTypeSymbol)
                            {
                                var namedTypeSymbol = (INamedTypeSymbol)enclosingSymbol;
                                if (namedTypeSymbol.TypeArguments.Length != 0)
                                {
                                    enclosingTypeParametersInsideOut.AddRange(namedTypeSymbol.TypeArguments);
                                }
                            }

                            enclosingSymbol = enclosingSymbol.ContainingSymbol;
                        }

                        if (containingType != null && !containingType.Equals(leftSymbol))
                        {
                            var namedType = leftSymbol as INamedTypeSymbol;
                            if (namedType != null)
                            {
                                if ((namedType.GetBaseTypes().Contains(containingType) &&
                                    !optionSet.GetOption(SimplificationOptions.AllowSimplificationToBaseType)) ||
                                    (!optionSet.GetOption(SimplificationOptions.AllowSimplificationToGenericType) &&
                                    containingType.TypeArguments.Length != 0))
                                {
                                    return false;
                                }
                            }

                            // We have a static member access or a nested type member access using a more derived type.
                            // Simplify syntax so as to use accessed member's most immediate containing type instead of the derived type.
                            replacementNode = containingType.GenerateTypeSyntax()
                                .WithLeadingTrivia(left.GetLeadingTrivia())
                                .WithTrailingTrivia(left.GetTrailingTrivia());
                            issueSpan = left.Span;
                            return true;
                        }
                    }
                }
            }

            return false;
        }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:77,代码来源:ExpressionSyntaxExtensions.cs

示例2: CreatePredefinedTypeSyntax

 private static TypeSyntax CreatePredefinedTypeSyntax(ExpressionSyntax expression, SyntaxKind keywordKind)
 {
     return SyntaxFactory.PredefinedType(SyntaxFactory.Token(expression.GetLeadingTrivia(), keywordKind, expression.GetTrailingTrivia()));
 }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:4,代码来源:ExpressionSyntaxExtensions.cs

示例3: FullyQualifyIdentifierName

            private ExpressionSyntax FullyQualifyIdentifierName(
                ISymbol symbol,
                ExpressionSyntax rewrittenNode,
                ExpressionSyntax originalNode,
                bool replaceNode,
                bool isInsideCref,
                bool omitLeftHandSide)
            {
                Debug.Assert(!replaceNode || rewrittenNode.Kind() == SyntaxKind.IdentifierName);

                //// TODO: use and expand Generate*Syntax(isymbol) to not depend on symbol display any more.
                //// See GenerateExpressionSyntax();

                var result = rewrittenNode;

                // only if this symbol has a containing type or namespace there is work for us to do.
                if (replaceNode || symbol.ContainingType != null || symbol.ContainingNamespace != null)
                {
                    ImmutableArray<SymbolDisplayPart> displayParts;

                    ExpressionSyntax left = null;

                    // we either need to create an AliasQualifiedName if the symbol is directly contained in the global namespace,
                    // otherwise it a QualifiedName.
                    if (!replaceNode && symbol.ContainingType == null && symbol.ContainingNamespace.IsGlobalNamespace)
                    {
                        return rewrittenNode.CopyAnnotationsTo(
                            SyntaxFactory.AliasQualifiedName(
                                SyntaxFactory.IdentifierName(SyntaxFactory.Token(SyntaxKind.GlobalKeyword)),
                                (SimpleNameSyntax)rewrittenNode.WithLeadingTrivia(null))
                                    .WithLeadingTrivia(rewrittenNode.GetLeadingTrivia()));
                    }

                    displayParts = replaceNode
                        ? symbol.ToDisplayParts(s_typeNameFormatWithGenerics)
                        : (symbol.ContainingType ?? (ISymbol)symbol.ContainingNamespace).ToDisplayParts(s_typeNameFormatWithGenerics);

                    rewrittenNode = TryAddTypeArgumentToIdentifierName(rewrittenNode, symbol);

                    // Replaces the '<' token with the '{' token since we are inside crefs
                    rewrittenNode = TryReplaceAngleBracesWithCurlyBraces(rewrittenNode, isInsideCref);
                    result = rewrittenNode;

                    if (!omitLeftHandSide)
                    {
                        left = SyntaxFactory.ParseTypeName(displayParts.ToDisplayString());

                        // Replaces the '<' token with the '{' token since we are inside crefs
                        left = TryReplaceAngleBracesWithCurlyBraces(left, isInsideCref);

                        if (replaceNode)
                        {
                            return left
                                .WithLeadingTrivia(rewrittenNode.GetLeadingTrivia())
                                .WithTrailingTrivia(rewrittenNode.GetTrailingTrivia());
                        }

                        // now create syntax for the combination of left and right syntax, or a simple replacement in case of an identifier
                        var parent = originalNode.Parent;
                        var leadingTrivia = rewrittenNode.GetLeadingTrivia();
                        rewrittenNode = rewrittenNode.WithLeadingTrivia(null);

                        switch (parent.Kind())
                        {
                            case SyntaxKind.QualifiedName:
                                var qualifiedParent = (QualifiedNameSyntax)parent;

                                result = rewrittenNode.CopyAnnotationsTo(
                                    SyntaxFactory.QualifiedName(
                                        (NameSyntax)left,
                                        (SimpleNameSyntax)rewrittenNode));

                                break;

                            case SyntaxKind.SimpleMemberAccessExpression:
                                var memberAccessParent = (MemberAccessExpressionSyntax)parent;

                                result = rewrittenNode.CopyAnnotationsTo(
                                    SyntaxFactory.MemberAccessExpression(
                                        SyntaxKind.SimpleMemberAccessExpression,
                                        left,
                                        (SimpleNameSyntax)rewrittenNode));

                                break;

                            default:
                                Debug.Assert(rewrittenNode is SimpleNameSyntax);

                                if (SyntaxFacts.IsInNamespaceOrTypeContext(originalNode))
                                {
                                    var right = (SimpleNameSyntax)rewrittenNode;
                                    result = rewrittenNode.CopyAnnotationsTo(SyntaxFactory.QualifiedName((NameSyntax)left, right.WithAdditionalAnnotations(Simplifier.SpecialTypeAnnotation)));
                                }
                                else if (originalNode.Parent is CrefSyntax)
                                {
                                    var right = (SimpleNameSyntax)rewrittenNode;
                                    result = rewrittenNode.CopyAnnotationsTo(SyntaxFactory.QualifiedName((NameSyntax)left, right));
                                }
                                else
                                {
//.........这里部分代码省略.........
开发者ID:RoryVL,项目名称:roslyn,代码行数:101,代码来源:CSharpSimplificationService.Expander.cs

示例4: TryAddTypeArgumentToIdentifierName

            private ExpressionSyntax TryAddTypeArgumentToIdentifierName(ExpressionSyntax newNode, ISymbol symbol)
            {
                if (newNode.Kind() == SyntaxKind.IdentifierName && symbol.Kind == SymbolKind.Method)
                {
                    if (((IMethodSymbol)symbol).TypeArguments.Length != 0)
                    {
                        var typeArguments = ((IMethodSymbol)symbol).TypeArguments;
                        if (!typeArguments.Any(t => t.ContainsAnonymousType()))
                        {
                            var genericName = SyntaxFactory.GenericName(
                                            ((IdentifierNameSyntax)newNode).Identifier,
                                            SyntaxFactory.TypeArgumentList(
                                                SyntaxFactory.SeparatedList(
                                                    typeArguments.Select(p => SyntaxFactory.ParseTypeName(p.ToDisplayParts(s_typeNameFormatWithGenerics).ToDisplayString())))))
                                            .WithLeadingTrivia(newNode.GetLeadingTrivia())
                                            .WithTrailingTrivia(newNode.GetTrailingTrivia())
                                            .WithAdditionalAnnotations(Simplifier.Annotation);

                            genericName = newNode.CopyAnnotationsTo(genericName);
                            return genericName;
                        }
                    }
                }

                return newNode;
            }
开发者ID:RoryVL,项目名称:roslyn,代码行数:26,代码来源:CSharpSimplificationService.Expander.cs

示例5: ConvertToAwaitExpression

        private static SyntaxNode ConvertToAwaitExpression(ExpressionSyntax expression)
        {
            if ((expression is BinaryExpressionSyntax || expression is ConditionalExpressionSyntax) && expression.HasTrailingTrivia)
            {
                var expWithTrailing = expression.WithoutLeadingTrivia();
                var span = expWithTrailing.GetLocation().GetLineSpan().Span;
                if (span.Start.Line == span.End.Line && !expWithTrailing.DescendantTrivia().Any(trivia => trivia.IsKind(SyntaxKind.SingleLineCommentTrivia)))
                {
                    return SyntaxFactory.AwaitExpression(SyntaxFactory.ParenthesizedExpression(expWithTrailing))
                                        .WithLeadingTrivia(expression.GetLeadingTrivia())
                                        .WithAdditionalAnnotations(Formatter.Annotation);
                }
            }

            return SyntaxFactory.AwaitExpression(expression.WithoutTrivia().Parenthesize())
                                .WithTriviaFrom(expression)
                                .WithAdditionalAnnotations(Simplifier.Annotation, Formatter.Annotation);
        }
开发者ID:jkotas,项目名称:roslyn,代码行数:18,代码来源:CSharpAddAwaitCodeFixProvider.cs

示例6: RewriteRoleExpression

        private ExpressionSyntax RewriteRoleExpression(ExpressionSyntax expression)
        {
            var isRole = IsRole(expression);
            if (isRole)
            {
                var fieldName = prefix + GetRoleName(expression);

                expression = (SimpleNameSyntax)Syntax.IdentifierName(fieldName).WithLeadingTrivia(expression.GetLeadingTrivia());
            }
            return expression;
        }
开发者ID:runefs,项目名称:Interact,代码行数:11,代码来源:ExpressionRewriter.cs


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