예제 #1
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var simpleType = context.GetNode <SimpleType>();

            if (simpleType != null && !(simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration))
            {
                return(GetActions(context, simpleType));
            }

            var createExpression = context.GetNode <ObjectCreateExpression>();

            if (createExpression != null)
            {
                return(GetActions(context, createExpression));
            }

            var identifier = context.GetNode <IdentifierExpression>();

            if (identifier != null && (identifier.Parent is MemberReferenceExpression))
            {
                return(GetActions(context, identifier));
            }

            return(Enumerable.Empty <CodeAction>());
        }
예제 #2
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var variableDeclaration = context.GetNode <VariableDeclarationStatement>();

            if (variableDeclaration == null)
            {
                yield break;
            }
            var entryNode = FindCurrentScopeEntryNode(variableDeclaration);

            if (entryNode == null)
            {
                yield break;
            }
            var selectedInitializer = context.GetNode <VariableInitializer>();

            if (selectedInitializer != null)
            {
                if (HasDependency(context, entryNode, selectedInitializer))
                {
                    yield return(MoveDeclarationAction(context, entryNode, variableDeclaration, selectedInitializer));
                }
                else
                {
                    yield return(MoveInitializerAction(context, entryNode, variableDeclaration, selectedInitializer));
                }
            }
            else
            {
                yield return(new CodeAction(context.TranslateString("Move declaration to outer scope"), script => {
                    script.Remove(variableDeclaration);
                    script.InsertBefore(entryNode, variableDeclaration.Clone());
                }, variableDeclaration));
            }
        }
        CodeAction ActionFromVariableInitializer(RefactoringContext context)
        {
            var initializer = context.GetNode <VariableInitializer>();

            if (initializer == null || initializer.Parent.Parent is ForStatement)
            {
                return(null);
            }
            var initializerRR = context.Resolve(initializer) as LocalResolveResult;

            if (initializerRR == null)
            {
                return(null);
            }
            var elementType = GetElementType(initializerRR, context);

            if (elementType == null)
            {
                return(null);
            }

            return(new CodeAction(context.TranslateString("Iterate via foreach"), script => {
                var iterator = MakeForeach(new IdentifierExpression(initializer.Name), elementType, context);
                script.InsertAfter(context.GetNode <Statement>(), iterator);
            }, initializer));
        }
        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);
                }
            }));
        }
예제 #5
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            // lambda
            var lambda = context.GetNode <LambdaExpression> ();

            if (lambda != null && lambda.ArrowToken.Contains(context.Location))
            {
                if (ContainsLocalReferences(context, lambda, lambda.Body))
                {
                    yield break;
                }

                bool           noReturn = false;
                BlockStatement body;
                if (lambda.Body is BlockStatement)
                {
                    body = (BlockStatement)lambda.Body.Clone();
                }
                else
                {
                    body = new BlockStatement();

                    var type = LambdaHelper.GetLambdaReturnType(context, lambda);
                    if (type == null || type.ReflectionName == "System.Void")
                    {
                        noReturn = true;
                        body.Add(new ExpressionStatement((Expression)lambda.Body.Clone()));
                    }
                    else
                    {
                        body.Add(new ReturnStatement((Expression)lambda.Body.Clone()));
                    }
                }
                var method = GetMethod(context, (LambdaResolveResult)context.Resolve(lambda), body, noReturn);
                yield return(GetAction(context, lambda, method));
            }

            // anonymous method
            var anonymousMethod = context.GetNode <AnonymousMethodExpression> ();

            if (anonymousMethod != null && anonymousMethod.DelegateToken.Contains(context.Location))
            {
                if (ContainsLocalReferences(context, anonymousMethod, anonymousMethod.Body))
                {
                    yield break;
                }

                var method = GetMethod(context, (LambdaResolveResult)context.Resolve(anonymousMethod),
                                       (BlockStatement)anonymousMethod.Body.Clone());
                yield return(GetAction(context, anonymousMethod, method));
            }
        }
