2
// ConvertIfToConditionalAction.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
29
using ICSharpCode.NRefactory.PatternMatching;
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
[ContextAction ("Convert 'if' to '?:'", Description = "Convert 'if' statement to '?:' operator.")]
34
public class ConvertIfToConditionalAction : SpecializedCodeAction <IfElseStatement>
36
static readonly InsertParenthesesVisitor insertParenthesesVisitor = new InsertParenthesesVisitor ();
37
protected override CodeAction GetAction (RefactoringContext context, IfElseStatement ifElseStatement)
39
if (!ifElseStatement.IfToken.Contains (context.Location))
41
var convertAssignment = GetConvertAssignmentAction (context, ifElseStatement);
42
if (convertAssignment != null)
43
return convertAssignment;
44
var convertReturn = GetConvertReturnAction (context, ifElseStatement);
48
CodeAction GetConvertAssignmentAction (RefactoringContext context, IfElseStatement ifElseStatement)
50
Func<Statement, AssignmentExpression> extractAssignment =
53
var exprStatement = node as ExpressionStatement;
54
if (exprStatement != null)
55
return exprStatement.Expression as AssignmentExpression;
58
return GetAction (context, ifElseStatement, extractAssignment,
59
(assignment1, assignment2) =>
61
if (assignment1.Operator != assignment2.Operator || !assignment1.Left.Match (assignment2.Left).Success)
63
var conditionalExpr = new ConditionalExpression (ifElseStatement.Condition.Clone (),
64
assignment1.Right.Clone (),
65
assignment2.Right.Clone ());
66
conditionalExpr.AcceptVisitor (insertParenthesesVisitor);
68
var assignment = new AssignmentExpression (assignment1.Left.Clone (), assignment1.Operator,
70
return new ExpressionStatement (assignment);
74
CodeAction GetConvertReturnAction (RefactoringContext context, IfElseStatement ifElseStatement)
76
Func<Statement, ReturnStatement> extractReturn = node => node as ReturnStatement;
77
return GetAction (context, ifElseStatement, extractReturn,
80
var conditionalExpr = new ConditionalExpression (ifElseStatement.Condition.Clone (),
81
return1.Expression.Clone (),
82
return2.Expression.Clone ());
83
conditionalExpr.AcceptVisitor (insertParenthesesVisitor);
84
return new ReturnStatement (conditionalExpr);
89
CodeAction GetAction<T> (RefactoringContext context, IfElseStatement ifElseStatement,
90
Func<Statement, T> extractor, Func<T, T, Statement> getReplaceStatement,
91
bool findImplicitFalseStatement = false)
95
var node1 = GetNode (ifElseStatement.TrueStatement, extractor);
99
var falseStatement = ifElseStatement.FalseStatement;
100
// try next statement if there is no FalseStatement
101
if (falseStatement.IsNull && findImplicitFalseStatement)
102
falseStatement = ifElseStatement.GetNextSibling (n => n is Statement) as Statement;
104
var node2 = GetNode (falseStatement, extractor);
108
var replacement = getReplaceStatement (node1, node2);
109
if (replacement == null)
112
return new CodeAction (context.TranslateString ("Convert 'if' to '?:'"),
115
script.Replace (ifElseStatement, replacement);
116
// remove implicit false statement
117
if (falseStatement != ifElseStatement.FalseStatement)
118
script.Remove (falseStatement);
124
static T GetNode<T> (Statement node, Func<Statement, T> extract)
127
var result = extract (node);
131
var blockStatement = node as BlockStatement;
132
if (blockStatement != null && blockStatement.Statements.Count == 1)
133
return GetNode (blockStatement.Statements.First (), extract);