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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateClassDeclarationAction.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
 
// CreateClassDeclarationAction.cs
3
 
//  
4
 
// Author:
5
 
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
6
 
// 
7
 
// Copyright (c) 2012 Xamarin <http://xamarin.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
 
 
27
 
using System.Collections.Generic;
28
 
using ICSharpCode.NRefactory.Semantics;
29
 
using System.Linq;
30
 
using ICSharpCode.NRefactory.TypeSystem;
31
 
 
32
 
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
 
{
34
 
        [ContextAction("Create class", Description = "Creates a class declaration out of an object creation.")]
35
 
        public class CreateClassDeclarationAction : ICodeActionProvider
36
 
        {
37
 
                public IEnumerable<CodeAction> GetActions(RefactoringContext context)
38
 
                {
39
 
                        var createExpression = context.GetNode<ObjectCreateExpression>();
40
 
                        if (createExpression != null) 
41
 
                                return GetActions(context, createExpression);
42
 
                        
43
 
                        var simpleType = context.GetNode<SimpleType>();
44
 
                        if (simpleType != null && !(simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration)) 
45
 
                                return GetActions(context, simpleType);
46
 
 
47
 
                        return Enumerable.Empty<CodeAction>();
48
 
                }
49
 
 
50
 
                static IEnumerable<CodeAction> GetActions(RefactoringContext context, AstNode node)
51
 
                {
52
 
                        var resolveResult = context.Resolve(node) as UnknownIdentifierResolveResult;
53
 
                        if (resolveResult == null)
54
 
                                yield break;
55
 
 
56
 
                        var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
57
 
                        if (service != null && !service.IsValidName(resolveResult.Identifier, AffectedEntity.Class)) { 
58
 
                                yield break;
59
 
                        }
60
 
 
61
 
                        yield return new CodeAction(context.TranslateString("Create class"), script => {
62
 
                                script.CreateNewType(CreateType(context, service, node));
63
 
                        });
64
 
 
65
 
                        yield return new CodeAction(context.TranslateString("Create nested class"), script => {
66
 
                                script.InsertWithCursor(
67
 
                                        context.TranslateString("Create nested class"),
68
 
                                        Script.InsertPosition.Before,
69
 
                                        CreateType(context, service, node)
70
 
                                );
71
 
                        });
72
 
                }
73
 
 
74
 
                static TypeDeclaration CreateType(RefactoringContext context, NamingConventionService service, AstNode node)
75
 
                {
76
 
                        var result = node is SimpleType ?
77
 
                                CreateClassFromType(context, (SimpleType)node) : 
78
 
                                CreateClassFromObjectCreation(context, (ObjectCreateExpression)node);
79
 
 
80
 
                        return AddBaseTypesAccordingToNamingRules(context, service, result);
81
 
                }
82
 
 
83
 
                static TypeDeclaration CreateClassFromType(RefactoringContext context, SimpleType simpleType)
84
 
                {
85
 
                        TypeDeclaration result;
86
 
                        string className = simpleType.Identifier;
87
 
 
88
 
                        if (simpleType.Parent is Attribute) {
89
 
                                if (!className.EndsWith("Attribute"))
90
 
                                        className += "Attribute";
91
 
                        }
92
 
 
93
 
                        result = new TypeDeclaration() { Name = className };
94
 
                        var entity = simpleType.GetParent<EntityDeclaration>();
95
 
                        if (entity != null)
96
 
                                result.Modifiers |= entity.Modifiers & ~Modifiers.Internal;
97
 
 
98
 
                        return result;
99
 
                }
100
 
 
101
 
                static TypeDeclaration CreateClassFromObjectCreation(RefactoringContext context, ObjectCreateExpression createExpression)
102
 
                {
103
 
                        TypeDeclaration result;
104
 
                        string className = createExpression.Type.GetText();
105
 
                        if (!createExpression.Arguments.Any()) {
106
 
                                result = new TypeDeclaration() { Name = className };
107
 
                        } else {
108
 
                                var decl = new ConstructorDeclaration() {
109
 
                                        Name = className,
110
 
                                        Modifiers = Modifiers.Public,
111
 
                                        Body = new BlockStatement() {
112
 
                                                new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
113
 
                                        }
114
 
                                };
115
 
                                result = new TypeDeclaration() {
116
 
                                        Name = className,
117
 
                                        Members = {
118
 
                                                decl
119
 
                                        }
120
 
                                };
121
 
                                decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));
122
 
                        }
123
 
                        var guessedType = CreateFieldAction.GuessType(context, createExpression);
124
 
                        if (guessedType.Kind == TypeKind.Interface || guessedType.Kind == TypeKind.Class && guessedType.GetDefinition ().IsAbstract) {
125
 
                                result.BaseTypes.Add(context.CreateShortType(guessedType));
126
 
                                AddImplementation(context, result, guessedType);
127
 
                        }
128
 
                        
129
 
                        return result;
130
 
                }
131
 
 
132
 
                static Modifiers GetModifiers(IEntity property)
