2
// IterateViaForeachTests.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
26
using ICSharpCode.NRefactory.CSharp.Refactoring;
27
using ICSharpCode.NRefactory.TypeSystem;
28
using System.Collections.Generic;
29
using ICSharpCode.NRefactory.Semantics;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
34
[ContextAction("Iterate via foreach", Description = "Iterates an IEnumerable with a foreach loop.")]
35
public class IterateViaForeachAction : ICodeActionProvider
37
#region ICodeActionProvider implementation
39
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
42
action = ActionFromUsingStatement(context);
45
action = ActionFromVariableInitializer(context);
48
action = ActionFromExpressionStatement(context);
53
CodeAction ActionFromUsingStatement(RefactoringContext context)
55
var initializer = context.GetNode<VariableInitializer>();
56
if (initializer == null)
58
var initializerRR = context.Resolve(initializer) as LocalResolveResult;
59
if (initializerRR == null)
61
var elementType = GetElementType(initializerRR, context);
62
if (elementType == null)
64
var usingStatement = initializer.Parent.Parent as UsingStatement;
65
if (usingStatement == null)
67
return new CodeAction(context.TranslateString("Iterate via foreach"), script => {
68
var iterator = MakeForeach(new IdentifierExpression(initializer.Name), elementType, context);
69
if (usingStatement.EmbeddedStatement is EmptyStatement) {
70
var blockStatement = new BlockStatement();
71
blockStatement.Statements.Add(iterator);
72
script.Replace(usingStatement.EmbeddedStatement, blockStatement);
73
script.FormatText((AstNode)blockStatement);
74
} else if (usingStatement.EmbeddedStatement is BlockStatement) {
75
var anchorNode = usingStatement.EmbeddedStatement.FirstChild;
76
script.InsertAfter(anchorNode, iterator);
77
script.FormatText(usingStatement.EmbeddedStatement);
82
CodeAction ActionFromVariableInitializer(RefactoringContext context)
84
var initializer = context.GetNode<VariableInitializer>();
85
if (initializer == null || initializer.Parent.Parent is ForStatement)
87
var initializerRR = context.Resolve(initializer) as LocalResolveResult;
88
if (initializerRR == null)
90
var elementType = GetElementType(initializerRR, context);
91
if (elementType == null)
94
return new CodeAction(context.TranslateString("Iterate via foreach"), script => {
95
var iterator = MakeForeach(new IdentifierExpression(initializer.Name), elementType, context);
96
script.InsertAfter(context.GetNode<Statement>(), iterator);
100
CodeAction ActionFromExpressionStatement(RefactoringContext context)
102
var expressionStatement = context.GetNode<ExpressionStatement>();
103
if (expressionStatement == null)
105
var expression = expressionStatement.Expression;
106
if (expression is AssignmentExpression)
107
expression = ((AssignmentExpression)expression).Left;
108
var expressionRR = context.Resolve(expression);
109
if (expressionRR == null)
111
var elementType = GetElementType(expressionRR, context);
112
if (elementType == null)
114
return new CodeAction(context.TranslateString("Iterate via foreach"), script => {
115
var iterator = MakeForeach(expression, elementType, context);
116
if (expression == expressionStatement.Expression)
117
script.Replace(expressionStatement, iterator);
119
script.InsertAfter(expressionStatement, iterator);
123
static ForeachStatement MakeForeach(Expression node, IType type, RefactoringContext context)
125
var namingHelper = new NamingHelper(context);
126
return new ForeachStatement {
127
VariableType = new SimpleType("var"),
128
VariableName = namingHelper.GenerateVariableName(type),
129
InExpression = node.Clone(),
130
EmbeddedStatement = new BlockStatement()
134
static IType GetElementType(ResolveResult rr, BaseRefactoringContext context)
136
if (rr.IsError || rr.Type.Kind == TypeKind.Unknown)
138
var type = GetCollectionType(rr.Type);
142
var parameterizedType = type as ParameterizedType;
143
if (parameterizedType != null)
144
return parameterizedType.TypeArguments.First();
145
return context.Compilation.FindType(KnownTypeCode.Object);
148
static IType GetCollectionType(IType type)
150
IType collectionType = null;
151
foreach (var baseType in type.GetAllBaseTypes()) {
152
if (baseType.IsKnownType(KnownTypeCode.IEnumerableOfT)) {
153
collectionType = baseType;
155
} else if (baseType.IsKnownType(KnownTypeCode.IEnumerable)) {
156
collectionType = baseType;
157
// Don't break, continue in case type implements IEnumerable<T>
160
return collectionType;