2
// RemoveBackingStore.cs
5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.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
28
using ICSharpCode.NRefactory.CSharp.Resolver;
29
using ICSharpCode.NRefactory.Semantics;
30
using ICSharpCode.NRefactory.TypeSystem;
31
using System.Threading;
32
using System.Collections.Generic;
34
namespace ICSharpCode.NRefactory.CSharp.Refactoring
36
[ContextAction("Remove backing store for property", Description = "Removes the backing store of a property and creates an auto property.")]
37
public class RemoveBackingStoreAction : ICodeActionProvider
39
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
41
var property = context.GetNode<PropertyDeclaration>();
42
var field = GetBackingField(context);
46
// create new auto property
47
var newProperty = (PropertyDeclaration)property.Clone();
48
newProperty.Getter.Body = BlockStatement.Null;
49
newProperty.Setter.Body = BlockStatement.Null;
51
yield return new CodeAction(context.TranslateString("Convert to auto property"), script => {
52
script.Rename((IEntity)field, newProperty.Name);
53
script.Remove (context.RootNode.GetNodeAt<FieldDeclaration> (field.Region.Begin));
54
script.Replace (property, newProperty);
55
}, property.NameToken);
58
// void ReplaceBackingFieldReferences (MDRefactoringContext context, IField backingStore, PropertyDeclaration property)
60
// using (var monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true, true)) {
61
// foreach (var memberRef in MonoDevelop.Ide.FindInFiles.ReferenceFinder.FindReferences (backingStore, monitor)) {
62
// if (property.Contains (memberRef.Line, memberRef.Column))
64
// if (backingStore.Location.Line == memberRef.Line && backingStore.Location.Column == memberRef.Column)
66
// context.Do (new TextReplaceChange () {
67
// FileName = memberRef.FileName,
68
// Offset = memberRef.Position,
69
// RemovedChars = memberRef.Name.Length,
70
// InsertedText = property.Name
76
static readonly Version csharp3 = new Version(3, 0);
78
internal static IField GetBackingField (RefactoringContext context)
80
var propertyDeclaration = context.GetNode<PropertyDeclaration> ();
81
// automatic properties always need getter & setter
82
if (propertyDeclaration == null || propertyDeclaration.Getter.IsNull || propertyDeclaration.Setter.IsNull || propertyDeclaration.Getter.Body.IsNull || propertyDeclaration.Setter.Body.IsNull)
84
if (!context.Supports(csharp3) || propertyDeclaration.HasModifier (ICSharpCode.NRefactory.CSharp.Modifiers.Abstract) || ((TypeDeclaration)propertyDeclaration.Parent).ClassType == ClassType.Interface)
86
var getterField = ScanGetter (context, propertyDeclaration);
87
if (getterField == null)
89
var setterField = ScanSetter (context, propertyDeclaration);
90
if (setterField == null)
92
if (getterField.Region != setterField.Region)
97
internal static IField ScanGetter (RefactoringContext context, PropertyDeclaration propertyDeclaration)
99
if (propertyDeclaration.Getter.Body.Statements.Count != 1)
101
var returnStatement = propertyDeclaration.Getter.Body.Statements.First () as ReturnStatement;
102
if (returnStatement == null)
104
var result = context.Resolve (returnStatement.Expression);
105
if (result == null || !(result is MemberResolveResult))
107
return ((MemberResolveResult)result).Member as IField;
110
internal static IField ScanSetter (RefactoringContext context, PropertyDeclaration propertyDeclaration)
112
if (propertyDeclaration.Setter.Body.Statements.Count != 1)
114
var setAssignment = propertyDeclaration.Setter.Body.Statements.First () as ExpressionStatement;
115
var assignment = setAssignment != null ? setAssignment.Expression as AssignmentExpression : null;
116
if (assignment == null || assignment.Operator != AssignmentOperatorType.Assign)
118
var result = context.Resolve (assignment.Left);
119
if (result == null || !(result is MemberResolveResult))
121
return ((MemberResolveResult)result).Member as IField;