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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractAnonymousMethodAction.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
// ExtractAnonymousMethodAction.cs
 
3
// 
 
4
// Author:
 
5
//      Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.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
 
 
27
using System.Collections.Generic;
 
28
using System.Linq;
 
29
using ICSharpCode.NRefactory.CSharp.Resolver;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
32
{
 
33
        [ContextAction ("Extract anonymous method",
 
34
                                        Description = "Extract anonymous method to method of the containing type")]
 
35
        public class ExtractAnonymousMethodAction : ICodeActionProvider
 
36
        {
 
37
                public IEnumerable<CodeAction> GetActions (RefactoringContext context)
 
38
                {
 
39
                        // lambda
 
40
                        var lambda = context.GetNode<LambdaExpression> ();
 
41
                        if (lambda != null && lambda.ArrowToken.Contains(context.Location)) {
 
42
                                if (ContainsLocalReferences (context, lambda, lambda.Body))
 
43
                                        yield break;
 
44
 
 
45
                                bool noReturn = false;
 
46
                                BlockStatement body;
 
47
                                if (lambda.Body is BlockStatement) {
 
48
                                        body = (BlockStatement)lambda.Body.Clone ();
 
49
                                } else {
 
50
                                        body = new BlockStatement ();
 
51
 
 
52
                                        var type = LambdaHelper.GetLambdaReturnType (context, lambda);
 
53
                                        if (type == null || type.ReflectionName == "System.Void") {
 
54
                                                noReturn = true;
 
55
                                                body.Add (new ExpressionStatement ((Expression)lambda.Body.Clone ()));
 
56
                                        } else {
 
57
                                                body.Add (new ReturnStatement ((Expression)lambda.Body.Clone ()));
 
58
                                        }
 
59
                                }
 
60
                                var method = GetMethod (context, (LambdaResolveResult)context.Resolve (lambda), body, noReturn);
 
61
                                yield return GetAction (context, lambda, method);
 
62
                        }
 
63
 
 
64
                        // anonymous method
 
65
                        var anonymousMethod = context.GetNode<AnonymousMethodExpression> ();
 
66
                        if (anonymousMethod != null && anonymousMethod.DelegateToken.Contains(context.Location)) {
 
67
                                if (ContainsLocalReferences (context, anonymousMethod, anonymousMethod.Body))
 
68
                                        yield break;
 
69
 
 
70
                                var method = GetMethod (context, (LambdaResolveResult)context.Resolve (anonymousMethod), 
 
71
                                                                                (BlockStatement)anonymousMethod.Body.Clone ());
 
72
                                yield return GetAction (context, anonymousMethod, method);
 
73
                        }
 
74
                }
 
75
 
 
76
                CodeAction GetAction (RefactoringContext context, AstNode node, MethodDeclaration method)
 
77
                {
 
78
                        return new CodeAction (context.TranslateString ("Extract anonymous method"),
 
79
                                script =>
 
80
                                {
 
81
                                        var identifier = new IdentifierExpression ("Method");
 
82
                                        script.Replace (node, identifier);
 
83
                                        script.InsertBefore (node.GetParent<EntityDeclaration> (), method);
 
84
                                        script.Link (method.NameToken, identifier);
 
85
                                }, method.NameToken);
 
86
                }
 
87
 
 
88
                static MethodDeclaration GetMethod (RefactoringContext context, LambdaResolveResult lambda, BlockStatement body,
 
89
                        bool noReturnValue = false)
 
90
                {
 
91
                        var method = new MethodDeclaration { Name = "Method" };
 
92
 
 
93
                        if (noReturnValue) {
 
94
                                method.ReturnType = new PrimitiveType ("void"); 
 
95
                        } else {
 
96
                                var type = lambda.GetInferredReturnType (lambda.Parameters.Select (p => p.Type).ToArray ());
 
97
                                method.ReturnType = type.Name == "?" ? new PrimitiveType ("void") : context.CreateShortType (type);
 
98
                        }
 
99
 
 
100
                        foreach (var param in lambda.Parameters)
 
101
                                method.Parameters.Add (new ParameterDeclaration (context.CreateShortType (param.Type), param.Name));
 
102
 
 
103
                        method.Body = body;
 
104
                        if (lambda.IsAsync)
 
105
                                method.Modifiers |= Modifiers.Async;
 
106
 
 
107
                        return method;
 
108
                }
 
109
 
 
110
                static bool ContainsLocalReferences (RefactoringContext context, AstNode expr, AstNode body)
 
111
                {
 
112
                        var visitor = new ExtractMethod.VariableLookupVisitor (context);
 
113
                        body.AcceptVisitor (visitor);
 
114
                        return visitor.UsedVariables.Any (variable => !expr.Contains (variable.Region.Begin));
 
115
                }
 
116
        }
 
117
}