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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertForeachToForAction.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
 
// ConvertForeachToFor.cs
3
 
//  
4
 
// Author:
5
 
//       Mike Krüger <mkrueger@novell.com>
6
 
// 
7
 
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.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
 
using System;
27
 
using System.Linq;
28
 
using ICSharpCode.NRefactory.TypeSystem;
29
 
using System.Threading;
30
 
using System.Collections.Generic;
31
 
 
32
 
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
 
{
34
 
        /// <summary>
35
 
        /// Converts a foreach loop to for.
36
 
        /// </summary>
37
 
        [ContextAction("Convert 'foreach' loop to 'for'", Description = "Works on 'foreach' loops that allow direct access to its elements.")]
38
 
        public class ConvertForeachToForAction : ICodeActionProvider
39
 
        {
40
 
                public IEnumerable<CodeAction> GetActions(RefactoringContext context)
41
 
                {
42
 
                        var foreachStatement = GetForeachStatement(context);
43
 
                        if (foreachStatement == null) {
44
 
                                yield break;
45
 
                        }
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);
49
 
                                
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();
55
 
                                
56
 
                                var variableDeclarationStatement = new VariableDeclarationStatement(
57
 
                                        foreachStatement.VariableType.Clone(),
58
 
                                        foreachStatement.VariableName,
59
 
                                        new IndexerExpression(foreachStatement.InExpression.Clone(), id3)
60
 
                                );
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
67
 
                                        }
68
 
                                };
69
 
                                
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);
75
 
                                        } else {
76
 
                                                oldBlock.Statements.Add(variableDeclarationStatement);
77
 
                                        }
78
 
                                        forStatement.EmbeddedStatement = oldBlock;
79
 
                                } else {
80
 
                                        forStatement.EmbeddedStatement.AddChild (foreachStatement.EmbeddedStatement.Clone (), BlockStatement.StatementRole);
81
 
                                }
82
 
                                script.Replace (foreachStatement, forStatement);
83
 
                                script.Link (initializer.Variables.First ().NameToken, id1, id2, id3);
84
 
                        });
85
 
                }
86
 
                
87
 
                static string GetCountProperty(IType type)
88
 
                {
89
 
                        if (type.Kind == TypeKind.Array) {
90
 
                                return "Length";
91
 
                        }
92
 
                        return "Count";
93
 
                }
94
 
 
95
 
                static ForeachStatement GetForeachStatement (RefactoringContext context)
96
 
                {
97
 
                        var astNode = context.GetNode ();
98
 
                        if (astNode == null)
99
 
                                return null;
100
 
                        var result = (astNode as ForeachStatement) ?? astNode.Parent as ForeachStatement;
101
 
                        if (result == null)
102
 
                                return null;
103
 
                        var collection = context.Resolve (result.InExpression);
104
 
                        if (collection.Type.Kind != TypeKind.Array && !collection.Type.GetProperties(p => p.IsIndexer).Any())
105
 
                                return null;
106
 
                        return result;
107
 
                }
108
 
        }
109
 
}