Ejemplo n.º 1
0
        static TypeDeclaration CreateClassFromType(RefactoringContext context, ClassType classType, SimpleType simpleType)
        {
            TypeDeclaration result;
            string          className = simpleType.Identifier;

            if (simpleType.Parent is Attribute && classType == ClassType.Class)
            {
                if (!className.EndsWith("Attribute", System.StringComparison.Ordinal))
                {
                    className += "Attribute";
                }
            }

            result = new TypeDeclaration {
                Name = className, ClassType = classType
            };
            var entity = simpleType.GetParent <EntityDeclaration>();

            if (entity != null)
            {
                result.Modifiers |= entity.Modifiers & Modifiers.Public;
            }

            var guessedType = CreateFieldAction.GuessType(context, simpleType);

            if (guessedType.Kind == TypeKind.TypeParameter)
            {
                ImplementConstraints(context, result, (ITypeParameter)guessedType);
            }
            return(result);
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var expr = GetCreatePropertyOrFieldNode(context);

            if (expr == null)
            {
                yield break;
            }
            if (!(expr is MemberReferenceExpression))
            {
                yield break;
            }
            var propertyName = CreatePropertyAction.GetPropertyName(expr);

            if (propertyName == null)
            {
                yield break;
            }
            if (IsInvocationTarget(expr))
            {
                yield break;
            }
            var statement = expr.GetParent <Statement>();

            if (statement == null)
            {
                yield break;
            }
            if (!(context.Resolve(expr).IsError))
            {
                yield break;
            }
            var guessedType = CreateFieldAction.GuessType(context, expr);

            if (guessedType == null || guessedType.Kind != TypeKind.Enum)
            {
                yield break;
            }
            var state = context.GetResolverStateBefore(expr);

            if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
            {
                yield break;
            }

            yield return(new CodeAction(context.TranslateString("Create enum value"), script => {
                var decl = new EnumMemberDeclaration {
                    Name = propertyName
                };
                script.InsertWithCursor(context.TranslateString("Create enum value"), guessedType.GetDefinition(), decl);
            }));
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var identifier = context.GetNode <IdentifierExpression>();

            if (identifier == null)
            {
                yield break;
            }
            if (CreateFieldAction.IsInvocationTarget(identifier))
            {
                yield break;
            }
            var statement = context.GetNode <Statement>();

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

            if (!(context.Resolve(identifier).IsError))
            {
                yield break;
            }
            var guessedType = CreateFieldAction.GuessAstType(context, identifier);

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

            yield return(new CodeAction(context.TranslateString("Create local variable"), script => {
                var initializer = new VariableInitializer(identifier.Identifier);
                var decl = new VariableDeclarationStatement()
                {
                    Type = guessedType,
                    Variables = { initializer }
                };
                if (identifier.Parent is AssignmentExpression && ((AssignmentExpression)identifier.Parent).Left == identifier)
                {
                    initializer.Initializer = ((AssignmentExpression)identifier.Parent).Right.Clone();
                    if (!context.UseExplicitTypes)
                    {
                        decl.Type = new SimpleType("var");
                    }
                    script.Replace(statement, decl);
                }
                else
                {
                    script.InsertBefore(statement, decl);
                }
            }));
        }
Ejemplo n.º 4
0
        IEnumerable <CodeAction> GetActionsFromIdentifier(RefactoringContext context, IdentifierExpression identifier)
        {
            if (!(context.Resolve(identifier).IsError))
            {
                yield break;
            }
            var methodName  = identifier.Identifier;
            var guessedType = CreateFieldAction.GuessType(context, identifier);

            if (guessedType.Kind != TypeKind.Delegate)
            {
                yield break;
            }
            var invocationMethod = guessedType.GetDelegateInvokeMethod();

            if (invocationMethod == null)
            {
                yield break;
            }
            var state = context.GetResolverStateBefore(identifier);

            if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
            {
                yield break;
            }
            bool isStatic = state.CurrentMember.IsStatic || state.CurrentTypeDefinition.IsStatic;

            var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));

            if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic))
            {
                yield break;
            }

            yield return(CreateAction(
                             context,
                             identifier,
                             methodName,
                             context.CreateShortType(invocationMethod.ReturnType),
                             invocationMethod.Parameters.Select(parameter => new ParameterDeclaration(context.CreateShortType(parameter.Type), parameter.Name)
            {
                ParameterModifier = GetModifiers(parameter)
            }),
                             false,
                             isStatic,
                             null));
        }
