Ejemplo n.º 1
0
        public void Run(RefactoringContext context)
        {
            IType type;
            var   anonymousMethodExpression = GetAnonymousMethodExpression(context, out type);

            var delegateMethod = type.GetDelegateInvokeMethod();

            var sb = new StringBuilder("(");

            for (int k = 0; k < delegateMethod.Parameters.Count; k++)
            {
                if (k > 0)
                {
                    sb.Append(", ");
                }

                var paramType = delegateMethod.Parameters [k].Type;

                sb.Append(context.CreateShortType(paramType));
                sb.Append(" ");
                sb.Append(delegateMethod.Parameters [k].Name);
            }
            sb.Append(")");

            using (var script = context.StartScript()) {
                script.InsertText(context.GetOffset(anonymousMethodExpression.DelegateToken.EndLocation), sb.ToString());
            }
        }
Ejemplo n.º 2
0
        public void Run(RefactoringContext context)
        {
            var property = context.GetNode <PropertyDeclaration> ();

            string backingStoreName = context.GetNameProposal(property.Name);

            // create field
            var backingStore = new FieldDeclaration();

            backingStore.ReturnType = property.ReturnType.Clone();

            var initializer = new VariableInitializer(backingStoreName);

            backingStore.Variables.Add(initializer);

            // create new property & implement the get/set bodies
            var newProperty = (PropertyDeclaration)property.Clone();
            var id1         = new IdentifierExpression(backingStoreName);
            var id2         = new IdentifierExpression(backingStoreName);

            newProperty.Getter.Body = new BlockStatement()
            {
                new ReturnStatement(id1)
            };
            newProperty.Setter.Body = new BlockStatement()
            {
                new ExpressionStatement(new AssignmentExpression(id2, AssignmentOperatorType.Assign, new IdentifierExpression("value")))
            };

            using (var script = context.StartScript()) {
                script.Replace(property, newProperty);
                script.InsertBefore(property, backingStore);
                script.Link(initializer, id1, id2);
            }
        }
Ejemplo n.º 3
0
        public void Run(RefactoringContext context)
        {
            var expr = GetEmptyString(context);

            using (var script = context.StartScript()) {
                script.Replace(expr, new MemberReferenceExpression(new TypeReferenceExpression(new PrimitiveType("string")), "Empty"));
            }
        }
Ejemplo n.º 4
0
        public void Run(RefactoringContext context)
        {
            var varDecl = GetVariableDeclarationStatement(context);

            using (var script = context.StartScript()) {
                script.Replace(varDecl.Type, new SimpleType("var"));
            }
        }
Ejemplo n.º 5
0
        public void Run(RefactoringContext context)
        {
            var identifier = GetIdentifier(context);

            using (var script = context.StartScript()) {
                script.InsertWithCursor("Create field", GenerateFieldDeclaration(context, identifier), Script.InsertPosition.Before);
            }
        }
Ejemplo n.º 6
0
        public void Run(RefactoringContext context)
        {
            var varDecl = GetVariableDeclarationStatement(context);

            using (var script = context.StartScript()) {
                var type = context.Resolve(varDecl.Variables.First().Initializer).Type;
                script.Replace(varDecl.Type, context.CreateShortType(type));
            }
        }
Ejemplo n.º 7
0
        public void Run(RefactoringContext context)
        {
            var initializer = GetVariableInitializer(context);
            var field       = initializer.Parent as FieldDeclaration;

            using (var script = context.StartScript()) {
                script.InsertWithCursor("Create getter", GeneratePropertyDeclaration(context, field, initializer), Script.InsertPosition.After);
            }
        }
Ejemplo n.º 8
0
        public void Run(RefactoringContext context)
        {
            var binop = GetBinaryOperatorExpression(context);

            using (var script = context.StartScript()) {
                script.Replace(binop.Left, binop.Right);
                script.Replace(binop.Right, binop.Left);
            }
        }
Ejemplo n.º 9
0
        public void Run(RefactoringContext context)
        {
            var pexpr  = context.GetNode <PrimitiveExpression> ();
            int offset = context.GetOffset(context.Location);

            using (var script = context.StartScript()) {
                script.InsertText(offset, pexpr.LiteralValue.StartsWith("@") ? "\" + @\"" : "\" + \"");
            }
        }
Ejemplo n.º 10
0
        public void Run(RefactoringContext context)
        {
            var block = GetBlockStatement(context);

            using (var script = context.StartScript()) {
                script.Remove(block.LBraceToken);
                script.Remove(block.RBraceToken);
                script.FormatText(ctx => ctx.Unit.GetNodeAt(block.Parent.StartLocation));
            }
        }
Ejemplo n.º 11
0
        void AddFormatCallToInvocation(RefactoringContext context, PrimitiveExpression pExpr, InvocationExpression invocation)
        {
            var newInvocation = (InvocationExpression)invocation.Clone();

            newInvocation.Arguments.First().ReplaceWith(CreateFormatString(context, pExpr, newInvocation.Arguments.Count() - 1));
            newInvocation.Arguments.Add(CreateFormatArgument(context));

            using (var script = context.StartScript()) {
                script.Replace(invocation, newInvocation);
            }
        }
