2
// CreateClassDeclarationAction.cs
5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin <http://xamarin.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
27
using System.Collections.Generic;
28
using ICSharpCode.NRefactory.Semantics;
30
using ICSharpCode.NRefactory.TypeSystem;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
34
[ContextAction("Create class", Description = "Creates a class declaration out of an object creation.")]
35
public class CreateClassDeclarationAction : ICodeActionProvider
37
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
39
var createExpression = context.GetNode<ObjectCreateExpression>();
40
if (createExpression != null)
41
return GetActions(context, createExpression);
43
var simpleType = context.GetNode<SimpleType>();
44
if (simpleType != null && !(simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration))
45
return GetActions(context, simpleType);
47
return Enumerable.Empty<CodeAction>();
50
static IEnumerable<CodeAction> GetActions(RefactoringContext context, AstNode node)
52
var resolveResult = context.Resolve(node) as UnknownIdentifierResolveResult;
53
if (resolveResult == null)
56
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
57
if (service != null && !service.IsValidName(resolveResult.Identifier, AffectedEntity.Class)) {
61
yield return new CodeAction(context.TranslateString("Create class"), script => {
62
script.CreateNewType(CreateType(context, service, node));
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)
74
static TypeDeclaration CreateType(RefactoringContext context, NamingConventionService service, AstNode node)
76
var result = node is SimpleType ?
77
CreateClassFromType(context, (SimpleType)node) :
78
CreateClassFromObjectCreation(context, (ObjectCreateExpression)node);
80
return AddBaseTypesAccordingToNamingRules(context, service, result);
83
static TypeDeclaration CreateClassFromType(RefactoringContext context, SimpleType simpleType)
85
TypeDeclaration result;
86
string className = simpleType.Identifier;
88
if (simpleType.Parent is Attribute) {
89
if (!className.EndsWith("Attribute"))
90
className += "Attribute";
93
result = new TypeDeclaration() { Name = className };
94
var entity = simpleType.GetParent<EntityDeclaration>();
96
result.Modifiers |= entity.Modifiers & ~Modifiers.Internal;
101
static TypeDeclaration CreateClassFromObjectCreation(RefactoringContext context, ObjectCreateExpression createExpression)
103
TypeDeclaration result;
104
string className = createExpression.Type.GetText();
105
if (!createExpression.Arguments.Any()) {
106
result = new TypeDeclaration() { Name = className };
108
var decl = new ConstructorDeclaration() {
110
Modifiers = Modifiers.Public,
111
Body = new BlockStatement() {
112
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
115
result = new TypeDeclaration() {
121
decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));
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);
132
static Modifiers GetModifiers(IEntity property)
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:
145
return Modifiers.Internal | Modifiers.Protected | Modifiers.Override;
146
case Accessibility.ProtectedAndInternal:
148
return Modifiers.Internal | Modifiers.Protected | Modifiers.Override;
150
return Modifiers.Override;
153
static void AddImplementation(RefactoringContext context, TypeDeclaration result, ICSharpCode.NRefactory.TypeSystem.IType guessedType)
155
foreach (var property in guessedType.GetProperties ()) {
156
if (!property.IsAbstract)
158
if (property.IsIndexer) {
159
var indexerDecl = new IndexerDeclaration() {
160
ReturnType = context.CreateShortType(property.ReturnType),
161
Modifiers = GetModifiers(property),
164
indexerDecl.Parameters.AddRange(ConvertParameters(context, property.Parameters));
166
indexerDecl.Getter = new Accessor();
168
indexerDecl.Setter = new Accessor();
169
result.AddChild(indexerDecl, Roles.TypeMemberRole);
172
var propDecl = new PropertyDeclaration() {
173
ReturnType = context.CreateShortType(property.ReturnType),
174
Modifiers = GetModifiers (property),
178
propDecl.Getter = new Accessor();
180
propDecl.Setter = new Accessor();
181
result.AddChild(propDecl, Roles.TypeMemberRole);
184
foreach (var method in guessedType.GetMethods ()) {
185
if (!method.IsAbstract)
187
var decl = new MethodDeclaration() {
188
ReturnType = context.CreateShortType(method.ReturnType),
189
Modifiers = GetModifiers (method),
191
Body = new BlockStatement() {
192
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
195
decl.Parameters.AddRange(ConvertParameters(context, method.Parameters));
196
result.AddChild(decl, Roles.TypeMemberRole);
199
foreach (var evt in guessedType.GetEvents ()) {
202
var decl = new EventDeclaration() {
203
ReturnType = context.CreateShortType(evt.ReturnType),
204
Modifiers = GetModifiers (evt),
207
result.AddChild(decl, Roles.TypeMemberRole);
211
static IEnumerable<ParameterDeclaration> ConvertParameters(RefactoringContext context, IList<IParameter> parameters)
213
foreach (var param in parameters) {
214
ParameterModifier mod = ParameterModifier.None;
216
mod = ParameterModifier.Out;
217
} else if (param.IsRef) {
218
mod = ParameterModifier.Ref;
219
} else if (param.IsParams) {
220
mod = ParameterModifier.Params;
222
yield return new ParameterDeclaration(context.CreateShortType(param.Type), param.Name, mod);
226
static TypeDeclaration AddBaseTypesAccordingToNamingRules(RefactoringContext context, NamingConventionService service, TypeDeclaration result)
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"));
b'\\ No newline at end of file'