private void ChangeToFieldCallRefactoringProvider_Apply(object sender, ApplyContentEventArgs ea)
        {
            var    property       = ea.Element.GetProperty();
            var    toReplace      = GetCodeElementToReplace(ea.Element, property);
            var    elementBuilder = ea.NewElementBuilder();
            string generatedCode  = GetChangeToFieldCallCode(elementBuilder, toReplace, property);

            ea.TextDocument.Replace(toReplace.Range, generatedCode, "Change to field call", true);
        }
        }     // refactoringProvider_CheckAvailability(sender, ea)

        /// <summary>
        /// Refactoring provider apply
        /// </summary>
        private void refactoringProvider_Apply(object sender, ApplyContentEventArgs ea)
        {
            if (_Property != null && ea.SelectedSubMenuItem != null)
            {
                ElementBuilder elementBuilder = ea.NewElementBuilder();
                string         newCode;
                switch (ea.SelectedSubMenuItem.Name)
                {
                case MenuItem_ConvertTo_XPOProperty:
                    newCode = GetNewPropertyDeclaration(elementBuilder, _Property);
                    ea.TextDocument.Replace(_Property.Range, newCode, "Converting into XPO property", true);
                    break;

                case MenuItem_ConvertTo_DelayedProperty:
                    newCode = GetNewDelayedPropertyDeclaration(elementBuilder, _Property);
                    ea.TextDocument.Replace(_Property.Range, newCode, "Converting into delayed property", true);
                    // How detect and remove property variable?
                    //if (_IsXpoProperty && _Property.HasGetter)
                    //    ea.TextDocument.Replace(_Property.Getter.LastChild.FirstDetail.Range, string.Empty, "Converting into delayed property - field var removed", true);
                    break;
                } // switch
            }     // if
        }         // refactoringProvider_Apply(sender, ea)
        } // refactoringProvider_CheckAvailability(sender, ea)

        /// <summary>
        /// Refactoring provider apply
        /// </summary>
        private void refactoringProvider_Apply(object sender, ApplyContentEventArgs ea)
        {
            if (_Property != null && ea.SelectedSubMenuItem != null)
            {
                ElementBuilder elementBuilder = ea.NewElementBuilder();
                string newCode;
                switch (ea.SelectedSubMenuItem.Name)
                {
                    case MenuItem_ConvertTo_XPOProperty:
                        newCode = GetNewPropertyDeclaration(elementBuilder, _Property);
                        ea.TextDocument.Replace(_Property.Range, newCode, "Converting into XPO property", true);
                        break;
                    case MenuItem_ConvertTo_DelayedProperty:
                        newCode = GetNewDelayedPropertyDeclaration(elementBuilder, _Property);
                        ea.TextDocument.Replace(_Property.Range, newCode, "Converting into delayed property", true);
                        // How detect and remove property variable?
                        //if (_IsXpoProperty && _Property.HasGetter)
                        //    ea.TextDocument.Replace(_Property.Getter.LastChild.FirstDetail.Range, string.Empty, "Converting into delayed property - field var removed", true);
                        break;
                } // switch
            } // if
        } // refactoringProvider_Apply(sender, ea)
        private void DeclareExtensionMethod_Apply(Object sender, ApplyContentEventArgs ea)
        {
            using (ea.TextDocument.NewCompoundAction("Declare Extension Method"))
            {
                // Get reference to Method
                MethodReferenceExpression MethodReferenceExpression = ea.Element as MethodReferenceExpression;
                MethodCallExpression MethodCallExpression = MethodReferenceExpression.Parent as MethodCallExpression;
                MethodCall MethodCall = MethodReferenceExpression.Parent as MethodCall;
                LanguageElement TheElement = MethodCallExpression == null ? (LanguageElement)MethodCall : (LanguageElement)MethodCallExpression;
                IWithArguments WithArgs = TheElement as IWithArguments;

                var Builder = ea.NewElementBuilder();

                // NEW CLASS
                ElementReferenceExpression TheObject = MethodReferenceExpression.Nodes[0] as ElementReferenceExpression;
                Variable variable = TheObject.GetDeclaration() as Variable;
                string TheObjectClassName = (variable.DetailNodes[0] as TypeReferenceExpression).Name;
                string ClassName = TheObjectClassName + "Ext";

                // NEW METHOD
                string ReturnTypeName;
                if (MethodCallExpression != null)
                {
                    ReturnTypeName = GetMethodReturnType((MethodCallExpression)MethodReferenceExpression.Parent);
                }
                else
                {
                    ReturnTypeName = "";
                }
                var NewMethod = Builder.BuildMethod(ReturnTypeName, MethodReferenceExpression.Name);
                NewMethod.IsStatic = true;
                NewMethod.Visibility = MemberVisibility.Public;

                // Add Source Param
                NewMethod.Parameters.Add(new ExtensionMethodParam(TheObjectClassName, "Source"));

                // Add Original Params
                int index = 0;
                foreach (Expression expression in WithArgs.Args)
                {
                    index += 1;
                    NewMethod.Parameters.Add(expression.ToParameter("Param" + index));
                }

                LanguageElement ParentNamespace = TheElement.GetParent(LanguageElementType.Namespace);
                Class ExtensionClass = getClassInNamespace(ClassName, ParentNamespace);
                LanguageElement Parent;
                LanguageElement Child;

                if (ExtensionClass == null)
                {
                    ExtensionClass = new Class(ClassName);
                    ExtensionClass.IsStatic = true;
                    ExtensionClass.Visibility = MemberVisibility.Public;
                    ParentNamespace.AddNode(ExtensionClass);

                    Child = ExtensionClass;
                    Parent = ParentNamespace;
                }
                else
                {
                    Child = NewMethod;
                    Parent = ExtensionClass;
                }
                ExtensionClass.AddNode(NewMethod);
                SourceRange FormatRange = InsertChildAtEndOfParent(Child, (DelimiterCapableBlock)Parent, ea.TextDocument);
                ea.TextDocument.Format(FormatRange);
            }
        }
