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


C# SyntaxToken.GetPreviousTokenIfTouchingWord方法代码示例

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


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

示例1: IsParameterTypeContext

        public static bool IsParameterTypeContext(this SyntaxTree syntaxTree, int position, SyntaxToken tokenOnLeftOfPosition, CancellationToken cancellationToken)
        {
            var token = tokenOnLeftOfPosition.GetPreviousTokenIfTouchingWord(position);

            if (token.Kind() == SyntaxKind.RefKeyword ||
                token.Kind() == SyntaxKind.OutKeyword ||
                token.Kind() == SyntaxKind.ParamsKeyword ||
                token.Kind() == SyntaxKind.ThisKeyword)
            {
                position = token.SpanStart;
                tokenOnLeftOfPosition = syntaxTree.FindTokenOnLeftOfPosition(position, cancellationToken);
            }

            if (syntaxTree.IsParameterModifierContext(position, tokenOnLeftOfPosition, cancellationToken))
            {
                return true;
            }

            // int this[ |
            // int this[int i, |
            if (token.Kind() == SyntaxKind.OpenParenToken ||
                token.Kind() == SyntaxKind.OpenBracketToken ||
                token.Kind() == SyntaxKind.CommaToken)
            {
                if (token.Parent.IsKind(SyntaxKind.ParameterList, SyntaxKind.BracketedParameterList))
                {
                    return true;
                }
            }

            return false;
        }
开发者ID:GeertVL,项目名称:roslyn,代码行数:32,代码来源:SyntaxTreeExtensions.cs

