Skip to content

Commit 1ee52fd

Browse files
Fixup
1 parent 9c4ff22 commit 1ee52fd

File tree

6 files changed

+34
-16
lines changed

6 files changed

+34
-16
lines changed

src/Features/Core/Portable/InvertIf/AbstractInvertIfCodeRefactoringProvider.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.CodeAnalysis.LanguageService;
1717
using Microsoft.CodeAnalysis.PooledObjects;
1818
using Microsoft.CodeAnalysis.Shared.Extensions;
19+
using Microsoft.CodeAnalysis.Simplification;
1920
using Microsoft.CodeAnalysis.Text;
2021
using Roslyn.Utilities;
2122

@@ -183,7 +184,25 @@ private async Task<Document> InvertIfDirectiveAsync(
183184
new TextChange(trueSpan, text.ToString(falseSpan)),
184185
new TextChange(falseSpan, text.ToString(trueSpan)));
185186

186-
return document.WithText(newText);
187+
var updatedDocument = document.WithText(newText);
188+
return updatedDocument;
189+
//var updatedRoot = await updatedDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
190+
191+
//// SyntaxGenerator.Negate often adds parentheses around the expression. However, because we are making text
192+
//// changes, we lose the annotations to go and clean that up later.
193+
//var updatedIfDirective = updatedRoot.FindNode(ifDirective.FullSpan, findInsideTrivia: true);
194+
//if (updatedIfDirective != null)
195+
//{
196+
// var syntaxFacts = generator.SyntaxFacts;
197+
// var updatedIfDirectiveWithAnnotations = updatedIfDirective.ReplaceNodes(
198+
// updatedIfDirective.DescendantNodes().Where(n => n.RawKind == syntaxFacts.SyntaxKinds.ParenthesizedExpression),
199+
// (_, current) => current.WithAdditionalAnnotations(Simplifier.Annotation));
200+
// updatedRoot = updatedRoot.ReplaceNode(
201+
// updatedIfDirective,
202+
// updatedIfDirectiveWithAnnotations);
203+
//}
204+
205+
//return updatedDocument.WithSyntaxRoot(updatedRoot);
187206
}
188207

189208
private static bool HasErrorDiagnostics(SyntaxNode node)

src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,8 +3363,8 @@ public override SyntaxNode ElementAccessExpression(SyntaxNode expression, IEnume
33633363
internal override SyntaxToken NumericLiteralToken(string text, ulong value)
33643364
=> SyntaxFactory.Literal(text, value);
33653365

3366-
private static SyntaxNode Parenthesize(SyntaxNode expression, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true)
3367-
=> CSharpSyntaxGeneratorInternal.Parenthesize(expression, includeElasticTrivia, addSimplifierAnnotation);
3366+
private static SyntaxNode Parenthesize(SyntaxNode expression, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true, bool parenthesizeIdentifiers = true)
3367+
=> CSharpSyntaxGeneratorInternal.Parenthesize(expression, includeElasticTrivia, addSimplifierAnnotation, parenthesizeIdentifiers);
33683368

33693369
public override SyntaxNode IsTypeExpression(SyntaxNode expression, SyntaxNode type)
33703370
=> SyntaxFactory.BinaryExpression(SyntaxKind.IsExpression, (ExpressionSyntax)Parenthesize(expression), (TypeSyntax)type);
@@ -3436,7 +3436,7 @@ public override SyntaxNode LogicalOrExpression(SyntaxNode left, SyntaxNode right
34363436
=> CreateBinaryExpression(SyntaxKind.LogicalOrExpression, left, right);
34373437

34383438
public override SyntaxNode LogicalNotExpression(SyntaxNode expression)
3439-
=> SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, (ExpressionSyntax)Parenthesize(expression));
3439+
=> SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, (ExpressionSyntax)Parenthesize(expression, parenthesizeIdentifiers: false));
34403440

