Пример #1
0
        public override void Execute(object parameter)
        {
            var factory     = new ExtractMethodPresenterFactory(Editor, _state.AllDeclarations, _indenter);
            var refactoring = new ExtractMethodRefactoring(factory, Editor);

            refactoring.InvalidSelection += HandleInvalidSelection;
            refactoring.Refactor();
        }
        public void ExtractMethod_PrivateFunction()
        {
            const string inputCode = @"
Private Sub Foo()
    Dim x As Integer
    x = 1 + 2
End Sub";

            const string expectedCode = @"
Private Sub Foo()
    x = Bar()
End Sub

Private Function Bar() As Integer
    Dim x As Integer
    x = 1 + 2
    Bar = x
End Function

";

            var codePaneFactory = new CodePaneWrapperFactory();

            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe    = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         module = component.CodeModule;

            var mockHost = new Mock <IHostApplication>();

            mockHost.SetupAllProperties();
            var editor = new ActiveCodePaneEditor(module.VBE, codePaneFactory);
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState());

            parser.Parse();
            if (parser.State.Status == ParserState.Error)
            {
                Assert.Inconclusive("Parser Error");
            }

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(module.Parent), new Selection(4, 1, 4, 20));
            var model = new ExtractMethodModel(editor, parser.State.AllDeclarations, qualifiedSelection);

            model.Method.Accessibility = Accessibility.Private;
            model.Method.MethodName    = "Bar";
            model.Method.ReturnValue   = new ExtractedParameter("Integer", ExtractedParameter.PassedBy.ByVal, "x");
            model.Method.Parameters    = new List <ExtractedParameter>();

            var factory = SetupFactory(model);

            //act
            var refactoring = new ExtractMethodRefactoring(factory.Object, editor);

            refactoring.Refactor(qualifiedSelection);

            //assert
            Assert.AreEqual(expectedCode, module.Lines());
        }
        protected override void OnExecute(object parameter)
        {
            var declarations       = _state.AllDeclarations;
            var qualifiedSelection = _vbe.GetActiveSelection();

            var extractMethodValidation = new ExtractMethodSelectionValidation(declarations);
            var canExecute = extractMethodValidation.withinSingleProcedure(qualifiedSelection.Value);

            if (!canExecute)
            {
                return;
            }

            using (var pane = _vbe.ActiveCodePane)
                using (var module = pane.CodeModule)
                {
                    var extraction = new ExtractMethodExtraction();
                    // bug: access to disposed closure

                    // todo: make ExtractMethodRefactoring request reparse like everyone else.
                    var refactoring = new ExtractMethodRefactoring(module, ParseRequest, CreateMethodModel, extraction);
                    refactoring.InvalidSelection += HandleInvalidSelection;
                    refactoring.Refactor();


                    void ParseRequest(object obj) => _state.OnParseRequested(obj);

                    IExtractMethodModel CreateMethodModel(QualifiedSelection?qs, string code)
                    {
                        if (qs == null)
                        {
                            return(null);
                        }
                        //TODO: Pull these even further back;
                        //      and implement with IProvider<IExtractMethodRule>
                        var rules = new List <IExtractMethodRule>
                        {
                            new ExtractMethodRuleInSelection(),
                            new ExtractMethodRuleIsAssignedInSelection(),
                            new ExtractMethodRuleUsedAfter(),
                            new ExtractMethodRuleUsedBefore()
                        };

                        var paramClassify = new ExtractMethodParameterClassification(rules);

                        var extractedMethod      = new ExtractedMethod();
                        var extractedMethodModel = new ExtractMethodModel(extractedMethod, paramClassify);

                        extractedMethodModel.extract(declarations, qs.Value, code);
                        return(extractedMethodModel);
                    }
                }
        }
Пример #4
0
        private void ExtractMethod()
        {
            var progress = new ParsingProgressPresenter();
            var result   = progress.Parse(_parser, IDE.ActiveVBProject);

            var declarations = result.Declarations;
            var factory      = new ExtractMethodPresenterFactory(_editor, declarations);
            var refactoring  = new ExtractMethodRefactoring(factory, _editor);

            refactoring.InvalidSelection += refactoring_InvalidSelection;
            refactoring.Refactor();
        }
