2
// ConvertConditionalToIfAction.cs
5
// Mansheng Yang <lightyang0@gmail.com>
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
[ContextAction ("Convert '?:' to 'if'", Description = "Convert '?:' operator to 'if' statement.")]
34
public class ConvertConditionalToIfAction : ICodeActionProvider
36
public IEnumerable<CodeAction> GetActions (RefactoringContext context)
39
var returnStatement = context.GetNode<ReturnStatement> ();
40
if (returnStatement != null) {
41
var action = GetAction (context, returnStatement, returnStatement.Expression,
42
expr => new Statement[]
44
new IfElseStatement (expr.Condition.Clone (),
45
new ReturnStatement (expr.TrueExpression.Clone ()),
46
new ReturnStatement (expr.FalseExpression.Clone ()))
54
var assignment = context.GetNode<AssignmentExpression> ();
55
if (assignment != null) {
56
var statement = assignment.Parent as ExpressionStatement;
57
if (statement == null)
60
// statement is in for initializer/iterator
61
if (statement.Parent is ForStatement &&
62
statement != ((ForStatement)statement.Parent).EmbeddedStatement)
65
// statement is in using resource-acquisition
66
if (statement.Parent is UsingStatement &&
67
statement != ((UsingStatement)statement.Parent).EmbeddedStatement)
70
var action = GetAction (context, statement, assignment.Right,
71
expr => new Statement [] { ConvertAssignment (assignment.Left, assignment.Operator, expr) });
78
// variable initializer
79
var initializer = context.GetNode<VariableInitializer> ();
80
if (initializer != null && initializer.Parent is VariableDeclarationStatement &&
81
initializer.Parent.Parent is BlockStatement) {
83
var variableDecl = (VariableDeclarationStatement)initializer.Parent;
84
var newVariableDecl = (VariableDeclarationStatement)variableDecl.Clone ();
85
foreach (var variable in newVariableDecl.Variables) {
86
if (variable.Name == initializer.Name)
87
variable.Initializer = Expression.Null;
90
var action = GetAction (context, variableDecl, initializer.Initializer,
91
expr => new Statement []
94
ConvertAssignment (new IdentifierExpression (initializer.Name), AssignmentOperatorType.Assign,
102
CodeAction GetAction (RefactoringContext context, Statement originalStatement, Expression conditionalCandidate,
103
Func<ConditionalExpression, IEnumerable<Statement>> getReplacement)
105
var conditionalExpr = GetConditionalExpression (conditionalCandidate);
106
if (conditionalExpr == null ||
107
!(conditionalExpr.QuestionMarkToken.Contains(context.Location) ||
108
conditionalExpr.ColonToken.Contains(context.Location)))
111
return new CodeAction (context.TranslateString ("Convert '?:' to 'if'"),
114
foreach (var node in getReplacement (conditionalExpr))
115
script.InsertBefore (originalStatement, node);
116
script.Remove (originalStatement);
120
static ConditionalExpression GetConditionalExpression (Expression expr)
122
while (expr != null) {
123
if (expr is ConditionalExpression)
124
return (ConditionalExpression)expr;
126
var parenthesizedExpr = expr as ParenthesizedExpression;
127
if (parenthesizedExpr == null)
129
expr = parenthesizedExpr.Expression;
134
static IfElseStatement ConvertAssignment (Expression target, AssignmentOperatorType op,
135
ConditionalExpression conditionalExpr)
137
var trueAssignment = new AssignmentExpression (target.Clone (), op,
138
conditionalExpr.TrueExpression.Clone ());
139
var falseAssignment = new AssignmentExpression (target.Clone (), op,
140
conditionalExpr.FalseExpression.Clone ());
141
return new IfElseStatement (conditionalExpr.Condition.Clone (),
142
new ExpressionStatement (trueAssignment),
143
new ExpressionStatement (falseAssignment));