2
// ConvertInitializerToExplicitInitializationsAction.cs
5
// Simon Lindgren <simon.n.lindgren@gmail.com>
7
// Copyright (c) 2012 Simon Lindgren
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
27
using System.Collections.Generic;
30
namespace ICSharpCode.NRefactory.CSharp.Refactoring
32
[ContextAction("Convert to explicit initializers",
33
Description = "Converts an object or collection initializer to explicit initializations.")]
34
public class ConvertInitializerToExplicitInitializationsAction : ICodeActionProvider
36
#region ICodeActionProvider implementation
38
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
40
var codeAction = GetActionForVariableInitializer(context);
41
if (codeAction != null) {
42
yield return codeAction;
45
codeAction = GetActionForAssignmentExpression(context);
46
if (codeAction != null) {
47
yield return codeAction;
52
public CodeAction GetActionForVariableInitializer(RefactoringContext context)
54
var variableInitializer = context.GetNode<VariableInitializer>();
55
if (variableInitializer == null)
57
var declaration = variableInitializer.Parent as VariableDeclarationStatement;
58
if (declaration == null)
60
if (variableInitializer.Initializer.IsNull)
62
var objectCreateExpression = variableInitializer.Initializer as ObjectCreateExpression;
63
if (objectCreateExpression == 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);
73
script.Replace(variableInitializer.Initializer, finalExpression);
74
}, variableInitializer);
79
public CodeAction GetActionForAssignmentExpression(RefactoringContext context)
81
var assignmentExpression = context.GetNode<AssignmentExpression>();
82
if (assignmentExpression == null)
84
var expressionStatement = assignmentExpression.Parent as ExpressionStatement;
85
if (expressionStatement == null)
87
var objectCreateExpression = assignmentExpression.Right as ObjectCreateExpression;
88
if (objectCreateExpression == 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);
98
script.Replace(assignmentExpression.Right, finalExpression);
99
}, assignmentExpression);
105
class InitializerConversionVisitor : DepthFirstAstVisitor<Expression, Expression>
107
RefactoringContext context;
108
IList<Statement> statements;
109
NamingHelper namingHelper;
111
public InitializerConversionVisitor(RefactoringContext context)
113
this.context = context;
114
namingHelper = new NamingHelper(context);
117
AstType GetDeclarationType(AstType type)
119
AstType declarationType;
120
if (context.UseExplicitTypes) {
121
declarationType = type.Clone();
123
declarationType = new SimpleType("var");
125
return declarationType;
128
public IList<Statement> ConvertInitializer(ObjectCreateExpression initializer, out Expression finalExpression)
130
statements = new List<Statement>();
132
finalExpression = initializer.AcceptVisitor(this, null);
137
public override Expression VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, Expression data)
139
var creationType = objectCreateExpression.Type.Clone();
141
var parameters = objectCreateExpression.Arguments.Select(arg => arg.Clone());
142
var newCreation = new ObjectCreateExpression(creationType, parameters);
143
if (objectCreateExpression.Initializer.IsNull) {
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);
151
var identifier = new IdentifierExpression(name);
152
base.VisitObjectCreateExpression(objectCreateExpression, identifier);
158
public override Expression VisitArrayInitializerExpression(ArrayInitializerExpression arrayInitializerExpression, Expression data)
160
if (!(arrayInitializerExpression.Parent is ArrayInitializerExpression)) {
161
return base.VisitArrayInitializerExpression(arrayInitializerExpression, data);
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(),
169
var statement = new ExpressionStatement(new InvocationExpression(method, arguments));
170
statements.Add(statement);
175
public override Expression VisitNamedExpression(NamedExpression namedExpression, Expression data)
177
var member = new MemberReferenceExpression {
178
Target = data.Clone(),
179
MemberName = namedExpression.Name
181
var expression = namedExpression.Expression.AcceptVisitor(this, member);
182
if (expression != null) {
183
var statement = new ExpressionStatement {
184
Expression = new AssignmentExpression {
186
Right = expression.Clone()
189
statements.Add(statement);
195
protected override Expression VisitChildren(AstNode node, Expression data)
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;
203
return base.VisitChildren(node, data);