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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveBackingStoreAction.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
 
// RemoveBackingStore.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
 
using System;
27
 
using System.Linq;
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;
33
 
 
34
 
namespace ICSharpCode.NRefactory.CSharp.Refactoring
35
 
{
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
38
 
        {
39
 
                public IEnumerable<CodeAction> GetActions(RefactoringContext context)
40
 
                {
41
 
                        var property = context.GetNode<PropertyDeclaration>();
42
 
                        var field = GetBackingField(context);
43
 
                        if (field == null) {
44
 
                                yield break;
45
 
                        }
46
 
                        // create new auto property
47
 
                        var newProperty = (PropertyDeclaration)property.Clone();
48
 
                        newProperty.Getter.Body = BlockStatement.Null;
49
 
                        newProperty.Setter.Body = BlockStatement.Null;
50
 
                        
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
 
                        });
56
 
                }
57
 
                
58
 
//              void ReplaceBackingFieldReferences (MDRefactoringContext context, IField backingStore, PropertyDeclaration property)
59
 
//              {
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))
63
 
//                                              continue;
64
 
//                                      if (backingStore.Location.Line == memberRef.Line && backingStore.Location.Column == memberRef.Column)
65
 
//                                              continue;
66
 
//                                      context.Do (new TextReplaceChange () {
67
 
//                                              FileName = memberRef.FileName,
68
 
//                                              Offset = memberRef.Position,
69
 
//                                              RemovedChars = memberRef.Name.Length,
70
 
//                                              InsertedText = property.Name
71
 
//                                      });
72
 
//                              }
73
 
//                      }
74
 
//              }
75
 
//
76
 
                static readonly Version csharp3 = new Version(3, 0);
77
 
                
78
 
                static IField GetBackingField (RefactoringContext context)
79
 
                {
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)
83
 
                                return null;
84
 
                        if (!context.Supports(csharp3) || propertyDeclaration.HasModifier (ICSharpCode.NRefactory.CSharp.Modifiers.Abstract) || ((TypeDeclaration)propertyDeclaration.Parent).ClassType == ClassType.Interface)
85
 
                                return null;
86
 
                        var getterField = ScanGetter (context, propertyDeclaration);
87
 
                        if (getterField == null)
88
 
                                return null;
89
 
                        var setterField = ScanSetter (context, propertyDeclaration);
90
 
                        if (setterField == null)
91
 
                                return null;
92
 
                        if (getterField.Region != setterField.Region)
93
 
                                return null;
94
 
                        return getterField;
95
 
                }
96
 
                
97
 
                internal static IField ScanGetter (RefactoringContext context, PropertyDeclaration propertyDeclaration)
98
 
                {
99
 
                        if (propertyDeclaration.Getter.Body.Statements.Count != 1)
100
 
                                return null;
101
 
                        var returnStatement = propertyDeclaration.Getter.Body.Statements.First () as ReturnStatement;
102
 
                        if (returnStatement == null)
103
 
                                return null;
104
 
                        var result = context.Resolve (returnStatement.Expression);
105
 
                        if (result == null || !(result is MemberResolveResult))
106
 
                                return null;
107
 
                        return ((MemberResolveResult)result).Member as IField;
108
 
                }
109
 
                
110
 
                internal static IField ScanSetter (RefactoringContext context, PropertyDeclaration propertyDeclaration)
111
 
                {
112
 
                        if (propertyDeclaration.Setter.Body.Statements.Count != 1)
113
 
                                return null;
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)
117
 
                                return null;
118
 
                        var result = context.Resolve (assignment.Left);
119
 
                        if (result == null || !(result is MemberResolveResult))
120
 
                                return null;
121
 
                        return ((MemberResolveResult)result).Member as IField;
122
 
                }
123
 
        }
124
 
}
125