コード例 #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());
            }
        }
コード例 #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);
            }
        }
コード例 #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"));
            }
        }
コード例 #4
0
        public void Run(RefactoringContext context)
        {
            var identifier = GetIdentifier(context);

            using (var script = context.StartScript()) {
                script.InsertWithCursor("Create property", GeneratePropertyDeclaration(context, identifier), Script.InsertPosition.Before);
            }
        }
コード例 #5
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("@") ? "\" + @\"" : "\" + \"");
            }
        }
コード例 #6
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);
            }
        }
コード例 #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);
            }
        }
コード例 #8
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));
            }
        }
コード例 #9
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);
            }
        }
コード例 #10
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, CppUtil.InvertCondition(ifStatement.Condition));
                script.Replace(ifStatement.TrueStatement, ifStatement.FalseStatement);
                script.Replace(ifStatement.FalseStatement, ifStatement.TrueStatement);
                script.FormatText(ctx => GetIfElseStatement(ctx));
            }
        }
コード例 #11
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)));
            }
        }
コード例 #12
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"));
         }
     }
 }
コード例 #13
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);
            }
        }
コード例 #14
0
 public void Run(RefactoringContext context)
 {
     using (var script = context.StartScript()) {
         var varDecl = GetVariableDeclarationStatement(context);
         if (varDecl != null)
         {
             var type = context.Resolve(varDecl.Variables.First().Initializer).Type;
             script.Replace(varDecl.Type, context.CreateShortType(type));
         }
         else
         {
             var foreachStatement = GetForeachStatement(context);
             var type             = context.Resolve(foreachStatement.VariableType).Type;
             script.Replace(foreachStatement.VariableType, context.CreateShortType(type));
         }
     }
 }
コード例 #15
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);
            }
        }
コード例 #16
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);
            }
        }
コード例 #17
0
        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));
            }
        }
コード例 #18
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);
            }
        }
コード例 #19
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);
            }
        }
コード例 #20
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.Cpp.Modifiers.Protected | ICSharpCode.NRefactory.Cpp.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);
                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);
            }
        }