Example #1
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var property = context.GetNode <PropertyDeclaration>();
            var field    = RemoveBackingStoreAction.GetBackingField(context);

            if (field == null)
            {
                yield break;
            }
            var resolvedType = ReflectionHelper.ParseReflectionName("System.EventHandler").Resolve(context.Compilation);

            if (resolvedType == null)
            {
                yield break;
            }
            var type = (TypeDeclaration)property.Parent;

            yield return(new CodeAction(context.TranslateString("Create changed event"), script => {
                var eventDeclaration = CreateChangedEventDeclaration(context, property);
                var methodDeclaration = CreateEventInvocatorAction.CreateEventInvocator(context, type, eventDeclaration, eventDeclaration.Variables.First(), resolvedType.GetDelegateInvokeMethod(), false);
                var stmt = new ExpressionStatement(new InvocationExpression(
                                                       new IdentifierExpression(methodDeclaration.Name),
                                                       new MemberReferenceExpression(new TypeReferenceExpression(context.CreateShortType("System", "EventArgs")), "Empty")
                                                       ));
                var task = script.InsertWithCursor(
                    context.TranslateString("Create event invocator"),
                    Script.InsertPosition.After,
                    new AstNode[] { eventDeclaration, methodDeclaration }
                    );

                Action <Task> insertInvocation = delegate {
                    script.InsertBefore(property.Setter.Body.RBraceToken, stmt);
                    script.FormatText(stmt);
                };

                if (task.IsCompleted)
                {
                    insertInvocation(null);
                }
                else
                {
                    task.ContinueWith(insertInvocation, TaskScheduler.FromCurrentSynchronizationContext());
                }
            }, property.NameToken));
        }
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var property = context.GetNode <PropertyDeclaration>();

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

            var field = RemoveBackingStoreAction.GetBackingField(context, property);

            if (field == null)
            {
                yield break;
            }
            var resolvedType = ReflectionHelper.ParseReflectionName("System.EventHandler").Resolve(context.Compilation);

            if (resolvedType == null)
            {
                yield break;
            }
            var type = (TypeDeclaration)property.Parent;

            yield return(new CodeAction(context.TranslateString("Create changed event"), script => {
                var eventDeclaration = CreateChangedEventDeclaration(context, property);
                var methodDeclaration = CreateEventInvocatorAction.CreateEventInvocator(context, type, eventDeclaration, eventDeclaration.Variables.First(), resolvedType.GetDelegateInvokeMethod(), false);
                var stmt = new ExpressionStatement(new InvocationExpression(
                                                       new IdentifierExpression(methodDeclaration.Name),
                                                       context.CreateShortType("System", "EventArgs").Member("Empty")
                                                       ));
                script.InsertWithCursor(
                    context.TranslateString("Create event invocator"),
                    Script.InsertPosition.After,
                    new AstNode[] { eventDeclaration, methodDeclaration }
                    ).ContinueScript(delegate {
                    script.InsertBefore(property.Setter.Body.RBraceToken, stmt);
                    script.FormatText(stmt);
                });
            }, property.NameToken));
        }