Ejemplo n.º 12
0
        public void Run(RefactoringContext context)
        {
            var stmt = context.GetNode <Statement> ();
            var unresolvedArguments = GetUnresolvedArguments(context);

            if (unresolvedArguments.Count > 0)
            {
                using (var script = context.StartScript()) {
                    foreach (var id in unresolvedArguments)
                    {
                        script.InsertBefore(stmt, GenerateLocalVariableDeclaration(context, id));
                    }
                }
                return;
            }

            using (var script = context.StartScript()) {
                script.InsertBefore(stmt, GenerateLocalVariableDeclaration(context, CreateField.GetIdentifier(context)));
            }
        }
Ejemplo n.º 13
0
        // TODO: Invert if without else
        // ex. if (cond) DoSomething () == if (!cond) return; DoSomething ()
        // beware of loop contexts return should be continue then.
        public void Run(RefactoringContext context)
        {
            var ifStatement = GetIfElseStatement(context);

            using (var script = context.StartScript()) {
                script.Replace(ifStatement.Condition, CSharpUtil.InvertCondition(ifStatement.Condition));
                script.Replace(ifStatement.TrueStatement, ifStatement.FalseStatement);
                script.Replace(ifStatement.FalseStatement, ifStatement.TrueStatement);
                script.FormatText(ctx => GetIfElseStatement(ctx));
            }
        }
Ejemplo n.º 14
0
 public void Run(RefactoringContext context)
 {
     using (var script = context.StartScript()) {
         var varDecl = GetVariableDeclarationStatement(context);
         if (varDecl != null)
         {
             script.Replace(varDecl.Type, new SimpleType("var"));
         }
         else
         {
             script.Replace(GetForeachStatement(context).VariableType, new SimpleType("var"));
         }
     }
 }
Ejemplo n.º 15
0
        public void Run(RefactoringContext context)
        {
            var parameter = GetParameterDeclaration(context);

            var bodyStatement = parameter.Parent.GetChildByRole(AstNode.Roles.Body);

            var statement = new IfElseStatement()
            {
                Condition     = new BinaryOperatorExpression(new IdentifierExpression(parameter.Name), BinaryOperatorType.Equality, new NullReferenceExpression()),
                TrueStatement = new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "ArgumentNullException"), new PrimitiveExpression(parameter.Name)))
            };

            using (var script = context.StartScript()) {
                script.AddTo(bodyStatement, statement);
            }
        }
Ejemplo n.º 16
0
        public void Run(RefactoringContext context)
        {
            var switchStatement = GetSwitchStatement(context);

            var result    = context.Resolve(switchStatement.Expression);
            var type      = result.Type;
            var newSwitch = (SwitchStatement)switchStatement.Clone();

            var target = new TypeReferenceExpression(context.CreateShortType(result.Type));

            foreach (var field in type.GetFields())
            {
                if (field.IsSynthetic || !field.IsConst)
                {
                    continue;
                }
                newSwitch.SwitchSections.Add(new SwitchSection()
                {
                    CaseLabels =
                    {
                        new CaseLabel(new MemberReferenceExpression(target.Clone(), field.Name))
                    },
                    Statements =
                    {
                        new BreakStatement()
                    }
                });
            }

            newSwitch.SwitchSections.Add(new SwitchSection()
            {
                CaseLabels =
                {
                    new CaseLabel()
                },
                Statements =
                {
                    new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "ArgumentOutOfRangeException")))
                }
            });

            using (var script = context.StartScript()) {
                script.Replace(switchStatement, newSwitch);
            }
        }
Ejemplo n.º 17
0
        public void Run(RefactoringContext context)
        {
            var property = context.GetNode <PropertyDeclaration> ();
            var field    = GetBackingField(context);

            context.ReplaceReferences(field, property);

            // create new auto property
            var newProperty = (PropertyDeclaration)property.Clone();

            newProperty.Getter.Body = BlockStatement.Null;
            newProperty.Setter.Body = BlockStatement.Null;

            using (var script = context.StartScript()) {
                script.Remove(context.Unit.GetNodeAt <FieldDeclaration> (field.Region.BeginLine, field.Region.BeginColumn));
                script.Replace(property, newProperty);
            }
        }
        public void Run(RefactoringContext context)
        {
            AstType type;
            var     varDecl = GetVariableDeclarationStatement(context, out type);

            var assign = new AssignmentExpression(new IdentifierExpression(varDecl.Variables.First().Name), AssignmentOperatorType.Assign, varDecl.Variables.First().Initializer.Clone());

            var newVarDecl = (VariableDeclarationStatement)varDecl.Clone();

            if (newVarDecl.Type.IsMatch(new SimpleType("var")))
            {
                newVarDecl.Type = type;
            }

            newVarDecl.Variables.First().Initializer = Expression.Null;

            using (var script = context.StartScript()) {
                script.InsertBefore(varDecl, newVarDecl);
                script.Replace(varDecl, varDecl.Parent is ForStatement ? (AstNode)assign : new ExpressionStatement(assign));
            }
        }
