~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertConditionalToIfAction.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ConvertConditionalToIfAction.cs
 
3
//  
 
4
// Author:
 
5
//       Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Linq;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
32
{
 
33
        [ContextAction ("Convert '?:' to 'if'", Description = "Convert '?:' operator to 'if' statement.")]
 
34
        public class ConvertConditionalToIfAction : ICodeActionProvider
 
35
        {
 
36
                public IEnumerable<CodeAction> GetActions (RefactoringContext context)
 
37
                {
 
38
                        // return
 
39
                        var returnStatement = context.GetNode<ReturnStatement> ();
 
40
                        if (returnStatement != null) {
 
41
                                var action = GetAction (context, returnStatement, returnStatement.Expression,
 
42
                                        expr => new Statement[]
 
43
                                        {
 
44
                                                new IfElseStatement (expr.Condition.Clone (), 
 
45
                                                                                         new ReturnStatement (expr.TrueExpression.Clone ()),
 
46
                                                                                         new ReturnStatement (expr.FalseExpression.Clone ()))
 
47
                                        });
 
48
                                if (action != null)
 
49
                                        yield return action;
 
50
                                yield break;
 
51
                        }
 
52
 
 
53
                        // assignment
 
54
                        var assignment = context.GetNode<AssignmentExpression> ();
 
55
                        if (assignment != null) {
 
56
                                var statement = assignment.Parent as ExpressionStatement;
 
57
                                if (statement == null)
 
58
                                        yield break;
 
59
 
 
60
                                // statement is in for initializer/iterator
 
61
                                if (statement.Parent is ForStatement &&
 
62
                                        statement != ((ForStatement)statement.Parent).EmbeddedStatement)
 
63
                                        yield break;
 
64
 
 
65
                                // statement is in using resource-acquisition
 
66
                                if (statement.Parent is UsingStatement &&
 
67
                                        statement != ((UsingStatement)statement.Parent).EmbeddedStatement)
 
68
                                        yield break;
 
69
 
 
70
                                var action = GetAction (context, statement, assignment.Right,
 
71
                                        expr => new Statement [] { ConvertAssignment (assignment.Left, assignment.Operator, expr) });
 
72
                                if (action != null)
 
73
                                        yield return action;
 
74
 
 
75
                                yield break;
 
76
                        }
 
77
 
 
78
                        // variable initializer
 
79
                        var initializer = context.GetNode<VariableInitializer> ();
 
80
                        if (initializer != null && initializer.Parent is VariableDeclarationStatement &&
 
81
                                initializer.Parent.Parent is BlockStatement) {
 
82
 
 
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;
 
88
                                }
 
89
 
 
90
                                var action = GetAction (context, variableDecl, initializer.Initializer,
 
91
                                        expr => new Statement []
 
92
                                        {
 
93
                                                newVariableDecl,
 
94
                                                ConvertAssignment (new IdentifierExpression (initializer.Name), AssignmentOperatorType.Assign,
 
95
                                                                                   expr)
 
96
                                        });
 
97
                                if (action != null)
 
98
                                        yield return action;
 
99
                        }
 
100
                }
 
101
 
 
102
                CodeAction GetAction (RefactoringContext context, Statement originalStatement, Expression conditionalCandidate, 
 
103
                                                          Func<ConditionalExpression, IEnumerable<Statement>> getReplacement)
 
104
                {
 
105
                        var conditionalExpr = GetConditionalExpression (conditionalCandidate);
 
106
                        if (conditionalExpr == null || 
 
107
                                !(conditionalExpr.QuestionMarkToken.Contains(context.Location) ||
 
108
                                  conditionalExpr.ColonToken.Contains(context.Location)))
 
109
                                return null;
 
110
 
 
111
                        return new CodeAction (context.TranslateString ("Convert '?:' to 'if'"),
 
112
                                script =>
 
113
                                {
 
114
                                        foreach (var node in getReplacement (conditionalExpr))
 
115
                                                script.InsertBefore (originalStatement, node);
 
116
                                        script.Remove (originalStatement);
 
117
                        }, conditionalExpr);
 
118
                }
 
119
 
 
120
                static ConditionalExpression GetConditionalExpression (Expression expr)
 
121
                {
 
122
                        while (expr != null) {
 
123
                                if (expr is ConditionalExpression)
 
124
                                        return (ConditionalExpression)expr;
 
125
 
 
126
                                var parenthesizedExpr = expr as ParenthesizedExpression;
 
127
                                if (parenthesizedExpr == null)
 
128
                                        break;
 
129
                                expr = parenthesizedExpr.Expression;
 
130
                        }
 
131
                        return null;
 
132
                }
 
133
 
 
134
                static IfElseStatement ConvertAssignment (Expression target, AssignmentOperatorType op,
 
135
                                                                                                  ConditionalExpression conditionalExpr)
 
136
                {
 
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));
 
144
                }
 
145
        }
 
146
}