예제 #6
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            AstNode node = context.GetNode();

            if (node is Identifier)
            {
                node = node.Parent;
            }
            if (node is SimpleType || node is IdentifierExpression)
            {
                return(GetActionsForType(context, node)
                       .Concat(GetActionsForAddNamespaceUsing(context, node)));
            }
            else if (node is MemberReferenceExpression && node.Parent is InvocationExpression)
            {
                return(GetActionsForExtensionMethodInvocation(context, (InvocationExpression)node.Parent));
            }
            else if (node is MemberReferenceExpression)
            {
                return(GetActionsForAddNamespaceUsing(context, node));
            }
            else
            {
                return(EmptyList <CodeAction> .Instance);
            }
        }
        static IField GetBackingField(RefactoringContext context)
        {
            var propertyDeclaration = context.GetNode <PropertyDeclaration> ();

            // automatic properties always need getter & setter
            if (propertyDeclaration == null || propertyDeclaration.Getter.IsNull || propertyDeclaration.Setter.IsNull || propertyDeclaration.Getter.Body.IsNull || propertyDeclaration.Setter.Body.IsNull)
            {
                return(null);
            }
            if (!context.Supports(csharp3) || propertyDeclaration.HasModifier(ICSharpCode.NRefactory.PlayScript.Modifiers.Abstract) || ((TypeDeclaration)propertyDeclaration.Parent).ClassType == ClassType.Interface)
            {
                return(null);
            }
            var getterField = ScanGetter(context, propertyDeclaration);

            if (getterField == null)
            {
                return(null);
            }
            var setterField = ScanSetter(context, propertyDeclaration);

            if (setterField == null)
            {
                return(null);
            }
            if (getterField.Region != setterField.Region)
            {
                return(null);
            }
            return(getterField);
        }
예제 #8
0
        CodeAction HandleExpressionStatement(RefactoringContext context, ExpressionStatement expressionStatement)
        {
            var expression = expressionStatement.Expression as AssignmentExpression;

            if (expression == null || expression.Operator != AssignmentOperatorType.Assign)
            {
                return(null);
            }
            if (!(expression.Right is ObjectCreateExpression))
            {
                return(null);
            }
            var expressionResolveResult = context.Resolve(expression.Left);

            if (!(expressionResolveResult is LocalResolveResult) && !(expressionResolveResult is MemberResolveResult))
            {
                return(null);
            }
            IList <AstNode> statements    = GetNodes(context.GetNode <Statement>());
            var             converter     = new StatementsToInitializerConverter(context);
            var             newExpression = converter.ConvertToInitializer(expression, ref statements);

            if (newExpression == null || statements.Count == 0)
            {
                return(null);
            }
            return(MakeAction(context, expression, newExpression, statements));
        }
예제 #9
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            if (context.IsSomethingSelected)
            {
                yield break;
            }
            var pexpr = context.GetNode <PrimitiveExpression>();

            if (pexpr == null || !(pexpr.Value is string))
            {
                yield break;
            }
            if (pexpr.LiteralValue.StartsWith("@", StringComparison.Ordinal))
            {
                if (!(pexpr.StartLocation < new TextLocation(context.Location.Line, context.Location.Column - 2) &&
                      new TextLocation(context.Location.Line, context.Location.Column + 2) < pexpr.EndLocation))
                {
                    yield break;
                }
            }
            else
            {
                if (!(pexpr.StartLocation < new TextLocation(context.Location.Line, context.Location.Column - 1) && new TextLocation(context.Location.Line, context.Location.Column + 1) < pexpr.EndLocation))
                {
                    yield break;
                }
            }

            yield return(new CodeAction(context.TranslateString("Split string literal"), script => {
                int offset = context.GetOffset(context.Location);
                script.InsertText(offset, pexpr.LiteralValue.StartsWith("@", StringComparison.Ordinal) ? "\" + @\"" : "\" + \"");
            }));
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var invocation = context.GetNode <InvocationExpression>();

            if (invocation == null)
            {
                yield break;
            }
            var memberReference = invocation.Target as MemberReferenceExpression;

            if (memberReference == null)
            {
                yield break;
            }
            var invocationRR = context.Resolve(invocation) as CSharpInvocationResolveResult;

            if (invocationRR == null)
            {
                yield break;
            }
            if (invocationRR.IsExtensionMethodInvocation)
            {
                yield return(new CodeAction(context.TranslateString("Convert to call to static method"), script => {
                    script.Replace(invocation, ToStaticMethodInvocation(invocation, memberReference, invocationRR));
                }, invocation));
            }
        }
