Ejemplo n.º 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>());
        }
        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);
            }));
        }
Ejemplo n.º 3
0
        public override System.Collections.Generic.IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var expression = context.GetNode(i => i is BinaryOperatorExpression || i is UnaryOperatorExpression);

            if (expression == null)
            {
                yield break;
            }
            var node = context.GetNode();

            if (node == null || !(node is PrimitiveExpression) && node.StartLocation != context.Location)
            {
                yield break;
            }

            var rr = context.Resolve(expression);

            if (rr.ConstantValue == null)
            {
                yield break;
            }
            yield return(new CodeAction(
                             context.TranslateString("Compute constant value"),
                             script => script.Replace(expression, new PrimitiveExpression(rr.ConstantValue)),
                             node
                             ));
        }
Ejemplo n.º 4
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());
                }));
            }
        }
Ejemplo n.º 5
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            Expression node = context.GetNode <IdentifierExpression>();

            if (node == null)
            {
                var mr = context.GetNode <MemberReferenceExpression>();
                if (mr == null || !mr.MemberNameToken.IsInside(context.Location))
                {
                    yield break;
                }
                node = mr;
            }
            if (node == null)
            {
                yield break;
            }
            var rr = context.Resolve(node) as MethodGroupResolveResult;

            if (rr == null || rr.IsError)
            {
                yield break;
            }
            var type = TypeGuessing.GetValidTypes(context.Resolver, node).FirstOrDefault(t => t.Kind == TypeKind.Delegate);

            if (type == null)
            {
                yield break;
            }
            var invocationMethod = type.GetDelegateInvokeMethod();

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

            yield return(new CodeAction(
                             context.TranslateString("Convert to anonymous method"),
                             script => {
                var expr = new InvocationExpression(node.Clone(), invocationMethod.Parameters.Select(p => new IdentifierExpression(context.GetNameProposal(p.Name))));
                var stmt = invocationMethod.ReturnType.IsKnownType(KnownTypeCode.Void) ? (Statement)expr : new ReturnStatement(expr);
                var anonymousMethod = new AnonymousMethodExpression {
                    Body = new BlockStatement {
                        stmt
                    }
                };
                foreach (var p in invocationMethod.Parameters)
                {
                    var decl = new ParameterDeclaration(context.CreateShortType(p.Type), context.GetNameProposal(p.Name));
                    anonymousMethod.Parameters.Add(decl);
                }

                script.Replace(node, anonymousMethod);
            },
                             node
                             ));
        }
Ejemplo n.º 6
0
        public override 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
                {
                    if (!(lambda.Body is Expression))
                    {
                        yield break;
                    }
                    body = new BlockStatement();

                    var type = LambdaHelper.GetLambdaReturnType(context, lambda);
                    if (type == null || type.ReflectionName == "System.Void")
                    {
                        noReturn = true;
                        body.Add((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));
            }
        }
Ejemplo n.º 7
0
        public override 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 = TypeGuessing.GuessAstType(context, identifier);

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

            yield return(new CodeAction(context.TranslateString("Create local variable"), script => {
                var initializer = new VariableInitializer(null, 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);
                }
            }, identifier)
            {
                Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
            });
        }
Ejemplo n.º 8
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            Expression node = context.GetNode <IdentifierExpression>();

            if (node == null)
            {
                var mr = context.GetNode <MemberReferenceExpression>();
                if (mr == null || !mr.MemberNameToken.IsInside(context.Location))
                {
                    yield break;
                }
                node = mr;
            }
            if (node == null)
            {
                yield break;
            }
            var rr = context.Resolve(node) as MethodGroupResolveResult;

            if (rr == null || rr.IsError)
            {
                yield break;
            }
            var type = TypeGuessing.GetValidTypes(context.Resolver, node).FirstOrDefault(t => t.Kind == TypeKind.Delegate);

            if (type == null)
            {
                yield break;
            }
            var invocationMethod = type.GetDelegateInvokeMethod();

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

            yield return(new CodeAction(
                             context.TranslateString("Convert to lambda expression"),
                             script => {
                var invocation = new InvocationExpression(node.Clone(), invocationMethod.Parameters.Select(p => new IdentifierExpression(context.GetNameProposal(p.Name))));
                var lambda = new LambdaExpression {
                    Body = invocation
                };
                lambda.Parameters.AddRange(
                    invocation.Arguments
                    .Cast <IdentifierExpression>()
                    .Select(p => new ParameterDeclaration {
                    Name = p.Identifier
                })
                    );
                script.Replace(node, lambda);
            },
                             node
                             ));
        }