133
 
                {
134
 
                        if (property.DeclaringType.Kind == TypeKind.Interface)
135
 
                                return Modifiers.Public;
136
 
                        switch (property.Accessibility) {
137
 
                                case Accessibility.Public:
138
 
                                        return Modifiers.Public | Modifiers.Override;
139
 
                                case Accessibility.Protected:
140
 
                                        return Modifiers.Protected | Modifiers.Override;
141
 
                                case Accessibility.Internal:
142
 
                                        return Modifiers.Internal | Modifiers.Override;
143
 
                                case Accessibility.ProtectedOrInternal:
144
 
                                        // TODO: oops
145
 
                                        return Modifiers.Internal | Modifiers.Protected | Modifiers.Override;
146
 
                                case Accessibility.ProtectedAndInternal:
147
 
                                        // TODO: oops
148
 
                                        return Modifiers.Internal | Modifiers.Protected | Modifiers.Override;
149
 
                        }
150
 
                        return Modifiers.Override;
151
 
                }
152
 
 
153
 
                static void AddImplementation(RefactoringContext context, TypeDeclaration result, ICSharpCode.NRefactory.TypeSystem.IType guessedType)
154
 
                {
155
 
                        foreach (var property in guessedType.GetProperties ()) {
156
 
                                if (!property.IsAbstract)
157
 
                                        continue;
158
 
                                if (property.IsIndexer) {
159
 
                                        var indexerDecl = new IndexerDeclaration() {
160
 
                                                ReturnType = context.CreateShortType(property.ReturnType),
161
 
                                                Modifiers = GetModifiers(property),
162
 
                                                Name = property.Name
163
 
                                        };
164
 
                                        indexerDecl.Parameters.AddRange(ConvertParameters(context, property.Parameters));
165
 
                                        if (property.CanGet)
166
 
                                                indexerDecl.Getter = new Accessor();
167
 
                                        if (property.CanSet)
168
 
                                                indexerDecl.Setter = new Accessor();
169
 
                                        result.AddChild(indexerDecl, Roles.TypeMemberRole);
170
 
                                        continue;
171
 
                                }
172
 
                                var propDecl = new PropertyDeclaration() {
173
 
                                        ReturnType = context.CreateShortType(property.ReturnType),
174
 
                                        Modifiers = GetModifiers (property),
175
 
                                        Name = property.Name
176
 
                                };
177
 
                                if (property.CanGet)
178
 
                                        propDecl.Getter = new Accessor();
179
 
                                if (property.CanSet)
180
 
                                        propDecl.Setter = new Accessor();
181
 
                                result.AddChild(propDecl, Roles.TypeMemberRole);
182
 
                        }
183
 
                        
184
 
                        foreach (var method in guessedType.GetMethods ()) {
185
 
                                if (!method.IsAbstract)
186
 
                                        continue;
187
 
                                var decl = new MethodDeclaration() {
188
 
                                        ReturnType = context.CreateShortType(method.ReturnType),
189
 
                                        Modifiers = GetModifiers (method),
190
 
                                        Name = method.Name,
191
 
                                        Body = new BlockStatement() {
192
 
                                                new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
193
 
                                        }
194
 
                                };
195
 
                                decl.Parameters.AddRange(ConvertParameters(context, method.Parameters));
196
 
                                result.AddChild(decl, Roles.TypeMemberRole);
197
 
                        }
198
 
 
199
 
                        foreach (var evt in guessedType.GetEvents ()) {
200
 
                                if (!evt.IsAbstract)
201
 
                                        continue;
202
 
                                var decl = new EventDeclaration() {
203
 
                                        ReturnType = context.CreateShortType(evt.ReturnType),
204
 
                                        Modifiers = GetModifiers (evt),
205
 
                                        Name = evt.Name
206
 
                                };
207
 
                                result.AddChild(decl, Roles.TypeMemberRole);
208
 
                        }
209
 
                }
210
 
 
211
 
                static IEnumerable<ParameterDeclaration> ConvertParameters(RefactoringContext context, IList<IParameter> parameters)
212
 
                {
213
 
                        foreach (var param in parameters) {
214
 
                                ParameterModifier mod = ParameterModifier.None;
215
 
                                if (param.IsOut) {
216
 
                                        mod = ParameterModifier.Out;
217
 
                                } else if (param.IsRef) {
218
 
                                        mod = ParameterModifier.Ref;
219
 
                                } else if (param.IsParams) {
220
 
                                        mod = ParameterModifier.Params;
221
 
                                }
222
 
                                yield return new ParameterDeclaration(context.CreateShortType(param.Type), param.Name, mod);
223
 
                        }
224
 
                }
225
 
 
226
 
                static TypeDeclaration AddBaseTypesAccordingToNamingRules(RefactoringContext context, NamingConventionService service, TypeDeclaration result)
227
 
                {
228
 
                        if (service.HasValidRule(result.Name, AffectedEntity.CustomAttributes, Modifiers.Public)) {
229
 
                                result.BaseTypes.Add(context.CreateShortType("System", "Attribute"));
230
 
                        } else if (service.HasValidRule(result.Name, AffectedEntity.CustomEventArgs, Modifiers.Public)) {
231
 
                                result.BaseTypes.Add(context.CreateShortType("System", "EventArgs"));
232
 
                        } else if (service.HasValidRule(result.Name, AffectedEntity.CustomExceptions, Modifiers.Public)) {
233
 
                                result.BaseTypes.Add(context.CreateShortType("System", "Exception"));
234
 
                        }
235
 
                        return result;
236
 
                }
237
 
        }
238
 
}
 
 
b'\\ No newline at end of file'