PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/ContextAction/InsertAnonymousMethodSignature.cs

http://github.com/icsharpcode/ILSpy
C# | 94 lines | 55 code | 13 blank | 26 comment | 16 complexity | 868f66d16994f58f0f0a2c53b617de09 MD5 | raw file
 1// 
 2// InsertAnonymousMethodSignature.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.
26using System;
27using ICSharpCode.NRefactory.TypeSystem;
28using System.Linq;
29using System.Text;
30
31namespace ICSharpCode.NRefactory.CSharp.Refactoring
32{
33	public class InsertAnonymousMethodSignature : IContextAction
34	{
35		public bool IsValid (RefactoringContext context)
36		{
37			IType type;
38			return GetAnonymousMethodExpression (context, out type) != null;
39		}
40		
41		public void Run (RefactoringContext context)
42		{
43			IType type;
44			var anonymousMethodExpression = GetAnonymousMethodExpression (context, out type);
45			
46			var delegateMethod = type.GetDelegateInvokeMethod ();
47			
48			var sb = new StringBuilder ("(");
49			for (int k = 0; k < delegateMethod.Parameters.Count; k++) {
50				if (k > 0)
51					sb.Append (", ");
52				
53				var paramType = delegateMethod.Parameters [k].Type;
54				
55				sb.Append (paramType.ToString ());
56				sb.Append (" ");
57				sb.Append (delegateMethod.Parameters [k].Name);
58			}
59			sb.Append (")");
60			
61			using (var script = context.StartScript ()) {
62				script.InsertText (context.GetOffset (anonymousMethodExpression.DelegateToken.EndLocation), sb.ToString ());
63			}
64		}
65		
66		static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out IType delegateType)
67		{
68			delegateType = null;
69			
70			var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> ();
71			if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location.Line, context.Location.Column) || anonymousMethodExpression.HasParameterList)
72				return null;
73			
74			IType resolvedType = null;
75			var parent = anonymousMethodExpression.Parent;
76			if (parent is AssignmentExpression) {
77				resolvedType = context.Resolve (((AssignmentExpression)parent).Left).Type;
78			} else if (parent is VariableDeclarationStatement) {
79				resolvedType = context.Resolve (((VariableDeclarationStatement)parent).Type).Type;
80			} else if (parent is InvocationExpression) {
81				// TODO: handle invocations
82			}
83			
84			if (resolvedType == null)
85				return null;
86			delegateType = resolvedType;
87			if (delegateType.Kind != TypeKind.Delegate) 
88				return null;
89			
90			return anonymousMethodExpression;
91		}
92	}
93}
94