A refactoring that extracts a method (procedure or function) out of a selection in the active code pane and replaces the selected code with a call to the extracted method.
Inheritance: IRefactoring
 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();
 }
Beispiel #2
0
        private void GeneratePreview()
        {
            _model.Method.MethodName     = _view.MethodName;
            _model.Method.Accessibility  = _view.Accessibility;
            _model.Method.Parameters     = _view.Parameters;
            _model.Method.ReturnValue    = _view.ReturnValue;
            _model.Method.SetReturnValue = _view.SetReturnValue;

            _view.Preview = ExtractMethodRefactoring.GetExtractedMethod(_model);
        }
        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());
        }
Beispiel #4
0
        private void GeneratePreview()
        {
            _model.Method.MethodName     = _view.MethodName;
            _model.Method.Accessibility  = _view.Accessibility;
            _model.Method.Parameters     = _view.Parameters;
            _model.Method.ReturnValue    = _view.ReturnValue;
            _model.Method.SetReturnValue = _view.SetReturnValue;

            var code = ExtractMethodRefactoring.GetExtractedMethod(_model).Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            _indenter.Indent(code, "Preview", false);

            _view.Preview = string.Join(Environment.NewLine, code);
        }
        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();
        }