示例2: IsExpressionContext

        public static bool IsExpressionContext(
            this SyntaxTree syntaxTree,
            int position,
            SyntaxToken tokenOnLeftOfPosition,
            bool attributes,
            CancellationToken cancellationToken,
            SemanticModel semanticModelOpt = null)
        {
            // cases:
            //   var q = |
            //   var q = a|
            // this list is *not* exhaustive.

            var token = tokenOnLeftOfPosition.GetPreviousTokenIfTouchingWord(position);

            if (token.GetAncestor<ConditionalDirectiveTriviaSyntax>() != null)
            {
                return false;
            }

            if (!attributes)
            {
                if (token.GetAncestor<AttributeListSyntax>() != null)
                {
                    return false;
                }
            }

            if (syntaxTree.IsConstantExpressionContext(position, tokenOnLeftOfPosition, cancellationToken))
            {
                return true;
            }

            // no expressions after .   ::   ->
            if (token.Kind() == SyntaxKind.DotToken ||
                token.Kind() == SyntaxKind.ColonColonToken ||
                token.Kind() == SyntaxKind.MinusGreaterThanToken)
            {
                return false;
            }

            // Normally you can have any sort of expression after an equals. However, this does not
            // apply to a "using Foo = ..." situation.
            if (token.Kind() == SyntaxKind.EqualsToken)
            {
                if (token.Parent.IsKind(SyntaxKind.NameEquals) &&
                    token.Parent.IsParentKind(SyntaxKind.UsingDirective))
                {
                    return false;
                }
            }

            // q = |
            // q -= |
            // q *= |
            // q += |
            // q /= |
            // q ^= |
            // q %= |
            // q &= |
            // q |= |
            // q <<= |
            // q >>= |
            if (token.Kind() == SyntaxKind.EqualsToken ||
                token.Kind() == SyntaxKind.MinusEqualsToken ||
                token.Kind() == SyntaxKind.AsteriskEqualsToken ||
                token.Kind() == SyntaxKind.PlusEqualsToken ||
                token.Kind() == SyntaxKind.SlashEqualsToken ||
                token.Kind() == SyntaxKind.ExclamationEqualsToken ||
                token.Kind() == SyntaxKind.CaretEqualsToken ||
                token.Kind() == SyntaxKind.AmpersandEqualsToken ||
                token.Kind() == SyntaxKind.BarEqualsToken ||
                token.Kind() == SyntaxKind.PercentEqualsToken ||
                token.Kind() == SyntaxKind.LessThanLessThanEqualsToken ||
                token.Kind() == SyntaxKind.GreaterThanGreaterThanEqualsToken)
            {
                return true;
            }

            // ( |
            if (token.Kind() == SyntaxKind.OpenParenToken &&
                token.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
            {
                return true;
            }

            // - |
            // + |
            // ~ |
            // ! |
            if (token.Parent is PrefixUnaryExpressionSyntax)
            {
                var prefix = token.Parent as PrefixUnaryExpressionSyntax;
                return prefix.OperatorToken == token;
            }

            // not sure about these:
            //   ++ |
            //   -- |
#if false
//.........这里部分代码省略.........
开发者ID:GeertVL,项目名称:roslyn,代码行数:101,代码来源:SyntaxTreeExtensions.cs

示例3: IsSizeOfExpressionContext

        public static bool IsSizeOfExpressionContext(
            this SyntaxTree syntaxTree, int position, SyntaxToken tokenOnLeftOfPosition, CancellationToken cancellationToken)
        {
            var token = tokenOnLeftOfPosition.GetPreviousTokenIfTouchingWord(position);

            if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.IsKind(SyntaxKind.SizeOfExpression))
            {
                return true;
            }

            return false;
        }
开发者ID:GeertVL,项目名称:roslyn,代码行数:12,代码来源:SyntaxTreeExtensions.cs

示例4: IsConstantExpressionContext

        public static bool IsConstantExpressionContext(this SyntaxTree syntaxTree, int position, SyntaxToken tokenOnLeftOfPosition, CancellationToken cancellationToken)
        {
            var token = tokenOnLeftOfPosition.GetPreviousTokenIfTouchingWord(position);

            // case |
            if (token.Kind() == SyntaxKind.CaseKeyword &&
                token.Parent.IsKind(SyntaxKind.CaseSwitchLabel))
            {
                return true;
            }

            // goto case |
            if (token.Kind() == SyntaxKind.CaseKeyword &&
                token.Parent.IsKind(SyntaxKind.GotoCaseStatement))
            {
                return true;
            }

            if (token.Kind() == SyntaxKind.EqualsToken &&
                token.Parent.IsKind(SyntaxKind.EqualsValueClause))
            {
                var equalsValue = (EqualsValueClauseSyntax)token.Parent;

                if (equalsValue.IsParentKind(SyntaxKind.VariableDeclarator) &&
                    equalsValue.Parent.IsParentKind(SyntaxKind.VariableDeclaration))
                {
                    // class C { const int i = |
                    var fieldDeclaration = equalsValue.GetAncestor<FieldDeclarationSyntax>();
                    if (fieldDeclaration != null)
                    {
                        return fieldDeclaration.Modifiers.Any(SyntaxKind.ConstKeyword);
                    }

                    // void M() { const int i = |
                    var localDeclaration = equalsValue.GetAncestor<LocalDeclarationStatementSyntax>();
                    if (localDeclaration != null)
                    {
                        return localDeclaration.Modifiers.Any(SyntaxKind.ConstKeyword);
                    }
                }

                // enum E { A = |
                if (equalsValue.IsParentKind(SyntaxKind.EnumMemberDeclaration))
                {
                    return true;
                }

                // void M(int i = |
                if (equalsValue.IsParentKind(SyntaxKind.Parameter))
                {
                    return true;
                }
            }

            // [Foo( |
            // [Foo(x, |
            if (token.Parent.IsKind(SyntaxKind.AttributeArgumentList) &&
               (token.Kind() == SyntaxKind.CommaToken ||
                token.Kind() == SyntaxKind.OpenParenToken))
            {
                return true;
            }

            // [Foo(x: |
            if (token.Kind() == SyntaxKind.ColonToken &&
                token.Parent.IsKind(SyntaxKind.NameColon) &&
                token.Parent.IsParentKind(SyntaxKind.AttributeArgument))
            {
                return true;
            }

            // [Foo(X = |
            if (token.Kind() == SyntaxKind.EqualsToken &&
                token.Parent.IsKind(SyntaxKind.NameEquals) &&
                token.Parent.IsParentKind(SyntaxKind.AttributeArgument))
            {
                return true;
            }

            // TODO: Fixed-size buffer declarations

            return false;
        }
开发者ID:GeertVL,项目名称:roslyn,代码行数:83,代码来源:SyntaxTreeExtensions.cs

示例5: IsPossibleCastTypeContext

        public static bool IsPossibleCastTypeContext(
            this SyntaxTree syntaxTree, int position, SyntaxToken tokenOnLeftOfPosition, CancellationToken cancellationToken)
        {
            var token = tokenOnLeftOfPosition.GetPreviousTokenIfTouchingWord(position);

            if (CodeAnalysis.CSharpExtensions.IsKind(token, SyntaxKind.OpenParenToken) &&
                syntaxTree.IsExpressionContext(token.SpanStart, syntaxTree.FindTokenOnLeftOfPosition(token.SpanStart, cancellationToken), false, cancellationToken))
            {
                return true;
            }

            return false;
        }
开发者ID:GeertVL,项目名称:roslyn,代码行数:13,代码来源:SyntaxTreeExtensions.cs

示例6: IsObjectCreationTypeContext

        public static bool IsObjectCreationTypeContext(
            this SyntaxTree syntaxTree, int position, SyntaxToken tokenOnLeftOfPosition, CancellationToken cancellationToken)
        {
            var token = tokenOnLeftOfPosition.GetPreviousTokenIfTouchingWord(position);

            if (token.Kind() == SyntaxKind.NewKeyword)
            {
                // we can follow a 'new' if it's the 'new' for an expression.
                var start = token.SpanStart;
                var tokenOnLeftOfStart = syntaxTree.FindTokenOnLeftOfPosition(start, cancellationToken);
                return
                    IsNonConstantExpressionContext(syntaxTree, token.SpanStart, tokenOnLeftOfStart, cancellationToken) ||
                    syntaxTree.IsStatementContext(token.SpanStart, tokenOnLeftOfStart, cancellationToken) ||
                    syntaxTree.IsGlobalStatementContext(token.SpanStart, cancellationToken);
            }

            return false;
        }
开发者ID:GeertVL,项目名称:roslyn,代码行数:18,代码来源:SyntaxTreeExtensions.cs

示例7: IsPossibleTupleContext

        // Tuple literals aren't recognized by the parser until there is a comma
        // So a parenthesized expression is a possible tuple context too
        public static bool IsPossibleTupleContext(this SyntaxTree syntaxTree,
            SyntaxToken tokenOnLeftOfPosition, int position)
        {
            tokenOnLeftOfPosition = tokenOnLeftOfPosition.GetPreviousTokenIfTouchingWord(position);

            if (tokenOnLeftOfPosition.IsKind(SyntaxKind.OpenParenToken))
            {
                return tokenOnLeftOfPosition.Parent.IsKind(SyntaxKind.ParenthesizedExpression,
                    SyntaxKind.TupleExpression, SyntaxKind.TupleType);
            }

            return tokenOnLeftOfPosition.IsKind(SyntaxKind.CommaToken) &&
                tokenOnLeftOfPosition.Parent.IsKind(SyntaxKind.TupleExpression, SyntaxKind.TupleType);
        }
开发者ID:jkotas,项目名称:roslyn,代码行数:16,代码来源:SyntaxTreeExtensions.cs

示例8: IsPossibleTupleContext

        /// <summary>
        /// Are you possibly typing a tuple type or expression?
        /// This is used to suppress colon as a completion trigger (so that you can type element names).
        /// This is also used to recommend some keywords (like var).
        /// </summary>
        public static bool IsPossibleTupleContext(this SyntaxTree syntaxTree, SyntaxToken leftToken, int position)
        {
            leftToken = leftToken.GetPreviousTokenIfTouchingWord(position);

            // ($$
            // (a, $$
            if (IsPossibleTupleOpenParenOrComma(leftToken))
            {
                return true;
            }

            // ((a, b) $$
            // (..., (a, b) $$
            if (leftToken.IsKind(SyntaxKind.CloseParenToken))
            {
                if (leftToken.Parent.IsKind(
                        SyntaxKind.ParenthesizedExpression,
                        SyntaxKind.TupleExpression,
                        SyntaxKind.TupleType))
                {
                    var possibleCommaOrParen = FindTokenOnLeftOfNode(leftToken.Parent);
                    if (IsPossibleTupleOpenParenOrComma(possibleCommaOrParen))
                    {
                        return true;
                    }
                }
            }

            // (a $$
            // (..., b $$
            if (leftToken.IsKind(SyntaxKind.IdentifierToken))
            {
                var possibleCommaOrParen = FindTokenOnLeftOfNode(leftToken.Parent);
                if (IsPossibleTupleOpenParenOrComma(possibleCommaOrParen))
                {
                    return true;
                }
            }

            // (a.b $$
            // (..., a.b $$
            if (leftToken.IsKind(SyntaxKind.IdentifierToken) &&
                leftToken.Parent.IsKind(SyntaxKind.IdentifierName) &&
                leftToken.Parent.IsParentKind(SyntaxKind.QualifiedName, SyntaxKind.SimpleMemberAccessExpression))
            {
                var possibleCommaOrParen = FindTokenOnLeftOfNode(leftToken.Parent.Parent);
                if (IsPossibleTupleOpenParenOrComma(possibleCommaOrParen))
                {
                    return true;
                }
            }

            return false;
        }
开发者ID:XieShuquan,项目名称:roslyn,代码行数:59,代码来源:SyntaxTreeExtensions.cs


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