34413441
public override SyntaxNode ConditionalExpression(SyntaxNode condition, SyntaxNode whenTrue, SyntaxNode whenFalse)
34423442
=> SyntaxFactory.ConditionalExpression((ExpressionSyntax)Parenthesize(condition), (ExpressionSyntax)Parenthesize(whenTrue), (ExpressionSyntax)Parenthesize(whenFalse));

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Extensions/ExpressionSyntaxExtensions.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Extensions;
1616
internal static partial class ExpressionSyntaxExtensions
1717
{
1818
public static ExpressionSyntax Parenthesize(
19-
this ExpressionSyntax expression, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true)
19+
this ExpressionSyntax expression, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true, bool parenthesizeIdentifiers = true)
2020
{
2121
// a 'ref' expression should never be parenthesized. It fundamentally breaks the code.
2222
// This is because, from the language's perspective there is no such thing as a ref
@@ -28,9 +28,7 @@ public static ExpressionSyntax Parenthesize(
2828
// on a ref-expression node. But this node isn't a true expression that be operated
2929
// on like with everything else.
3030
if (expression.IsKind(SyntaxKind.RefExpression))
31-
{
3231
return expression;
33-
}
3432

3533
// Throw expressions are not permitted to be parenthesized:
3634
//
@@ -42,17 +40,18 @@ public static ExpressionSyntax Parenthesize(
4240
//
4341
// is not.
4442
if (expression.IsKind(SyntaxKind.ThrowExpression))
45-
{
4643
return expression;
47-
}
44+
45+
if (!parenthesizeIdentifiers && expression is IdentifierNameSyntax)
46+
return expression;
4847

4948
var result = ParenthesizeWorker(expression, includeElasticTrivia);
5049
return addSimplifierAnnotation
5150
? result.WithAdditionalAnnotations(Simplifier.Annotation)
5251
: result;
5352
}
5453

55-
private static ExpressionSyntax ParenthesizeWorker(
54+
private static ParenthesizedExpressionSyntax ParenthesizeWorker(
5655
this ExpressionSyntax expression, bool includeElasticTrivia)
5756
{
5857
var withoutTrivia = expression.WithoutTrivia();

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpSyntaxGeneratorInternal.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ public override SyntaxNode MemberBindingExpression(SyntaxNode name)
8383
public override SyntaxNode RefExpression(SyntaxNode expression)
8484
=> SyntaxFactory.RefExpression((ExpressionSyntax)expression);
8585

86-
public override SyntaxNode AddParentheses(SyntaxNode expressionOrPattern, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true)
87-
=> Parenthesize(expressionOrPattern, includeElasticTrivia, addSimplifierAnnotation);
86+
public override SyntaxNode AddParentheses(SyntaxNode expressionOrPattern, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true, bool parenthesizeIdentifiers = true)
87+
=> Parenthesize(expressionOrPattern, includeElasticTrivia, addSimplifierAnnotation, parenthesizeIdentifiers);
8888

89-
internal static SyntaxNode Parenthesize(SyntaxNode expressionOrPattern, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true)
89+
internal static SyntaxNode Parenthesize(SyntaxNode expressionOrPattern, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true, bool parenthesizeIdentifiers = true)
9090
=> expressionOrPattern switch
9191
{
92-
ExpressionSyntax expression => expression.Parenthesize(includeElasticTrivia, addSimplifierAnnotation),
92+
ExpressionSyntax expression => expression.Parenthesize(includeElasticTrivia, addSimplifierAnnotation, parenthesizeIdentifiers),
9393
PatternSyntax pattern => pattern.Parenthesize(includeElasticTrivia, addSimplifierAnnotation),
9494
var other => other,
9595
};

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/SyntaxGeneratorInternalExtensions/SyntaxGeneratorInternal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public SyntaxNode LocalDeclarationStatement(SyntaxToken name, SyntaxNode initial
5858
/// <summary>
5959
/// Wraps with parens.
6060
/// </summary>
61-
public abstract SyntaxNode AddParentheses(SyntaxNode expression, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true);
61+
public abstract SyntaxNode AddParentheses(SyntaxNode expression, bool includeElasticTrivia = true, bool addSimplifierAnnotation = true, bool parenthesizeIdentifiers = true);
6262

6363
/// <summary>
6464
/// Creates a statement that can be used to yield a value from an iterator method.

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicSyntaxGeneratorInternal.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
9797
Return expression
9898
End Function
9999

100-
Public Overrides Function AddParentheses(expression As SyntaxNode, Optional includeElasticTrivia As Boolean = True, Optional addSimplifierAnnotation As Boolean = True) As SyntaxNode
100+
Public Overrides Function AddParentheses(expression As SyntaxNode, Optional includeElasticTrivia As Boolean = True, Optional addSimplifierAnnotation As Boolean = True, Optional parenthesizeIdentifiers As Boolean = True) As SyntaxNode
101101
Return Parenthesize(expression, addSimplifierAnnotation)
102102
End Function
103103

0 commit comments

Comments
 (0)