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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/IterateViaForeachAction.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
// IterateViaForeachTests.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 ICSharpCode.NRefactory.CSharp.Refactoring;
 
27
using ICSharpCode.NRefactory.TypeSystem;
 
28
using System.Collections.Generic;
 
29
using ICSharpCode.NRefactory.Semantics;
 
30
using System.Linq;
 
31
 
 
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
33
{
 
34
        [ContextAction("Iterate via foreach", Description = "Iterates an IEnumerable with a foreach loop.")]
 
35
        public class IterateViaForeachAction : ICodeActionProvider
 
36
        {
 
37
                #region ICodeActionProvider implementation
 
38
 
 
39
                public IEnumerable<CodeAction> GetActions(RefactoringContext context)
 
40
                {
 
41
                        CodeAction action;
 
42
                        action = ActionFromUsingStatement(context);
 
43
                        if (action != null)
 
44
                                yield return action;
 
45
                        action = ActionFromVariableInitializer(context);
 
46
                        if (action != null)
 
47
                                yield return action;
 
48
                        action = ActionFromExpressionStatement(context);
 
49
                        if (action != null)
 
50
                                yield return action;
 
51
                }
 
52
 
 
53
                CodeAction ActionFromUsingStatement(RefactoringContext context)
 
54
                {
 
55
                        var initializer = context.GetNode<VariableInitializer>();
 
56
                        if (initializer == null)
 
57
                                return null;
 
58
                        var initializerRR = context.Resolve(initializer) as LocalResolveResult;
 
59
                        if (initializerRR == null)
 
60
                                return null;
 
61
                        var elementType = GetElementType(initializerRR, context);
 
62
                        if (elementType == null)
 
63
                                return null;
 
64
                        var usingStatement = initializer.Parent.Parent as UsingStatement;
 
65
                        if (usingStatement == null)
 
66
                                return 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);
 
78
                                }
 
79
                        }, initializer);
 
80
                }
 
81
 
 
82
                CodeAction ActionFromVariableInitializer(RefactoringContext context)
 
83
                {
 
84
                        var initializer = context.GetNode<VariableInitializer>();
 
85
                        if (initializer == null || initializer.Parent.Parent is ForStatement)
 
86
                                return null;
 
87
                        var initializerRR = context.Resolve(initializer) as LocalResolveResult;
 
88
                        if (initializerRR == null)
 
89
                                return null;
 
90
                        var elementType = GetElementType(initializerRR, context);
 
91
                        if (elementType == null)
 
92
                                return null;
 
93
 
 
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);
 
97
                        }, initializer);
 
98
                }
 
99
 
 
100
                CodeAction ActionFromExpressionStatement(RefactoringContext context)
 
101
                {
 
102
                        var expressionStatement = context.GetNode<ExpressionStatement>();
 
103
                        if (expressionStatement == null)
 
104
                                return 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)
 
110
                                return null;
 
111
                        var elementType = GetElementType(expressionRR, context);
 
112
                        if (elementType == null)
 
113
                                return 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);
 
118
                                else
 
119
                                        script.InsertAfter(expressionStatement, iterator);
 
120
                        }, expression);
 
121
                }
 
122
 
 
123
                static ForeachStatement MakeForeach(Expression node, IType type, RefactoringContext context)
 
124
                {
 
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()
 
131
                        };
 
132
                }
 
133
 
 
134
                static IType GetElementType(ResolveResult rr, BaseRefactoringContext context)
 
135
                {
 
136
                        if (rr.IsError || rr.Type.Kind == TypeKind.Unknown)
 
137
                                return null;
 
138
                        var type = GetCollectionType(rr.Type);
 
139
                        if (type == null)
 
140
                                return null;
 
141
 
 
142
                        var parameterizedType = type as ParameterizedType;
 
143
                        if (parameterizedType != null)
 
144
                                return parameterizedType.TypeArguments.First();
 
145
                        return context.Compilation.FindType(KnownTypeCode.Object);
 
146
                }
 
147
 
 
148
                static IType GetCollectionType(IType type)
 
149
                {
 
150
                        IType collectionType = null;
 
151
                        foreach (var baseType in type.GetAllBaseTypes()) {
 
152
                                if (baseType.IsKnownType(KnownTypeCode.IEnumerableOfT)) {
 
153
                                        collectionType = baseType;
 
154
                                        break;
 
155
                                } else if (baseType.IsKnownType(KnownTypeCode.IEnumerable)) {
 
156
                                        collectionType = baseType;
 
157
                                        // Don't break, continue in case type implements IEnumerable<T>
 
158
                                }
 
159
                        }
 
160
                        return collectionType;
 
161
                }
 
162
 
 
163
                #endregion
 
164
        }
 
165
}