2
// ConvertForeachToFor.cs
5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.com>
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
28
using ICSharpCode.NRefactory.TypeSystem;
29
using System.Threading;
30
using System.Collections.Generic;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
35
/// Converts a foreach loop to for.
37
[ContextAction("Convert 'foreach' loop to 'for'", Description = "Works on 'foreach' loops that allow direct access to its elements.")]
38
public class ConvertForeachToForAction : ICodeActionProvider
40
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
42
var foreachStatement = GetForeachStatement(context);
43
if (foreachStatement == null) {
46
yield return new CodeAction(context.TranslateString("Convert 'foreach' loop to 'for'"), script => {
47
var result = context.Resolve(foreachStatement.InExpression);
48
var countProperty = GetCountProperty(result.Type);
50
// TODO: use another variable name if 'i' is already in use
51
var initializer = new VariableDeclarationStatement(new PrimitiveType("int"), "i", new PrimitiveExpression(0));
52
var id1 = new IdentifierExpression("i");
53
var id2 = id1.Clone();
54
var id3 = id1.Clone();
56
var variableDeclarationStatement = new VariableDeclarationStatement(
57
foreachStatement.VariableType.Clone(),
58
foreachStatement.VariableName,
59
new IndexerExpression(foreachStatement.InExpression.Clone(), id3)
61
var forStatement = new ForStatement() {
62
Initializers = { initializer },
63
Condition = new BinaryOperatorExpression (id1, BinaryOperatorType.LessThan, new MemberReferenceExpression (foreachStatement.InExpression.Clone (), countProperty)),
64
Iterators = { new ExpressionStatement (new UnaryOperatorExpression (UnaryOperatorType.PostIncrement, id2)) },
65
EmbeddedStatement = new BlockStatement {
66
variableDeclarationStatement
70
if (foreachStatement.EmbeddedStatement is BlockStatement) {
71
variableDeclarationStatement.Remove();
72
var oldBlock = (BlockStatement)foreachStatement.EmbeddedStatement.Clone();
73
if (oldBlock.Statements.Any()) {
74
oldBlock.Statements.InsertBefore(oldBlock.Statements.First(), variableDeclarationStatement);
76
oldBlock.Statements.Add(variableDeclarationStatement);
78
forStatement.EmbeddedStatement = oldBlock;
80
forStatement.EmbeddedStatement.AddChild (foreachStatement.EmbeddedStatement.Clone (), BlockStatement.StatementRole);
82
script.Replace (foreachStatement, forStatement);
83
script.Link (initializer.Variables.First ().NameToken, id1, id2, id3);
87
static string GetCountProperty(IType type)
89
if (type.Kind == TypeKind.Array) {
95
static ForeachStatement GetForeachStatement (RefactoringContext context)
97
var astNode = context.GetNode ();
100
var result = (astNode as ForeachStatement) ?? astNode.Parent as ForeachStatement;
103
var collection = context.Resolve (result.InExpression);
104
if (collection.Type.Kind != TypeKind.Array && !collection.Type.GetProperties(p => p.IsIndexer).Any())