Ejemplo n.º 5
0
        static void ModifyClassTypeBasedOnTypeGuessing(RefactoringContext context, AstNode node, ref ClassType classType)
        {
            var guessedType = CreateFieldAction.GuessType(context, node);

            if (guessedType.Kind == TypeKind.TypeParameter)
            {
                var tp = (ITypeParameter)guessedType;
                if (tp.HasValueTypeConstraint)
                {
                    classType = ClassType.Struct;
                }
                if (tp.HasReferenceTypeConstraint)
                {
                    classType = ClassType.Class;
                }
            }
        }
Ejemplo n.º 6
0
        static TypeDeclaration CreateClassFromObjectCreation(RefactoringContext context, ObjectCreateExpression createExpression)
        {
            TypeDeclaration result;
            string          className = createExpression.Type.GetText();

            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 = CreateFieldAction.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);
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var expr = GetCreatePropertyOrFieldNode(context);

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

            if (expr is MemberReferenceExpression && !(((MemberReferenceExpression)expr).Target is ThisReferenceExpression))
            {
                yield break;
            }

            var propertyName = CreatePropertyAction.GetPropertyName(expr);

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

            if (IsInvocationTarget(expr))
            {
                yield break;
            }
            var statement = expr.GetParent <Statement>();

            if (statement == null)
            {
                yield break;
            }
            if (!(context.Resolve(expr).IsError))
            {
                yield break;
            }
            var guessedType = CreateFieldAction.GuessAstType(context, expr);

            if (guessedType == null)
            {
                yield break;
            }
            var state = context.GetResolverStateBefore(expr);

            if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
            {
                yield break;
            }
            bool isStatic = !(expr is NamedExpression) && (state.CurrentMember.IsStatic | state.CurrentTypeDefinition.IsStatic);

//			var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
//			if (service != null && !service.IsValidName(identifier.Identifier, AffectedEntity.Field, Modifiers.Private, isStatic)) {
//				yield break;
//			}

            yield return(new CodeAction(context.TranslateString("Create field"), script => {
                var decl = new FieldDeclaration()
                {
                    ReturnType = guessedType,
                    Variables = { new VariableInitializer(propertyName) }
                };
                if (isStatic)
                {
                    decl.Modifiers |= Modifiers.Static;
                }
                script.InsertWithCursor(context.TranslateString("Create field"), Script.InsertPosition.Before, decl);
            }));
        }
Ejemplo n.º 8
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var identifier = CreateFieldAction.GetCreatePropertyOrFieldNode(context);

            if (identifier == null)
            {
                yield break;
            }
            if (CreateFieldAction.IsInvocationTarget(identifier))
            {
                yield break;
            }

            var propertyName = GetPropertyName(identifier);

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

            var statement = context.GetNode <Statement>();

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

            if (!(context.Resolve(identifier).IsError))
            {
                yield break;
            }

            var guessedType = CreateFieldAction.GuessAstType(context, identifier);

            if (guessedType == null)
            {
                yield break;
            }
            var state = context.GetResolverStateBefore(identifier);

            if (state.CurrentTypeDefinition == null)
            {
                yield break;
            }

            bool          createInOtherType   = false;
            ResolveResult targetResolveResult = null;

            if (identifier is MemberReferenceExpression)
            {
                targetResolveResult = context.Resolve(((MemberReferenceExpression)identifier).Target);
                if (targetResolveResult.Type.GetDefinition() == null || targetResolveResult.Type.GetDefinition().Region.IsEmpty)
                {
                    yield break;
                }
                createInOtherType = !state.CurrentTypeDefinition.Equals(targetResolveResult.Type.GetDefinition());
            }

            bool isStatic = targetResolveResult is TypeResolveResult;

            if (createInOtherType)
            {
                if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface || targetResolveResult.Type.Kind == TypeKind.Enum)
                {
                    yield break;
                }
            }
            else
            {
                if (state.CurrentMember == null)
                {
                    yield break;
                }
                isStatic |= state.CurrentTypeDefinition.IsStatic;
                if (targetResolveResult == null)
                {
                    isStatic |= state.CurrentMember.IsStatic;
                }
            }
            isStatic &= !(identifier is NamedExpression);

            //			var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
