static TypeDeclaration CreateClassFromObjectCreation(RefactoringContext context, ObjectCreateExpression createExpression)
        {
            TypeDeclaration result;
            string          className = createExpression.Type.ToString();

            if (!createExpression.Arguments.Any())
            {
                result = new TypeDeclaration {
                    Name = className
                };
            }
            else
            {
                var decl = new ConstructorDeclaration {
                    Name      = className,
                    Modifiers = Modifiers.Public,
                    Body      = new BlockStatement {
                        new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
                    }
                };
                result = new TypeDeclaration {
                    Name    = className,
                    Members =
                    {
                        decl
                    }
                };
                decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));
            }
            var guessedType = TypeGuessing.GuessType(context, createExpression);

            if (guessedType.Kind == TypeKind.Interface || guessedType.Kind == TypeKind.Class && guessedType.GetDefinition().IsAbstract)
            {
                result.BaseTypes.Add(context.CreateShortType(guessedType));
                AddImplementation(context, result, guessedType);
            }

            return(result);
        }
Exemplo n.º 2
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var createExpression = context.GetNode <Expression>() as ObjectCreateExpression;

            if (createExpression == null)
            {
                yield break;
            }

            var resolveResult = context.Resolve(createExpression) as AlInvocationResolveResult;

            if (resolveResult == null || !resolveResult.IsError || resolveResult.Member.DeclaringTypeDefinition == null || resolveResult.Member.DeclaringTypeDefinition.IsSealed || resolveResult.Member.DeclaringTypeDefinition.Region.IsEmpty)
            {
                yield break;
            }

            yield return(new CodeAction(context.TranslateString("Create constructor"), script =>
                                        script.InsertWithCursor(
                                            context.TranslateString("Create constructor"),
                                            resolveResult.Member.DeclaringTypeDefinition,
                                            (s, c) => {
                var decl = new ConstructorDeclaration {
                    Name = resolveResult.Member.DeclaringTypeDefinition.Name,
                    Modifiers = Modifiers.Public,
                    Body = new BlockStatement {
                        new ThrowStatement(new ObjectCreateExpression(c.CreateShortType("System", "NotImplementedException")))
                    }
                };
                decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));
                return decl;
            }
                                            )
                                        , createExpression)
            {
                Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
            });
        }
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var indexer = context.GetNode <IndexerExpression>();

            if (indexer == null)
            {
                yield break;
            }
            if (!(context.Resolve(indexer).IsError))
            {
                yield break;
            }

            var state = context.GetResolverStateBefore(indexer);

            if (state.CurrentTypeDefinition == null)
            {
                yield break;
            }
            var guessedType = TypeGuessing.GuessAstType(context, indexer);

            bool          createInOtherType   = false;
            ResolveResult targetResolveResult = null;

            targetResolveResult = context.Resolve(indexer.Target);
            createInOtherType   = !state.CurrentTypeDefinition.Equals(targetResolveResult.Type.GetDefinition());

            bool isStatic;

            if (createInOtherType)
            {
                if (targetResolveResult.Type.GetDefinition() == null || targetResolveResult.Type.GetDefinition().Region.IsEmpty)
                {
                    yield break;
                }
                isStatic = targetResolveResult is TypeResolveResult;
                if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface || targetResolveResult.Type.Kind == TypeKind.Enum)
                {
                    yield break;
                }
            }
            else
            {
                isStatic = indexer.Target is IdentifierExpression && state.CurrentMember.IsStatic;
            }

            yield return(new CodeAction(context.TranslateString("Create indexer"), script => {
                var decl = new IndexerDeclaration()
                {
                    ReturnType = guessedType,
                    Getter = new Accessor()
                    {
                        Body = new BlockStatement()
                        {
                            new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
                        }
                    },
                    Setter = new Accessor()
                    {
                        Body = new BlockStatement()
                        {
                            new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
                        }
                    },
                };
                decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, indexer.Arguments));
                if (isStatic)
                {
                    decl.Modifiers |= Modifiers.Static;
                }

                if (createInOtherType)
                {
                    if (targetResolveResult.Type.Kind == TypeKind.Interface)
                    {
                        decl.Getter.Body = null;
                        decl.Setter.Body = null;
                        decl.Modifiers = Modifiers.None;
                    }
                    else
                    {
                        decl.Modifiers |= Modifiers.Public;
                    }

                    script.InsertWithCursor(context.TranslateString("Create indexer"), targetResolveResult.Type.GetDefinition(), (s, c) => decl);
                    return;
                }

                script.InsertWithCursor(context.TranslateString("Create indexer"), Script.InsertPosition.Before, decl);
            }, indexer)
            {
                Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
            });
        }