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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertIfToConditionalAction.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
// ConvertIfToConditionalAction.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.Linq;
 
29
using ICSharpCode.NRefactory.PatternMatching;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
32
{
 
33
        [ContextAction ("Convert 'if' to '?:'", Description = "Convert 'if' statement to '?:' operator.")]
 
34
        public class ConvertIfToConditionalAction : SpecializedCodeAction <IfElseStatement>
 
35
        {
 
36
                static readonly InsertParenthesesVisitor insertParenthesesVisitor = new InsertParenthesesVisitor ();
 
37
                protected override CodeAction GetAction (RefactoringContext context, IfElseStatement ifElseStatement)
 
38
                {
 
39
                        if (!ifElseStatement.IfToken.Contains (context.Location))
 
40
                                return null;
 
41
                        var convertAssignment = GetConvertAssignmentAction (context, ifElseStatement);
 
42
                        if (convertAssignment != null)
 
43
                                return convertAssignment;
 
44
                        var convertReturn = GetConvertReturnAction (context, ifElseStatement);
 
45
                        return convertReturn;
 
46
                }
 
47
 
 
48
                CodeAction GetConvertAssignmentAction (RefactoringContext context, IfElseStatement ifElseStatement)
 
49
                {
 
50
                        Func<Statement, AssignmentExpression> extractAssignment =
 
51
                                node =>
 
52
                                {
 
53
                                        var exprStatement = node as ExpressionStatement;
 
54
                                        if (exprStatement != null)
 
55
                                                return exprStatement.Expression as AssignmentExpression;
 
56
                                        return  null;
 
57
                                };
 
58
                        return GetAction (context, ifElseStatement, extractAssignment,
 
59
                                (assignment1, assignment2) =>
 
60
                                {
 
61
                                        if (assignment1.Operator != assignment2.Operator || !assignment1.Left.Match (assignment2.Left).Success)
 
62
                                                return null;
 
63
                                        var conditionalExpr = new ConditionalExpression (ifElseStatement.Condition.Clone (),
 
64
                                                                                                                                         assignment1.Right.Clone (),
 
65
                                                                                                                                         assignment2.Right.Clone ());
 
66
                                        conditionalExpr.AcceptVisitor (insertParenthesesVisitor);
 
67
 
 
68
                                        var assignment = new AssignmentExpression (assignment1.Left.Clone (), assignment1.Operator,
 
69
                                                                                                                           conditionalExpr);
 
70
                                        return new ExpressionStatement (assignment);
 
71
                                });
 
72
                }
 
73
 
 
74
                CodeAction GetConvertReturnAction (RefactoringContext context, IfElseStatement ifElseStatement)
 
75
                {
 
76
                        Func<Statement, ReturnStatement> extractReturn = node => node as ReturnStatement;
 
77
                        return GetAction (context, ifElseStatement, extractReturn,
 
78
                                (return1, return2) =>
 
79
                                {
 
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);
 
85
                                },
 
86
                                true);
 
87
                }
 
88
 
 
89
                CodeAction GetAction<T> (RefactoringContext context, IfElseStatement ifElseStatement,
 
90
                                                                 Func<Statement, T> extractor, Func<T, T, Statement> getReplaceStatement,
 
91
                                                                 bool findImplicitFalseStatement = false)
 
92
                        where T : AstNode
 
93
                {
 
94
 
 
95
                        var node1 = GetNode (ifElseStatement.TrueStatement, extractor);
 
96
                        if (node1 == null)
 
97
                                return null;
 
98
 
 
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;
 
103
 
 
104
                        var node2 = GetNode (falseStatement, extractor);
 
105
                        if (node2 == null)
 
106
                                return null;
 
107
 
 
108
                        var replacement = getReplaceStatement (node1, node2);
 
109
                        if (replacement == null)
 
110
                                return null;
 
111
 
 
112
                        return new CodeAction (context.TranslateString ("Convert 'if' to '?:'"),
 
113
                                script =>
 
114
                                {
 
115
                                        script.Replace (ifElseStatement, replacement);
 
116
                                        // remove implicit false statement
 
117
                                        if (falseStatement != ifElseStatement.FalseStatement)
 
118
                                                script.Remove (falseStatement);
 
119
                                }, 
 
120
                                ifElseStatement
 
121
                        );
 
122
                }
 
123
 
 
124
                static T GetNode<T> (Statement node, Func<Statement, T> extract)
 
125
                        where T : AstNode
 
126
                {
 
127
                        var result = extract (node);
 
128
                        if (result != null)
 
129
                                return result;
 
130
 
 
131
                        var blockStatement = node as BlockStatement;
 
132
                        if (blockStatement != null && blockStatement.Statements.Count == 1)
 
133
                                return GetNode (blockStatement.Statements.First (), extract);
 
134
                        return null;
 
135
                }
 
136
        }
 
137
}