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); } }
static PropertyDeclaration GeneratePropertyDeclaration(RefactoringContext context, FieldDeclaration field, string fieldName) { var mod = ICSharpCode.NRefactory.CSharp.Modifiers.Public; if (field.HasModifier(ICSharpCode.NRefactory.CSharp.Modifiers.Static)) { mod |= ICSharpCode.NRefactory.CSharp.Modifiers.Static; } return(new PropertyDeclaration() { Modifiers = mod, Name = context.GetNameProposal(fieldName, false), ReturnType = field.ReturnType.Clone(), Getter = new Accessor() { Body = new BlockStatement() { new ReturnStatement(new IdentifierExpression(fieldName)) } }, Setter = new Accessor() { Body = new BlockStatement() { new ExpressionStatement(new AssignmentExpression(new IdentifierExpression(fieldName), new IdentifierExpression("value"))) } } }); }
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 || field.HasModifier(Modifiers.Readonly) || field.HasModifier(Modifiers.Const)) { yield break; } var resolveResult = context.Resolve(initializer) as MemberResolveResult; if (resolveResult == null) { yield break; } yield return(new CodeAction(context.TranslateString("Create property"), script => { var fieldName = context.GetNameProposal(initializer.Name, true); if (initializer.Name == context.GetNameProposal(initializer.Name, false)) { script.Rename(resolveResult.Member, fieldName); } script.InsertWithCursor( context.TranslateString("Create property"), Script.InsertPosition.After, GeneratePropertyDeclaration(context, field, fieldName)); })); }
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)); }
protected override CodeAction GetAction(RefactoringContext ctx, IfElseStatement ifElseStatement) { var isExpr = ctx.GetNode <IsExpression>(); if (isExpr == null || !isExpr.IsToken.Contains(ctx.Location)) { return(null); } int foundCasts; var action = ScanIfElse(ctx, ifElseStatement, out isExpr, out foundCasts); if (action != null) { return(action); } isExpr = ctx.GetNode <IsExpression>(); var node = isExpr.Parent; while (node is ParenthesizedExpression) { node = node.Parent; } var uOp = node as UnaryOperatorExpression; if (uOp != null && uOp.Operator == UnaryOperatorType.Not) { var rr = ctx.Resolve(isExpr.Type); if (rr == null || rr.IsError || rr.Type.IsReferenceType == false) { return(null); } return(new CodeAction(ctx.TranslateString("Use 'as' and check for null"), script => { var varName = ctx.GetNameProposal(CreateMethodDeclarationAction.GuessNameFromType(rr.Type), ifElseStatement.StartLocation); var varDec = new VariableDeclarationStatement(new PrimitiveType("var"), varName, new AsExpression(isExpr.Expression.Clone(), isExpr.Type.Clone())); var binaryOperatorIdentifier = new IdentifierExpression(varName); var binaryOperatorExpression = new BinaryOperatorExpression(binaryOperatorIdentifier, BinaryOperatorType.Equality, new NullReferenceExpression()); if (IsEmbeddedStatement(ifElseStatement)) { var block = new BlockStatement(); block.Add(varDec); var newIf = (IfElseStatement)ifElseStatement.Clone(); newIf.Condition = binaryOperatorExpression; block.Add(newIf); script.Replace(ifElseStatement, block); } else { script.InsertBefore(ifElseStatement, varDec); script.Replace(uOp, binaryOperatorExpression); } }, isExpr.IsToken)); } var obj = isExpr.Expression; var castToType = isExpr.Type; var cast = new Choice { PatternHelper.OptionalParentheses(PatternHelper.OptionalParentheses(obj.Clone()).CastTo(castToType.Clone())), PatternHelper.OptionalParentheses(PatternHelper.OptionalParentheses(obj.Clone()).CastAs(castToType.Clone())) }; var rr2 = ctx.Resolve(castToType); if (rr2 == null || rr2.IsError || rr2.Type.IsReferenceType == false) { return(null); } var foundCasts2 = isExpr.GetParent <Statement>().DescendantNodesAndSelf(n => !cast.IsMatch(n)).Where(n => isExpr.StartLocation < n.StartLocation && cast.IsMatch(n)).ToList(); return(new CodeAction(ctx.TranslateString("Use 'as' and check for null"), script => { var varName = ctx.GetNameProposal(CreateMethodDeclarationAction.GuessNameFromType(rr2.Type), ifElseStatement.StartLocation); var varDec = new VariableDeclarationStatement(new PrimitiveType("var"), varName, new AsExpression(obj.Clone(), castToType.Clone())); var binaryOperatorIdentifier = new IdentifierExpression(varName); var binaryOperatorExpression = new BinaryOperatorExpression(binaryOperatorIdentifier, BinaryOperatorType.InEquality, new NullReferenceExpression()); var linkedNodes = new List <AstNode>(); linkedNodes.Add(varDec.Variables.First().NameToken); linkedNodes.Add(binaryOperatorIdentifier); if (IsEmbeddedStatement(ifElseStatement)) { var block = new BlockStatement(); block.Add(varDec); var newIf = (IfElseStatement)ifElseStatement.Clone(); newIf.Condition = binaryOperatorExpression; foreach (var node2 in newIf.DescendantNodesAndSelf(n => !cast.IsMatch(n)).Where(n => cast.IsMatch(n))) { var id = new IdentifierExpression(varName); linkedNodes.Add(id); node2.ReplaceWith(id); } block.Add(newIf); script.Replace(ifElseStatement, block); } else { script.InsertBefore(ifElseStatement, varDec); script.Replace(isExpr, binaryOperatorExpression); foreach (var c in foundCasts2) { var id = new IdentifierExpression(varName); linkedNodes.Add(id); script.Replace(c, id); } } script.Link(linkedNodes); }, isExpr.IsToken)); }
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 )); }
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 )); }