예제 #11
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var createExpression = context.GetNode <Expression>() as ObjectCreateExpression;

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

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

            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 => {
                var decl = new ConstructorDeclaration()
                {
                    Name = resolveResult.Member.DeclaringTypeDefinition.Name,
                    Modifiers = Modifiers.Public,
                    Body = new BlockStatement()
                    {
                        new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
                    }
                };
                decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));

                script.InsertWithCursor(
                    context.TranslateString("Create constructor"),
                    resolveResult.Member.DeclaringTypeDefinition,
                    decl
                    );
            }, createExpression));
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            if (context.IsSomethingSelected)
            {
                yield break;
            }
            var node = context.GetNode <VariableDeclarationStatement>();

            if (node == null || node.Variables.Count != 1)
            {
                yield break;
            }
            var initializer = node.Variables.First();

            if (!initializer.NameToken.Contains(context.Location) || initializer.Initializer.IsNull)
            {
                yield break;
            }
            var resolveResult = context.Resolve(initializer) as LocalResolveResult;

            if (resolveResult == null || resolveResult.IsError)
            {
                yield break;
            }
            var unit = context.RootNode as SyntaxTree;

            if (unit == null)
            {
                yield break;
            }
            yield return(new CodeAction(context.TranslateString("Inline local variable"), script => {
                refFinder.FindLocalReferences(resolveResult.Variable, context.UnresolvedFile, unit, context.Compilation, (n, r) => script.Replace(n, AddParensIfRequired(n, initializer.Initializer.Clone())), default(CancellationToken));
                script.Remove(node);
            }, initializer));
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var catchClause = context.GetNode <CatchClause>();

            if (catchClause == null)
            {
                yield break;
            }
            if (!catchClause.Type.IsNull)
            {
                yield break;
            }
            yield return(new CodeAction(context.TranslateString("Add type specifier"), script => {
                var newType = context.CreateShortType("System", "Exception");
                var namingHelper = new NamingHelper(context);
                var newIdentifier = Identifier.Create(namingHelper.GenerateVariableName(newType, "e"));

                script.Replace(catchClause, new CatchClause {
                    Type = newType,
                    VariableNameToken = newIdentifier,
                    Body = catchClause.Body.Clone() as BlockStatement
                });
                script.Select(newType);
            }));
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            if (!context.IsSomethingSelected)
            {
                yield break;
            }
            var pexpr = context.GetNode <PrimitiveExpression>();

            if (pexpr == null || !(pexpr.Value is string))
            {
                yield break;
            }
            if (pexpr.LiteralValue.StartsWith("@", StringComparison.Ordinal))
            {
                if (!(pexpr.StartLocation < new TextLocation(context.Location.Line, context.Location.Column - 1) && new TextLocation(context.Location.Line, context.Location.Column + 1) < pexpr.EndLocation))
                {
                    yield break;
                }
            }
            else
            {
                if (!(pexpr.StartLocation < context.Location && context.Location < pexpr.EndLocation))
                {
                    yield break;
                }
            }

            yield return(new CodeAction(context.TranslateString("Introduce format item"), script => {
                var invocation = context.GetNode <InvocationExpression>();
                if (invocation != null && invocation.Target.IsMatch(PrototypeFormatReference))
                {
                    AddFormatCallToInvocation(context, script, pexpr, invocation);
                    return;
                }

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

                script.Replace(pexpr, newInvocation);
                script.Select(arg);
            }));
        }
        static PropertyDeclaration GetPropertyDeclaration(RefactoringContext context)
        {
            var node = context.GetNode();

            if (node == null)
            {
                return(null);
            }
            return(node.Parent as PropertyDeclaration);
        }
예제 #16
0
        static PreProcessorDirective GetDirective(RefactoringContext context)
        {
            var directive = context.GetNode <PreProcessorDirective> ();

            if (directive == null || directive.Type != PreProcessorDirectiveType.Region)
            {
                return(null);
            }
            return(directive);
        }
예제 #17
0
        static ForeachStatement GetForeachStatement(RefactoringContext context)
        {
            var result = context.GetNode <ForeachStatement> ();

            if (result != null && result.VariableType.Contains(context.Location) && result.VariableType.IsMatch(varType))
            {
                return(result);
            }
            return(null);
        }
        static IfElseStatement GetIfElseStatement(RefactoringContext context)
        {
            var result = context.GetNode <IfElseStatement> ();

            if (result != null && result.IfToken.Contains(context.Location))
            {
                return(result);
            }
            return(null);
        }
        static PrimitiveExpression GetEmptyString(RefactoringContext context)
        {
            var node = context.GetNode <PrimitiveExpression> ();

            if (node == null || !(node.Value is string) || node.Value.ToString() != "")
            {
                return(null);
            }
            return(node);
        }
        static VariableDeclarationStatement GetVariableDeclarationStatement(RefactoringContext context)
        {
            var result = context.GetNode <VariableDeclarationStatement> ();

            if (result != null && result.Variables.Count == 1 && !result.Variables.First().Initializer.IsNull&& result.Type.Contains(context.Location) && !result.Type.IsMatch(varType))
            {
                return(result);
            }
            return(null);
        }
예제 #21
0
        static SwitchStatement GetSwitchStatement(RefactoringContext context)
        {
            var switchStatment = context.GetNode <SwitchStatement> ();

            if (switchStatment != null && switchStatment.SwitchSections.Count == 0)
            {
                return(switchStatment);
            }
            return(null);
        }
예제 #22
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var simpleType = context.GetNode <SimpleType>();

            if (simpleType != null && (simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration))
            {
                return(GetActions(context, simpleType));
            }

            return(Enumerable.Empty <CodeAction>());
        }