Ejemplo n.º 9
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var property = context.GetNode <PropertyDeclaration>();

            if (property == null || !property.NameToken.Contains(context.Location))
            {
                yield break;
            }

            var field = GetBackingField(context);

            if (field == null)
            {
                yield break;
            }
            // create new auto property
            var newProperty = (PropertyDeclaration)property.Clone();

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

            yield return(new CodeAction(context.TranslateString("Convert to auto property"), script => {
                script.Rename((IEntity)field, newProperty.Name);
                script.Remove(context.RootNode.GetNodeAt <FieldDeclaration> (field.Region.Begin));
                script.Replace(property, newProperty);
            }, property.NameToken));
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var type = context.GetNode <AstType>();

            if (type == null || type.Role != Roles.BaseType)
            {
                yield break;
            }
            var state = context.GetResolverStateBefore(type);

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

            var resolveResult = context.Resolve(type);

            if (resolveResult.Type.Kind != TypeKind.Class || resolveResult.Type.GetDefinition() == null || !resolveResult.Type.GetDefinition().IsAbstract)
            {
                yield break;
            }

            yield break;

            /*
             * yield return new CodeAction(context.TranslateString("Implement abstract members"), script => {
             *      script.InsertWithCursor(
             *              context.TranslateString("Implement abstract members"),
             *              state.CurrentTypeDefinition,
             *              ImplementInterfaceAction.GenerateImplementation (context, toImplement)
             *      );
             * });*/
        }
Ejemplo n.º 11
0
        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);
            }, catchClause));
        }
		public IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			var type = context.GetNode<AstType>();
			if (type == null || type.Role != Roles.BaseType)
				yield break;
			var state = context.GetResolverStateBefore(type);
			if (state.CurrentTypeDefinition == null)
				yield break;

			var resolveResult = context.Resolve(type);
			if (resolveResult.Type.Kind != TypeKind.Class || resolveResult.Type.GetDefinition() == null || !resolveResult.Type.GetDefinition().IsAbstract)
				yield break;

			var toImplement = CollectMembersToImplement(state.CurrentTypeDefinition, resolveResult.Type);
			if (toImplement.Count == 0)
				yield break;

			yield return new CodeAction(context.TranslateString("Implement abstract members"), script => {
				script.InsertWithCursor(
					context.TranslateString("Implement abstract members"),
					state.CurrentTypeDefinition,
					ImplementInterfaceAction.GenerateImplementation (context, toImplement.Select (m => Tuple.Create (m, false))).Select (entity => {
						var decl = entity as EntityDeclaration;
						if (decl != null)
							decl.Modifiers |= Modifiers.Override;
						return entity;
					})
				);
			}, type);
		}
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var entity = context.GetNode <ConstructorDeclaration>();

            if (entity == null)
            {
                yield break;
            }
            var type = entity.Parent as TypeDeclaration;

            if (type == null || entity.Name == type.Name)
            {
                yield break;
            }

            var typeDeclaration = entity.GetParent <TypeDeclaration>();

            yield return(new CodeAction(context.TranslateString("This is a constructor"), script => {
                script.Replace(entity.NameToken, Identifier.Create(typeDeclaration.Name, TextLocation.Empty));
            }, entity));

            yield return(new CodeAction(context.TranslateString("This is a void method"), script => {
                var generatedMethod = new MethodDeclaration();
                generatedMethod.Modifiers = entity.Modifiers;
                generatedMethod.ReturnType = new PrimitiveType("void");
                generatedMethod.Name = entity.Name;
                generatedMethod.Parameters.AddRange(entity.Parameters.Select(parameter => (ParameterDeclaration)parameter.Clone()));
                generatedMethod.Body = (BlockStatement)entity.Body.Clone();
                generatedMethod.Attributes.AddRange(entity.Attributes.Select(attribute => (AttributeSection)attribute.Clone()));

                script.Replace(entity, generatedMethod);
            }, entity));
        }
