public override SyntaxNode VisitParenthesizedPattern(ParenthesizedPatternSyntax node) { return(SimplifyExpression( node, newNode: base.VisitParenthesizedPattern(node), simplifier: s_simplifyParentheses)); }
public static Doc Print(ParenthesizedPatternSyntax node) { return(Doc.Concat( Token.Print(node.OpenParenToken), Node.Print(node.Pattern), Token.Print(node.CloseParenToken) )); }
private Doc PrintParenthesizedPatternSyntax( ParenthesizedPatternSyntax node) { return(Concat( this.PrintSyntaxToken(node.OpenParenToken), this.Print(node.Pattern), this.PrintSyntaxToken(node.CloseParenToken) )); }
private static SyntaxNode SimplifyParentheses( ParenthesizedPatternSyntax node, SemanticModel semanticModel, OptionSet optionSet, CancellationToken cancellationToken) { if (node.CanRemoveParentheses(semanticModel)) { var resultNode = CSharpSyntaxFacts.Instance.Unparenthesize(node); return(SimplificationHelpers.CopyAnnotations(from: node, to: resultNode)); } // We don't know how to simplify this. return(node); }
private static bool RemovalChangesAssociation( ParenthesizedPatternSyntax node, PatternSyntax parentPattern) { var pattern = node.Pattern; var precedence = pattern.GetOperatorPrecedence(); var parentPrecedence = parentPattern.GetOperatorPrecedence(); if (precedence == OperatorPrecedence.None || parentPrecedence == OperatorPrecedence.None) { // Be conservative if the expression or its parent has no precedence. return(true); } // Association always changes if the expression's precedence is lower that its parent. return(precedence < parentPrecedence); }
public static bool CanRemoveParentheses(this ParenthesizedPatternSyntax node) { if (node.OpenParenToken.IsMissing || node.CloseParenToken.IsMissing) { // int x = (3; return(false); } var pattern = node.Pattern; // We wrap a parenthesized pattern and we're parenthesized. We can remove our parens. if (pattern is ParenthesizedPatternSyntax) { return(true); } // (not ...) -> not ... // // this is safe because unary patterns have the highest precedence, so even if you had: // (not ...) or (not ...) // // you can safely convert to `not ... or not ...` var patternPrecedence = pattern.GetOperatorPrecedence(); if (patternPrecedence == OperatorPrecedence.Primary || patternPrecedence == OperatorPrecedence.Unary) { return(true); } // We're parenthesized and are inside a parenthesized pattern. We can remove our parens. // ((x)) -> (x) if (node.Parent is ParenthesizedPatternSyntax) { return(true); } // x is (...) -> x is ... if (node.Parent is IsPatternExpressionSyntax) { return(true); } // (x or y) => ... -> x or y => ... if (node.Parent is SwitchExpressionArmSyntax) { return(true); } // X: (y or z) -> X: y or z if (node.Parent is SubpatternSyntax) { return(true); } // case (x or y): -> case x or y: if (node.Parent is CasePatternSwitchLabelSyntax) { return(true); } // Operator precedence cases: // - If the parent is not an expression, do not remove parentheses // - Otherwise, parentheses may be removed if doing so does not change operator associations. return(node.Parent is PatternSyntax patternParent && !RemovalChangesAssociation(node, patternParent)); }
public override void VisitParenthesizedPattern(ParenthesizedPatternSyntax node) { Log(node, "Unsupported Syntax !"); }