public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var identifier = context.GetNode <IdentifierExpression>();

            if (identifier == null)
            {
                yield break;
            }
            if (IsInvocationTarget(identifier))
            {
                yield break;
            }
            var statement = identifier.GetParent <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.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(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(identifier.Identifier) }
                };
                if (isStatic)
                {
                    decl.Modifiers |= Modifiers.Static;
                }
                script.InsertWithCursor(context.TranslateString("Create field"), decl, Script.InsertPosition.Before);
            }));
        }
Beispiel #2
0
        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);
                }
            }));
        }
        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));
        }
Beispiel #4
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));
        }
        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);
            }, expr));
        }
Beispiel #6
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);
            }));
        }