Ejemplo n.º 19
0
        public void Run(RefactoringContext context)
        {
            var foreachStatement = GetForeachStatement(context);

            var result        = context.Resolve(foreachStatement.InExpression);
            var countProperty = GetCountProperty(result.Type);

            var initializer = new VariableDeclarationStatement(new PrimitiveType("int"), "i", new PrimitiveExpression(0));
            var id1         = new IdentifierExpression("i");
            var id2         = id1.Clone();
            var id3         = id1.Clone();

            var forStatement = new ForStatement()
            {
                Initializers      = { initializer },
                Condition         = new BinaryOperatorExpression(id1, BinaryOperatorType.LessThan, new MemberReferenceExpression(foreachStatement.InExpression.Clone(), countProperty)),
                Iterators         = { new ExpressionStatement(new UnaryOperatorExpression(UnaryOperatorType.PostIncrement, id2)) },
                EmbeddedStatement = new BlockStatement {
                    new VariableDeclarationStatement(foreachStatement.VariableType.Clone(), foreachStatement.VariableName, new IndexerExpression(foreachStatement.InExpression.Clone(), id3))
                }
            };

            if (foreachStatement.EmbeddedStatement is BlockStatement)
            {
                foreach (var child in ((BlockStatement)foreachStatement.EmbeddedStatement).Statements)
                {
                    forStatement.EmbeddedStatement.AddChild(child.Clone(), BlockStatement.StatementRole);
                }
            }
            else
            {
                forStatement.EmbeddedStatement.AddChild(foreachStatement.EmbeddedStatement.Clone(), BlockStatement.StatementRole);
            }

            using (var script = context.StartScript()) {
                script.Replace(foreachStatement, forStatement);
                script.Link(initializer.Variables.First().NameToken, id1, id2, id3);
            }
        }
Ejemplo n.º 20
0
        public void Run(RefactoringContext context)
        {
            var pexpr      = context.GetNode <PrimitiveExpression> ();
            var invocation = context.GetNode <InvocationExpression> ();

            if (invocation != null && invocation.Target.IsMatch(PrototypeFormatReference))
            {
                AddFormatCallToInvocation(context, pexpr, invocation);
                return;
            }

            var arg           = CreateFormatArgument(context);
            var newInvocation = new InvocationExpression(PrototypeFormatReference.Clone())
            {
                Arguments = { CreateFormatString(context, pexpr, 0), arg }
            };

            using (var script = context.StartScript()) {
                script.Replace(pexpr, newInvocation);
                script.Select(arg);
            }
        }
Ejemplo n.º 21
0
        public void Run(RefactoringContext context)
        {
            VariableInitializer initializer;
            var eventDeclaration = GetEventDeclaration(context, out initializer);
            var type             = context.Resolve(eventDeclaration.ReturnType).Type;

            if (type == null)
            {
                return;
            }
            var invokeMethod = type.GetDelegateInvokeMethod();

            if (invokeMethod == null)
            {
                return;
            }

            bool hasSenderParam           = false;
            IEnumerable <IParameter> pars = invokeMethod.Parameters;

            if (invokeMethod.Parameters.Any())
            {
                var first = invokeMethod.Parameters [0];
                if (first.Name == "sender" /*&& first.Type == "System.Object"*/)
                {
                    hasSenderParam = true;
                    pars           = invokeMethod.Parameters.Skip(1);
                }
            }
            const string handlerName = "handler";

            var arguments = new List <Expression> ();

            if (hasSenderParam)
            {
                arguments.Add(new ThisReferenceExpression());
            }
            foreach (var par in pars)
            {
                arguments.Add(new IdentifierExpression(par.Name));
            }

            var methodDeclaration = new MethodDeclaration()
            {
                Name       = "On" + initializer.Name,
                ReturnType = new PrimitiveType("void"),
                Modifiers  = ICSharpCode.NRefactory.CSharp.Modifiers.Protected | ICSharpCode.NRefactory.CSharp.Modifiers.Virtual,
                Body       = new BlockStatement()
                {
                    new VariableDeclarationStatement(context.CreateShortType(eventDeclaration.ReturnType), handlerName, new MemberReferenceExpression(new ThisReferenceExpression(), initializer.Name)),
                    new IfElseStatement()
                    {
                        Condition     = new BinaryOperatorExpression(new IdentifierExpression(handlerName), BinaryOperatorType.InEquality, new PrimitiveExpression(null)),
                        TrueStatement = new ExpressionStatement(new InvocationExpression(new IdentifierExpression(handlerName), arguments))
                    }
                }
            };

            foreach (var par in pars)
            {
                var typeName = context.CreateShortType(par.Type.Resolve(context.TypeResolveContext));
                var decl     = new ParameterDeclaration(typeName, par.Name);
                methodDeclaration.Parameters.Add(decl);
            }

            using (var script = context.StartScript()) {
                script.InsertWithCursor("Create event invocator", methodDeclaration, Script.InsertPosition.After);
            }
        }