2
// ExtractAnonymousMethodAction.cs
5
// Mansheng Yang <lightyang0@gmail.com>
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.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
27
using System.Collections.Generic;
29
using ICSharpCode.NRefactory.CSharp.Resolver;
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
[ContextAction ("Extract anonymous method",
34
Description = "Extract anonymous method to method of the containing type")]
35
public class ExtractAnonymousMethodAction : ICodeActionProvider
37
public IEnumerable<CodeAction> GetActions (RefactoringContext context)
40
var lambda = context.GetNode<LambdaExpression> ();
41
if (lambda != null && lambda.ArrowToken.Contains(context.Location)) {
42
if (ContainsLocalReferences (context, lambda, lambda.Body))
45
bool noReturn = false;
47
if (lambda.Body is BlockStatement) {
48
body = (BlockStatement)lambda.Body.Clone ();
50
body = new BlockStatement ();
52
var type = LambdaHelper.GetLambdaReturnType (context, lambda);
53
if (type == null || type.ReflectionName == "System.Void") {
55
body.Add (new ExpressionStatement ((Expression)lambda.Body.Clone ()));
57
body.Add (new ReturnStatement ((Expression)lambda.Body.Clone ()));
60
var method = GetMethod (context, (LambdaResolveResult)context.Resolve (lambda), body, noReturn);
61
yield return GetAction (context, lambda, method);
65
var anonymousMethod = context.GetNode<AnonymousMethodExpression> ();
66
if (anonymousMethod != null && anonymousMethod.DelegateToken.Contains(context.Location)) {
67
if (ContainsLocalReferences (context, anonymousMethod, anonymousMethod.Body))
70
var method = GetMethod (context, (LambdaResolveResult)context.Resolve (anonymousMethod),
71
(BlockStatement)anonymousMethod.Body.Clone ());
72
yield return GetAction (context, anonymousMethod, method);
76
CodeAction GetAction (RefactoringContext context, AstNode node, MethodDeclaration method)
78
return new CodeAction (context.TranslateString ("Extract anonymous method"),
81
var identifier = new IdentifierExpression ("Method");
82
script.Replace (node, identifier);
83
script.InsertBefore (node.GetParent<EntityDeclaration> (), method);
84
script.Link (method.NameToken, identifier);
88
static MethodDeclaration GetMethod (RefactoringContext context, LambdaResolveResult lambda, BlockStatement body,
89
bool noReturnValue = false)
91
var method = new MethodDeclaration { Name = "Method" };
94
method.ReturnType = new PrimitiveType ("void");
96
var type = lambda.GetInferredReturnType (lambda.Parameters.Select (p => p.Type).ToArray ());
97
method.ReturnType = type.Name == "?" ? new PrimitiveType ("void") : context.CreateShortType (type);
100
foreach (var param in lambda.Parameters)
101
method.Parameters.Add (new ParameterDeclaration (context.CreateShortType (param.Type), param.Name));
105
method.Modifiers |= Modifiers.Async;
110
static bool ContainsLocalReferences (RefactoringContext context, AstNode expr, AstNode body)
112
var visitor = new ExtractMethod.VariableLookupVisitor (context);
113
body.AcceptVisitor (visitor);
114
return visitor.UsedVariables.Any (variable => !expr.Contains (variable.Region.Begin));