Ejemplo n.º 14
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);
            }
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var initializer = context.GetNode <VariableInitializer>();

            if (initializer == null || !initializer.NameToken.Contains(context.Location.Line, context.Location.Column))
            {
                yield break;
            }
            var type = initializer.Parent.Parent as TypeDeclaration;

            if (type == null)
            {
                yield break;
            }
            foreach (var member in type.Members)
            {
                if (member is PropertyDeclaration && ContainsGetter((PropertyDeclaration)member, initializer))
                {
                    yield break;
                }
            }
            var field = initializer.Parent as FieldDeclaration;

            if (field == null)
            {
                yield break;
            }
            yield return(new CodeAction(context.TranslateString("Create property"), script => {
                script.InsertWithCursor(context.TranslateString("Create property"), GeneratePropertyDeclaration(context, field, initializer), Script.InsertPosition.After);
            }));
        }
Ejemplo n.º 16
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var ifStatement = context.GetNode <IfElseStatement>();

            if (ifStatement == null)
            {
                yield break;
            }
            var bOp = ifStatement.GetNodeAt <BinaryOperatorExpression>(context.Location);

            if (bOp == null || !bOp.OperatorToken.Contains(context.Location))
            {
                yield break;
            }
            if (bOp.Ancestors.OfType <BinaryOperatorExpression>().Any(b => b.Operator != bOp.Operator))
            {
                yield break;
            }
            if (bOp.Operator == BinaryOperatorType.ConditionalAnd)
            {
                yield return(CreateAndSplit(context, ifStatement, bOp));
            }
            else if (bOp.Operator == BinaryOperatorType.ConditionalOr)
            {
                yield return(CreateOrSplit(context, ifStatement, bOp));
            }
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var createExpression = context.GetNode <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"), decl, resolveResult.Member.DeclaringTypeDefinition);
            }));
        }
Ejemplo n.º 18
0
        public override 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 static method call"), script => {
                    script.Replace(invocation, ToStaticMethodInvocation(invocation, memberReference, invocationRR));
                }, invocation));
            }
        }
Ejemplo n.º 19
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));
        }
Ejemplo n.º 20
0
        public override 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));
        }
Ejemplo n.º 21
0
        internal 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.CSharp.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.Equals(setterField))
            {
                return(null);
            }
            return(getterField);
        }
Ejemplo n.º 22
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var method = context.GetNode <MethodDeclaration>();

            if (method == null || !method.NameToken.Contains(context.Location))
            {
                yield break;
            }

            if (method.HasModifier(Modifiers.Static))
            {
                yield break;
            }
            var param = method.Parameters.FirstOrDefault();

            if (param == null || param.ParameterModifier != ParameterModifier.This)
            {
                yield break;
            }
            yield return(new CodeAction(
                             context.TranslateString("Convert method to static"),
                             script => script.ChangeModifier(method, method.Modifiers | Modifiers.Static),
                             method)
            {
                Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
            });
        }
Ejemplo n.º 23
0
        public override 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);
            }
        }
Ejemplo n.º 24
0
		public override 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) ? "\" + @\"" : "\" + \"");
			}, pexpr);
		}
Ejemplo n.º 25
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            string    keyword;
            Statement embeddedStatement;
//			BlockStatement block;

            var curNode = context.GetNode();

            if (!RemoveBracesAction.IsSpecialNode(curNode, out keyword, out embeddedStatement))
            {
                yield break;
            }
            if (embeddedStatement is BlockStatement)
            {
                yield break;
            }
            yield return(new CodeAction(
                             string.Format(context.TranslateString("Add braces to '{0}'"), keyword),
                             script => {
                script.Replace(embeddedStatement, new BlockStatement {
                    embeddedStatement.Clone()
                });
            },
                             curNode
                             ));
        }
Ejemplo n.º 26
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 - 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);
            }, pexpr));
        }
Ejemplo n.º 27
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.º 28
0
        public bool IsValid(RefactoringContext context)
        {
            var propertyDeclaration = context.GetNode <PropertyDeclaration> ();

            return(propertyDeclaration != null &&
                   !propertyDeclaration.Getter.IsNull && !propertyDeclaration.Setter.IsNull &&              // automatic properties always need getter & setter
                   propertyDeclaration.Getter.Body.IsNull &&
                   propertyDeclaration.Setter.Body.IsNull);
        }
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var property = context.GetNode <PropertyDeclaration>();

            if (property == null || !property.NameToken.Contains(context.Location))
            {
                yield break;
            }

            if (!(!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));
        }
Ejemplo n.º 30
0
        static PrimitiveExpression GetEmptyString(RefactoringContext context)
        {
            var node = context.GetNode <PrimitiveExpression> ();

            if (node == null || !(node.Value is string) || node.Value.ToString() != "")
            {
                return(null);
            }
            return(node);
        }