/
PostfixUnaryExpressionRefactoring.cs
144 lines (120 loc) · 5.93 KB
/
PostfixUnaryExpressionRefactoring.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Roslynator.CSharp.CSharpFactory;
namespace Roslynator.CSharp.Refactorings;
internal static class PostfixUnaryExpressionRefactoring
{
public static void ComputeRefactorings(RefactoringContext context, PostfixUnaryExpressionSyntax postfixUnaryExpression)
{
if (!context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(postfixUnaryExpression.OperatorToken))
return;
switch (postfixUnaryExpression.Kind())
{
case SyntaxKind.PostIncrementExpression:
{
InvertPostIncrement(context, postfixUnaryExpression);
ReplacePostIncrementWithPreIncrement(context, postfixUnaryExpression);
break;
}
case SyntaxKind.PostDecrementExpression:
{
InvertPostDecrement(context, postfixUnaryExpression);
ReplacePostDecrementWithPreDecrement(context, postfixUnaryExpression);
break;
}
}
}
private static void ReplacePostIncrementWithPreIncrement(RefactoringContext context, PostfixUnaryExpressionSyntax postIncrement)
{
if (!context.IsRefactoringEnabled(RefactoringDescriptors.ReplacePrefixOperatorWithPostfixOperator))
return;
ExpressionSyntax operand = postIncrement.Operand;
if (operand is null)
return;
PrefixUnaryExpressionSyntax preIncrement = PreIncrementExpression(operand.WithoutTrivia())
.WithTriviaFrom(postIncrement)
.WithFormatterAnnotation();
context.RegisterRefactoring(
$"Replace '{postIncrement}' with '{preIncrement}'",
ct => ChangePostIncrementToPreIncrementAsync(context.Document, postIncrement, preIncrement, ct),
RefactoringDescriptors.ReplacePrefixOperatorWithPostfixOperator);
}
private static void InvertPostIncrement(RefactoringContext context, PostfixUnaryExpressionSyntax postIncrement)
{
if (!context.IsRefactoringEnabled(RefactoringDescriptors.InvertPrefixOrPostfixUnaryOperator))
return;
PostfixUnaryExpressionSyntax postDecrement = postIncrement
.WithOperatorToken(Token(SyntaxKind.MinusMinusToken))
.WithTriviaFrom(postIncrement)
.WithFormatterAnnotation();
context.RegisterRefactoring(
$"Invert {postIncrement.OperatorToken}",
ct => ChangePostIncrementToPostDecrementAsync(context.Document, postIncrement, postDecrement, ct),
RefactoringDescriptors.InvertPrefixOrPostfixUnaryOperator);
}
private static void ReplacePostDecrementWithPreDecrement(RefactoringContext context, PostfixUnaryExpressionSyntax postDecrement)
{
if (!context.IsRefactoringEnabled(RefactoringDescriptors.ReplacePrefixOperatorWithPostfixOperator))
return;
ExpressionSyntax operand = postDecrement.Operand;
if (operand is null)
return;
PrefixUnaryExpressionSyntax preDecrement = PreDecrementExpression(operand.WithoutTrivia())
.WithTriviaFrom(postDecrement)
.WithFormatterAnnotation();
context.RegisterRefactoring(
$"Replace '{postDecrement}' with '{preDecrement}'",
ct => ChangePostDecrementToPreDecrementAsync(context.Document, postDecrement, preDecrement, ct),
RefactoringDescriptors.ReplacePrefixOperatorWithPostfixOperator);
}
private static void InvertPostDecrement(RefactoringContext context, PostfixUnaryExpressionSyntax postDecrement)
{
if (!context.IsRefactoringEnabled(RefactoringDescriptors.InvertPrefixOrPostfixUnaryOperator))
return;
PostfixUnaryExpressionSyntax postIncrement = postDecrement
.WithOperatorToken(Token(SyntaxKind.PlusPlusToken))
.WithTriviaFrom(postDecrement)
.WithFormatterAnnotation();
context.RegisterRefactoring(
$"Invert {postDecrement.OperatorToken}",
ct => ChangePostDecrementToPostIncrementAsync(context.Document, postDecrement, postIncrement, ct),
RefactoringDescriptors.InvertPrefixOrPostfixUnaryOperator);
}
private static Task<Document> ChangePostIncrementToPreIncrementAsync(
Document document,
PostfixUnaryExpressionSyntax postIncrement,
PrefixUnaryExpressionSyntax preIncrement,
CancellationToken cancellationToken = default)
{
return document.ReplaceNodeAsync(postIncrement, preIncrement, cancellationToken);
}
private static Task<Document> ChangePostIncrementToPostDecrementAsync(
Document document,
PostfixUnaryExpressionSyntax postIncrement,
PostfixUnaryExpressionSyntax postDecrement,
CancellationToken cancellationToken = default)
{
return document.ReplaceNodeAsync(postIncrement, postDecrement, cancellationToken);
}
private static Task<Document> ChangePostDecrementToPreDecrementAsync(
Document document,
PostfixUnaryExpressionSyntax postDecrement,
PrefixUnaryExpressionSyntax preDecrement,
CancellationToken cancellationToken = default)
{
return document.ReplaceNodeAsync(postDecrement, preDecrement, cancellationToken);
}
private static Task<Document> ChangePostDecrementToPostIncrementAsync(
Document document,
PostfixUnaryExpressionSyntax postDecrement,
PostfixUnaryExpressionSyntax postIncrement,
CancellationToken cancellationToken = default)
{
return document.ReplaceNodeAsync(postDecrement, postIncrement, cancellationToken);
}
}