Пример #1
0
        public static SyntaxNode Unparenthesize(
            this ISyntaxFacts syntaxFacts, SyntaxNode node)
        {
            SyntaxToken openParenToken;
            SyntaxNode  operand;
            SyntaxToken closeParenToken;

            if (syntaxFacts.IsParenthesizedPattern(node))
            {
                syntaxFacts.GetPartsOfParenthesizedPattern(node,
                                                           out openParenToken, out operand, out closeParenToken);
            }
            else
            {
                syntaxFacts.GetPartsOfParenthesizedExpression(node,
                                                              out openParenToken, out operand, out closeParenToken);
            }

            var leadingTrivia = openParenToken.LeadingTrivia
                                .Concat(openParenToken.TrailingTrivia)
                                .Where(t => !syntaxFacts.IsElastic(t))
                                .Concat(operand.GetLeadingTrivia());

            var trailingTrivia = operand.GetTrailingTrivia()
                                 .Concat(closeParenToken.LeadingTrivia)
                                 .Where(t => !syntaxFacts.IsElastic(t))
                                 .Concat(closeParenToken.TrailingTrivia);

            var resultNode = operand
                             .WithLeadingTrivia(leadingTrivia)
                             .WithTrailingTrivia(trailingTrivia);

            // If there's no trivia between the original node and the tokens around it, then add
            // elastic markers so the formatting engine will spaces if necessary to keep things
            // parseable.
            if (resultNode.GetLeadingTrivia().Count == 0)
            {
                var previousToken = node.GetFirstToken().GetPreviousToken();
                if (previousToken.TrailingTrivia.Count == 0 &&
                    syntaxFacts.IsWordOrNumber(previousToken) &&
                    syntaxFacts.IsWordOrNumber(resultNode.GetFirstToken()))
                {
                    resultNode = resultNode.WithPrependedLeadingTrivia(syntaxFacts.ElasticMarker);
                }
            }

            if (resultNode.GetTrailingTrivia().Count == 0)
            {
                var nextToken = node.GetLastToken().GetNextToken();
                if (nextToken.LeadingTrivia.Count == 0 &&
                    syntaxFacts.IsWordOrNumber(nextToken) &&
                    syntaxFacts.IsWordOrNumber(resultNode.GetLastToken()))
                {
                    resultNode = resultNode.WithAppendedTrailingTrivia(syntaxFacts.ElasticMarker);
                }
            }

            return(resultNode);
        }
Пример #2
0
 public static SyntaxNode GetPatternOfParenthesizedPattern(
     this ISyntaxFacts syntaxFacts,
     SyntaxNode node
     )
 {
     syntaxFacts.GetPartsOfParenthesizedPattern(node, out _, out var pattern, out _);
     return(pattern);
 }
        private static bool IsLegalPattern(
            ISyntaxFacts syntaxFacts,
            SyntaxNode pattern,
            bool designatorsLegal
            )
        {
            // It is illegal to create a pattern that has a designator under a not-pattern or or-pattern
            if (syntaxFacts.IsBinaryPattern(pattern))
            {
                syntaxFacts.GetPartsOfBinaryPattern(pattern, out var left, out _, out var right);
                designatorsLegal = designatorsLegal && !syntaxFacts.IsOrPattern(pattern);
                return(IsLegalPattern(syntaxFacts, left, designatorsLegal) &&
                       IsLegalPattern(syntaxFacts, right, designatorsLegal));
            }

            if (syntaxFacts.IsNotPattern(pattern))
            {
                syntaxFacts.GetPartsOfUnaryPattern(pattern, out _, out var subPattern);
                return(IsLegalPattern(syntaxFacts, subPattern, designatorsLegal: false));
            }

            if (syntaxFacts.IsParenthesizedPattern(pattern))
            {
                syntaxFacts.GetPartsOfParenthesizedPattern(
                    pattern,
                    out _,
                    out var subPattern,
                    out _
                    );
                return(IsLegalPattern(syntaxFacts, subPattern, designatorsLegal));
            }

            if (syntaxFacts.IsDeclarationPattern(pattern))
            {
                syntaxFacts.GetPartsOfDeclarationPattern(pattern, out _, out var designator);
                return(designator == null || designatorsLegal);
            }

            if (syntaxFacts.IsRecursivePattern(pattern))
            {
                syntaxFacts.GetPartsOfRecursivePattern(
                    pattern,
                    out _,
                    out _,
                    out _,
                    out var designator
                    );
                return(designator == null || designatorsLegal);
            }

            if (syntaxFacts.IsVarPattern(pattern))
            {
                return(designatorsLegal);
            }

            return(true);
        }
        private static bool IsLegalPattern(ISyntaxFacts syntaxFacts, SyntaxNode pattern, bool designatorsLegal)
        {
            // It is illegal to create a pattern that has a designator under a not-pattern or or-pattern
            if (syntaxFacts.IsBinaryPattern(pattern))
            {
                syntaxFacts.GetPartsOfBinaryPattern(pattern, out var left, out _, out var right);
                designatorsLegal = designatorsLegal && !syntaxFacts.IsOrPattern(pattern);
                return(IsLegalPattern(syntaxFacts, left, designatorsLegal) &&
                       IsLegalPattern(syntaxFacts, right, designatorsLegal));
            }

            if (syntaxFacts.IsNotPattern(pattern))
            {
                // it's fine to have `not string s` (or `not (string s)`) as long as we're currently in a location where
                // designators are legal themselves.
                syntaxFacts.GetPartsOfUnaryPattern(pattern, out _, out var subPattern);
                if (syntaxFacts.IsParenthesizedPattern(subPattern))
                {
                    subPattern = syntaxFacts.GetPatternOfParenthesizedPattern(subPattern);
                }

                if (syntaxFacts.IsDeclarationPattern(subPattern))
                {
                    return(designatorsLegal);
                }

                return(IsLegalPattern(syntaxFacts, subPattern, designatorsLegal: false));
            }

            if (syntaxFacts.IsParenthesizedPattern(pattern))
            {
                syntaxFacts.GetPartsOfParenthesizedPattern(pattern, out _, out var subPattern, out _);
                return(IsLegalPattern(syntaxFacts, subPattern, designatorsLegal));
            }

            if (syntaxFacts.IsDeclarationPattern(pattern))
            {
                syntaxFacts.GetPartsOfDeclarationPattern(pattern, out _, out var designator);
                return(designator == null || designatorsLegal);
            }

            if (syntaxFacts.IsRecursivePattern(pattern))
            {
                syntaxFacts.GetPartsOfRecursivePattern(pattern, out _, out _, out _, out var designator);
                return(designator == null || designatorsLegal);
            }

            if (syntaxFacts.IsVarPattern(pattern))
            {
                return(designatorsLegal);
            }

            return(true);
        }