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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertToInitializer/ConvertInitializerToExplicitInitializationsAction.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
// ConvertInitializerToExplicitInitializationsAction.cs
 
3
//
 
4
// Author:
 
5
//       Simon Lindgren <simon.n.lindgren@gmail.com>
 
6
//
 
7
// Copyright (c) 2012 Simon Lindgren
 
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
using System;
 
27
using System.Collections.Generic;
 
28
using System.Linq;
 
29
 
 
30
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
31
{
 
32
        [ContextAction("Convert to explicit initializers",
 
33
                       Description = "Converts an object or collection initializer to explicit initializations.")]
 
34
        public class ConvertInitializerToExplicitInitializationsAction : ICodeActionProvider
 
35
        {
 
36
                #region ICodeActionProvider implementation
 
37
 
 
38
                public IEnumerable<CodeAction> GetActions(RefactoringContext context)
 
39
                {
 
40
                        var codeAction = GetActionForVariableInitializer(context);
 
41
                        if (codeAction != null) {
 
42
                                yield return codeAction;
 
43
                                yield break;
 
44
                        }
 
45
                        codeAction = GetActionForAssignmentExpression(context);
 
46
                        if (codeAction != null) {
 
47
                                yield return codeAction;
 
48
                                yield break;
 
49
                        }
 
50
                }
 
51
                
 
52
                public CodeAction GetActionForVariableInitializer(RefactoringContext context)
 
53
                {
 
54
                        var variableInitializer = context.GetNode<VariableInitializer>();
 
55
                        if (variableInitializer == null)
 
56
                                return null;
 
57
                        var declaration = variableInitializer.Parent as VariableDeclarationStatement;
 
58
                        if (declaration == null)
 
59
                                return null;
 
60
                        if (variableInitializer.Initializer.IsNull)
 
61
                                return null;
 
62
                        var objectCreateExpression = variableInitializer.Initializer as ObjectCreateExpression;
 
63
                        if (objectCreateExpression == null)
 
64
                                return null;
 
65
                        var converter = new InitializerConversionVisitor(context);
 
66
                        Expression finalExpression;
 
67
                        var statements = converter.ConvertInitializer(objectCreateExpression, out finalExpression);
 
68
                        if (statements.Count > 0) {
 
69
                                return new CodeAction(context.TranslateString("Convert to explicit initializers"), script => {
 
70
                                        foreach (var statement in statements) {
 
71
                                                script.InsertBefore(declaration, statement);
 
72
                                        }
 
73
                                        script.Replace(variableInitializer.Initializer, finalExpression);
 
74
                                }, variableInitializer);
 
75
                        }
 
76
                        return null;
 
77
                }
 
78
                
 
79
                public CodeAction GetActionForAssignmentExpression(RefactoringContext context)
 
80
                {
 
81
                        var assignmentExpression = context.GetNode<AssignmentExpression>();
 
82
                        if (assignmentExpression == null)
 
83
                                return null;
 
84
                        var expressionStatement = assignmentExpression.Parent as ExpressionStatement;
 
85
                        if (expressionStatement == null)
 
86
                                return null;
 
87
                        var objectCreateExpression = assignmentExpression.Right as ObjectCreateExpression;
 
88
                        if (objectCreateExpression == null)
 
89
                                return null;
 
90
                        var converter = new InitializerConversionVisitor(context);
 
91
                        Expression finalExpression;
 
92
                        var statements = converter.ConvertInitializer(objectCreateExpression, out finalExpression);
 
93
                        if (statements.Count > 0) {
 
94
                                return new CodeAction(context.TranslateString("Convert to explicit initializers"), script => {
 
95
                                        foreach (var statement in statements) {
 
96
                                                script.InsertBefore(expressionStatement, statement);
 
97
                                        }
 
98
                                        script.Replace(assignmentExpression.Right, finalExpression);
 
99
                                }, assignmentExpression);
 
100
                        }
 
101
                        return null;
 
102
                }
 
103
                #endregion
 
104
 
 
105
                class InitializerConversionVisitor : DepthFirstAstVisitor<Expression, Expression>
 
106
                {
 
107
                        RefactoringContext context;
 
108
                        IList<Statement> statements;
 
109
                        NamingHelper namingHelper;
 
110
 
 
111
                        public InitializerConversionVisitor(RefactoringContext context)
 
112
                        {
 
113
                                this.context = context;
 
114
                                namingHelper = new NamingHelper(context);
 
115
                        }
 
116
 
 
117
                        AstType GetDeclarationType(AstType type)
 
118
                        {
 
119
                                AstType declarationType;
 
120
                                if (context.UseExplicitTypes) {
 
121
                                        declarationType = type.Clone();
 
122
                                } else {
 
123
                                        declarationType = new SimpleType("var");
 
124
                                }
 
125
                                return declarationType;
 
126
                        }
 
127
 
 
128
                        public IList<Statement> ConvertInitializer(ObjectCreateExpression initializer, out Expression finalExpression)
 
129
                        {
 
130
                                statements = new List<Statement>();
 
131
 
 
132
                                finalExpression = initializer.AcceptVisitor(this, null);
 
133
 
 
134
                                return statements;
 
135
                        }
 
136
 
 
137
                        public override Expression VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, Expression data)
 
138
                        {
 
139
                                var creationType = objectCreateExpression.Type.Clone();
 
140
 
 
141
                                var parameters = objectCreateExpression.Arguments.Select(arg => arg.Clone());
 
142
                                var newCreation = new ObjectCreateExpression(creationType, parameters);
 
143
                                if (objectCreateExpression.Initializer.IsNull) {
 
144
                                        return newCreation;
 
145
                                } else {
 
146
                                        AstType declarationType = GetDeclarationType(objectCreateExpression.Type);
 
147
                                        var name = namingHelper.GenerateVariableName(objectCreateExpression.Type);
 
148
                                        var variableInitializer = new VariableDeclarationStatement(declarationType, name, newCreation);
 
149
                                        statements.Add(variableInitializer);
 
150
 
 
151
                                        var identifier = new IdentifierExpression(name);
 
152
                                        base.VisitObjectCreateExpression(objectCreateExpression, identifier);
 
153
 
 
154
                                        return identifier;
 
155
                                }
 
156
                        }
 
157
 
 
158
                        public override Expression VisitArrayInitializerExpression(ArrayInitializerExpression arrayInitializerExpression, Expression data)
 
159
                        {
 
160
                                if (!(arrayInitializerExpression.Parent is ArrayInitializerExpression)) {
 
161
                                        return base.VisitArrayInitializerExpression(arrayInitializerExpression, data);
 
162
                                }
 
163
                                // this a tuple in a collection initializer
 
164
                                var arguments = arrayInitializerExpression.Elements.Select(element => element.AcceptVisitor(this, null).Clone());
 
165
                                var method = new MemberReferenceExpression {
 
166
                                        Target = data.Clone(),
 
167
                                        MemberName = "Add"
 
168
                                };
 
169
                                var statement = new ExpressionStatement(new InvocationExpression(method, arguments));
 
170
                                statements.Add(statement);
 
171
 
 
172
                                return null;
 
173
                        }
 
174
 
 
175
                        public override Expression VisitNamedExpression(NamedExpression namedExpression, Expression data)
 
176
                        {
 
177
                                var member = new MemberReferenceExpression {
 
178
                                        Target = data.Clone(),
 
179
                                        MemberName = namedExpression.Name
 
180
                                };
 
181
                                var expression = namedExpression.Expression.AcceptVisitor(this, member);
 
182
                                if (expression != null) {
 
183
                                        var statement = new ExpressionStatement {
 
184
                                                Expression = new AssignmentExpression {
 
185
                                                        Left = member,
 
186
                                                        Right = expression.Clone()
 
187
                                                }
 
188
                                        };
 
189
                                        statements.Add(statement);
 
190
                                }
 
191
 
 
192
                                return null;
 
193
                        }
 
194
 
 
195
                        protected override Expression VisitChildren(AstNode node, Expression data)
 
196
                        {
 
197
                                // Most expressions should just be used as-is, and
 
198
                                // a) need not be visited
 
199
                                // b) only return themselves
 
200
                                if (node is Expression && !(node is ObjectCreateExpression || node is ArrayInitializerExpression || node is NamedExpression)){
 
201
                                        return (Expression)node;
 
202
                                }
 
203
                                return base.VisitChildren(node, data);
 
204
                        }
 
205
                }
 
206
        }
 
207
}
 
208