PageRenderTime 60ms CodeModel.GetById 51ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/icsharpcode/ILSpy
C# | 89 lines | 51 code | 10 blank | 28 comment | 17 complexity | 5cd6190deabd6928b049efdf8dfa6ecf MD5 | raw file
 1// 
 2// AddAnotherAccessor.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
27namespace ICSharpCode.NRefactory.CSharp.Refactoring
28{
29	/// <summary>
30	/// Add another accessor to a property declaration that has only one.
31	/// </summary>
32	public class AddAnotherAccessor : IContextAction
33	{
34		public bool IsValid (RefactoringContext context)
35		{
36			var pdecl = GetPropertyDeclaration (context);
37			if (pdecl == null)
38				return false;
39			var type = pdecl.Parent as TypeDeclaration;
40			if (type != null && type.ClassType == ClassType.Interface)
41				return false;
42			
43			return pdecl.Setter.IsNull || pdecl.Getter.IsNull;
44		}
45		
46		public void Run (RefactoringContext context)
47		{
48			var pdecl = GetPropertyDeclaration (context);
49			var accessorStatement = BuildAccessorStatement (context, pdecl);
50			
51			Accessor accessor = new Accessor () {
52				Body = new BlockStatement { accessorStatement }
53			};
54			
55			pdecl.AddChild (accessor, pdecl.Setter.IsNull ? PropertyDeclaration.SetterRole : PropertyDeclaration.GetterRole);
56			
57			using (var script = context.StartScript ()) {
58				script.InsertBefore (pdecl.RBraceToken, accessor);
59				script.Select (accessorStatement);
60				script.FormatText (ctx => GetPropertyDeclaration (context));
61			}
62		}
63
64		static Statement BuildAccessorStatement (RefactoringContext context, PropertyDeclaration pdecl)
65		{
66			if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) {
67				var field = RemoveBackingStore.ScanGetter (context, pdecl);
68				if (field != null) 
69					return new ExpressionStatement (new AssignmentExpression (new IdentifierExpression (field.Name), AssignmentOperatorType.Assign, new IdentifierExpression ("value")));
70			}
71			
72			if (!pdecl.Setter.IsNull && pdecl.Getter.IsNull) {
73				var field = RemoveBackingStore.ScanSetter (context, pdecl);
74				if (field != null) 
75					return new ReturnStatement (new IdentifierExpression (field.Name));
76			}
77			
78			return new ThrowStatement (new ObjectCreateExpression (context.CreateShortType ("System", "NotImplementedException")));
79		}
80		
81		static PropertyDeclaration GetPropertyDeclaration (RefactoringContext context)
82		{
83			var node = context.GetNode ();
84			if (node == null)
85				return null;
86			return node.Parent as PropertyDeclaration;
87		}
88	}
89}