예제 #23
0
 public NamingHelper(RefactoringContext context)
 {
     this.context = context;
     if (usedVariableNames == null)
     {
         var visitor = new VariableFinderVisitor();
         var astNode = context.GetNode <Statement>();
         astNode.AcceptVisitor(visitor);
         usedVariableNames = visitor.VariableNames;
     }
 }
        static EventDeclaration GetEventDeclaration(RefactoringContext context, out VariableInitializer initializer)
        {
            var result = context.GetNode <EventDeclaration> ();

            if (result == null)
            {
                initializer = null;
                return(null);
            }
            initializer = result.Variables.FirstOrDefault(v => v.NameToken.Contains(context.Location));
            return(initializer != null ? result : null);
        }
예제 #25
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var property = context.GetNode <PropertyDeclaration>();

            if (!(property != null &&
                  !property.Getter.IsNull && !property.Setter.IsNull &&               // automatic properties always need getter & setter
                  property.Getter.Body.IsNull &&
                  property.Setter.Body.IsNull))
            {
                yield break;
            }

            yield return(new CodeAction(context.TranslateString("Create backing store"), script => {
                string backingStoreName = context.GetNameProposal(property.Name);

                // create field
                var backingStore = new FieldDeclaration();
                if (property.Modifiers.HasFlag(Modifiers.Static))
                {
                    backingStore.Modifiers |= Modifiers.Static;
                }
                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();
                Expression id1;
                if (backingStoreName == "value")
                {
                    id1 = new ThisReferenceExpression().Member("value");
                }
                else
                {
                    id1 = new IdentifierExpression(backingStoreName);
                }
                Expression id2 = id1.Clone();
                newProperty.Getter.Body = new BlockStatement()
                {
                    new ReturnStatement(id1)
                };
                newProperty.Setter.Body = new BlockStatement()
                {
                    new AssignmentExpression(id2, AssignmentOperatorType.Assign, new IdentifierExpression("value"))
                };

                script.Replace(property, newProperty);
                script.InsertBefore(property, backingStore);
                script.Link(initializer, id1, id2);
            }, property.NameToken));
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            // field, local var, event, fixed var, fixed field

            // local variable
            var variableDecl = context.GetNode <VariableDeclarationStatement> ();

            if (variableDecl != null && variableDecl.Parent is BlockStatement)
            {
                return(GetAction(context, variableDecl, v => v.Variables));
            }

            // field
            var fieldDecl = context.GetNode <FieldDeclaration> ();

            if (fieldDecl != null)
            {
                return(GetAction(context, fieldDecl, f => f.Variables));
            }

            // event
            var eventDecl = context.GetNode <EventDeclaration> ();

            if (eventDecl != null)
            {
                return(GetAction(context, eventDecl, e => e.Variables));
            }

            // fixed field
            var fixedFieldDecl = context.GetNode <FixedFieldDeclaration> ();

            if (fixedFieldDecl != null)
            {
                return(GetAction(context, fixedFieldDecl, f => f.Variables));
            }

            return(Enumerable.Empty <CodeAction> ());
        }
예제 #27
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var initializer = context.GetNode <VariableInitializer>();

            if (initializer != null)
            {
                var action = HandleInitializer(context, initializer);
                if (action != null)
                {
                    yield return(action);
                }
            }
            var expressionStatement = context.GetNode <ExpressionStatement>();

            if (expressionStatement != null)
            {
                var action = HandleExpressionStatement(context, expressionStatement);
                if (action != null)
                {
                    yield return(action);
                }
            }
        }
예제 #28
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var identifier = context.GetNode <IdentifierExpression>();

            if (identifier != null && !(identifier.Parent is InvocationExpression && ((InvocationExpression)identifier.Parent).Target == identifier))
            {
                return(GetActionsFromIdentifier(context, identifier));
            }

            var memberReference = context.GetNode <MemberReferenceExpression>();

            if (memberReference != null && !(memberReference.Parent is InvocationExpression && ((InvocationExpression)memberReference.Parent).Target == memberReference))
            {
                return(GetActionsFromMemberReferenceExpression(context, memberReference));
            }

            var invocation = context.GetNode <InvocationExpression>();

            if (invocation != null)
            {
                return(GetActionsFromInvocation(context, invocation));
            }
            return(Enumerable.Empty <CodeAction>());
        }
        public System.Collections.Generic.IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var node = context.GetNode <T>();

            if (node == null)
            {
                yield break;
            }
            var action = GetAction(context, node);

            if (action == null)
            {
                yield break;
            }
            yield return(action);
        }
예제 #30
0
        public static BinaryOperatorExpression GetBinaryOperatorExpression(RefactoringContext context)
        {
            var node = context.GetNode <BinaryOperatorExpression> ();

            if (node == null || !node.OperatorToken.Contains(context.Location))
            {
                return(null);
            }
            var result = node as BinaryOperatorExpression;

            if (result == null || (result.Operator != BinaryOperatorType.Equality && result.Operator != BinaryOperatorType.InEquality))
            {
                return(null);
            }
            return(result);
        }