static PropertyDeclaration GeneratePropertyDeclaration(RefactoringContext context, FieldDeclaration field, string fieldName)
        {
            var mod = ICSharpCode.NRefactory.PlayScript.Modifiers.Public;

            if (field.HasModifier(ICSharpCode.NRefactory.PlayScript.Modifiers.Static))
            {
                mod |= ICSharpCode.NRefactory.PlayScript.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));
            }));
        }
Esempio n. 3
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));
        }