Пример #5
0
        void TestExtractMethod(string inputString, string outputString)
        {
            ExtractMethodRefactoring refactoring = new ExtractMethodRefactoring();
            RefactoringOptions       options     = CreateRefactoringOptions(inputString);

            ExtractMethodRefactoring.ExtractMethodParameters parameters = refactoring.CreateParameters(options);
            Assert.IsNotNull(parameters);
            parameters.Name           = "NewMethod";
            parameters.InsertionPoint = new Mono.TextEditor.InsertionPoint(new DocumentLocation(options.ResolveResult.CallingMember.BodyRegion.End.Line + 1, 1), NewLineInsertion.BlankLine, NewLineInsertion.None);
            List <Change> changes = refactoring.PerformChanges(options, parameters);
            string        output  = GetOutput(options, changes);

            Assert.IsTrue(CompareSource(output, outputString), "Expected:" + Environment.NewLine + outputString + Environment.NewLine + "was:" + Environment.NewLine + output);
        }
Пример #6
0
        public override void Execute(object parameter)
        {
            var declarations       = _state.AllDeclarations;
            var qualifiedSelection = Vbe.ActiveCodePane.GetQualifiedSelection();

            var extractMethodValidation = new ExtractMethodSelectionValidation(declarations);
            var canExecute = extractMethodValidation.withinSingleProcedure(qualifiedSelection.Value);

            if (!canExecute)
            {
                return;
            }
            ICodeModuleWrapper codeModuleWrapper = new CodeModuleWrapper(Vbe.ActiveCodePane.CodeModule);
            VBComponent        vbComponent       = Vbe.SelectedVBComponent;

            Func <QualifiedSelection?, string, IExtractMethodModel> createMethodModel = (qs, code) =>
            {
                if (qs == null)
                {
                    return(null);
                }
                //TODO: Pull these even further back;
                //      and implement with IProvider<IExtractMethodRule>
                var rules = new List <IExtractMethodRule>()
                {
                    new ExtractMethodRuleInSelection(),
                    new ExtractMethodRuleIsAssignedInSelection(),
                    new ExtractMethodRuleUsedAfter(),
                    new ExtractMethodRuleUsedBefore()
                };

                var paramClassify = new ExtractMethodParameterClassification(rules);

                var extractedMethod      = new ExtractedMethod();
                var extractedMethodModel = new ExtractMethodModel(extractedMethod, paramClassify);
                extractedMethodModel.extract(declarations, qs.Value, code);
                return(extractedMethodModel);
            };

            var             extraction   = new ExtractMethodExtraction();
            Action <Object> parseRequest = (obj) => _state.OnParseRequested(obj, vbComponent);

            var refactoring = new ExtractMethodRefactoring(codeModuleWrapper, parseRequest, createMethodModel, extraction);

            refactoring.InvalidSelection += HandleInvalidSelection;
            refactoring.Refactor();
        }
            public void shouldCallTheExtraction()
            {
                QualifiedModuleName   qualifiedModuleName;
                RubberduckParserState state;

                MockParser.ParseString(inputCode, out qualifiedModuleName, out state);

                var declarations = state.AllDeclarations;
                var selection    = new Selection(4, 4, 4, 14);
                QualifiedSelection?qualifiedSelection = new QualifiedSelection(qualifiedModuleName, selection);
                var codeModule = new Mock <ICodeModuleWrapper>();

                var emRules = new List <IExtractMethodRule>()
                {
                    new ExtractMethodRuleUsedAfter(), new ExtractMethodRuleUsedBefore(), new ExtractMethodRuleInSelection(), new ExtractMethodRuleIsAssignedInSelection()
                };
                var             extractedMethod = new Mock <IExtractedMethod>();
                Action <object> onParseRequest  = (obj) => { };

                extractedMethod.Setup(em => em.MethodName).Returns("Bar");
                var paramClassify = new Mock <IExtractMethodParameterClassification>();
                var model         = new ExtractMethodModel(extractedMethod.Object, paramClassify.Object);

                model.extract(declarations, qualifiedSelection.Value, extractCode);
                var insertCode = "Bar x";

                Func <QualifiedSelection?, string, IExtractMethodModel> createMethodModel = (q, s) => { return(model); };

                codeModule.SetupGet(cm => cm.QualifiedSelection).Returns(qualifiedSelection);
                codeModule.Setup(cm => cm.GetLines(selection)).Returns(extractCode);
                codeModule.Setup(cm => cm.DeleteLines(It.IsAny <Selection>()));
                codeModule.Setup(cm => cm.InsertLines(It.IsAny <int>(), It.IsAny <String>()));

                var extraction = new Mock <IExtractMethodExtraction>();

                var SUT = new ExtractMethodRefactoring(codeModule.Object, onParseRequest, createMethodModel, extraction.Object);

                SUT.Refactor();

                extraction.Verify(ext => ext.apply(codeModule.Object, It.IsAny <IExtractMethodModel>(), selection));
            }