Пример #5
0
        private void ExpandEvent_Execute(Object sender, ApplyContentEventArgs ea)
        {
            ElementBuilder Builder = ea.NewElementBuilder();
            Variable NewEventField = Builder.BuildVariable("EventHandler", "_" + _EventToExpand.Name);
            NewEventField.Visibility = _EventToExpand.Visibility;

            // Build Event and Add/Remove
            Event NewEvent = Builder.BuildEvent(_EventToExpand.Name, _EventToExpand.MemberTypeReference);
            NewEvent.Visibility = _EventToExpand.Visibility;
            EventAdd NewEventAdd = Builder.AddEventAdd(NewEvent);
            Builder.AddAssignment(NewEventAdd, NewEventField.Name, "value", AssignmentOperatorType.PlusEquals);
            EventRemove NewEventRemove = Builder.AddEventRemove(NewEvent);
            Builder.AddAssignment(NewEventRemove, NewEventField.Name, "value", AssignmentOperatorType.MinusEquals);

            TextDocument ActiveDoc = CodeRush.Documents.ActiveTextDocument;
            ActiveDoc.QueueReplace(_EventToExpand, CodeRush.CodeMod.GenerateCode(NewEvent));
            ActiveDoc.QueueInsert(_EventToExpand.Range.Start, CodeRush.CodeMod.GenerateCode(NewEventField));
            ActiveDoc.ApplyQueuedEdits("Expanded Event", true);
        }
Пример #6
0
        private void CompressEvent_Execute(Object sender, ApplyContentEventArgs ea)
        {
            ElementBuilder Builder = ea.NewElementBuilder();

            // Build Event and Add/Remove
            Event NewEvent = Builder.BuildEvent(_EventToCompress.Name, _EventToCompress.MemberTypeReference);
            NewEvent.Visibility = _EventToCompress.Visibility;

            TextDocument ActiveDoc = CodeRush.Documents.ActiveTextDocument;
            ActiveDoc.QueueDelete(_EventToCompressFieldDeclaration);
            ActiveDoc.QueueReplace(_EventToCompress, CodeRush.CodeMod.GenerateCode(NewEvent, true));
            ActiveDoc.ApplyQueuedEdits("Compressed Event");
        }