//			if (service != null && !service.IsValidName(propertyName, AffectedEntity.Property, Modifiers.Private, isStatic)) {
//				yield break;
//			}

            yield return(new CodeAction(context.TranslateString("Create property"), script => {
                var decl = new PropertyDeclaration()
                {
                    ReturnType = guessedType,
                    Name = propertyName,
                    Getter = new Accessor(),
                    Setter = new Accessor()
                };
                if (isStatic)
                {
                    decl.Modifiers |= Modifiers.Static;
                }

                if (createInOtherType)
                {
                    if (targetResolveResult.Type.Kind == TypeKind.Interface)
                    {
                        decl.Modifiers = Modifiers.None;
                    }
                    else
                    {
                        decl.Modifiers |= Modifiers.Public;
                    }
                    script.InsertWithCursor(
                        context.TranslateString("Create property"),
                        targetResolveResult.Type.GetDefinition(),
                        decl);

                    return;
                }

                script.InsertWithCursor(context.TranslateString("Create property"), Script.InsertPosition.Before, decl);
            }, identifier));
        }
Ejemplo n.º 9
0
        public 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 = CreateFieldAction.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(), decl);
                    return;
                }

                script.InsertWithCursor(context.TranslateString("Create indexer"), Script.InsertPosition.Before, decl);
            }));
        }
Ejemplo n.º 10
0
        IEnumerable <CodeAction> GetActionsFromMemberReferenceExpression(RefactoringContext context, MemberReferenceExpression invocation)
        {
            if (!(context.Resolve(invocation).IsError))
            {
                yield break;
            }

            var methodName  = invocation.MemberName;
            var guessedType = CreateFieldAction.GuessType(context, invocation);

            if (guessedType.Kind != TypeKind.Delegate)
            {
                yield break;
            }
            var invocationMethod = guessedType.GetDelegateInvokeMethod();
            var state            = context.GetResolverStateBefore(invocation);

            if (state.CurrentTypeDefinition == null)
            {
                yield break;
            }
            ResolveResult targetResolveResult = context.Resolve(invocation.Target);
            bool          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
            {
                if (state.CurrentMember == null)
                {
                    yield break;
                }
                isStatic = state.CurrentMember.IsStatic || state.CurrentTypeDefinition.IsStatic;
            }

//			var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
//			if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic)) {
//				yield break;
//			}

            yield return(CreateAction(
                             context,
                             methodName,
                             context.CreateShortType(invocationMethod.ReturnType),
                             invocationMethod.Parameters.Select(parameter => new ParameterDeclaration(context.CreateShortType(parameter.Type), parameter.Name)
            {
                ParameterModifier = GetModifiers(parameter)
            }),
                             createInOtherType,
                             isStatic,
                             targetResolveResult));
        }
Ejemplo n.º 11
0
        IEnumerable <CodeAction> GetActionsFromInvocation(RefactoringContext context, InvocationExpression invocation)
        {
            if (!(context.Resolve(invocation.Target).IsError))
            {
                yield break;
            }

            var methodName = GetMethodName(invocation);

            if (methodName == null)
            {
                yield break;
            }
            var state = context.GetResolverStateBefore(invocation);

            if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
            {
                yield break;
            }
            var guessedType = invocation.Parent is ExpressionStatement ? new PrimitiveType("void") : CreateFieldAction.GuessAstType(context, invocation);

            bool          createInOtherType   = false;
            ResolveResult targetResolveResult = null;

            if (invocation.Target is MemberReferenceExpression)
            {
                targetResolveResult = context.Resolve(((MemberReferenceExpression)invocation.Target).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 = state.CurrentMember.IsStatic || state.CurrentTypeDefinition.IsStatic;
            }

//			var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
//			if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic)) {
//				yield break;
//			}


            yield return(CreateAction(
                             context,
                             methodName,
                             guessedType,
                             GenerateParameters(context, invocation.Arguments),
                             createInOtherType,
                             isStatic,
                             targetResolveResult));
        }