Пример #1
0
                public void shouldReturnFalse()
                {
                    var inputCode = @"
Option Explicit
Private Sub Foo(byval a as long, _
                byval b as long)

    Dim x As Integer
    x = 1 + 2
End Sub


Private Sub NewMethod
End Sub";


                    QualifiedModuleName qualifiedModuleName;

                    using (var state = MockParser.ParseString(inputCode, out qualifiedModuleName))
                    {
                        var declarations = state.AllDeclarations;
                        var selection    = new Selection(4, 4, 7, 14);
                        QualifiedSelection?qSelection = new QualifiedSelection(qualifiedModuleName, selection);

                        var SUT = new ExtractMethodSelectionValidation(declarations);

                        var actual = SUT.withinSingleProcedure(qSelection.Value);

                        var expected = false;
                        Assert.AreEqual(expected, actual);
                    }
                }
Пример #2
0
        public override bool CanExecute(object parameter)
        {
            if (Vbe.ActiveCodePane == null || _state.Status != ParserState.Ready)
            {
                return(false);
            }

            var qualifiedSelection = Vbe.ActiveCodePane.GetQualifiedSelection();

            if (!qualifiedSelection.HasValue)
            {
                return(false);
            }
            var selection = qualifiedSelection.Value.Selection;

            var code = Vbe.ActiveCodePane.CodeModule.Lines[selection.StartLine, selection.LineCount];

            var allDeclarations         = _state.AllDeclarations;
            var extractMethodValidation = new ExtractMethodSelectionValidation(allDeclarations);
            //var parentProcedure = _state.AllDeclarations.FindSelectedDeclaration(qualifiedSelection.Value, DeclarationExtensions.ProcedureTypes, d => ((ParserRuleContext)d.Context.Parent).GetSelection());
            var canExecute = extractMethodValidation.withinSingleProcedure(qualifiedSelection.Value);

            /*
             * var canExecute = parentProcedure != null
             *  && selection.StartColumn != selection.EndColumn
             *  && selection.LineCount > 0
             *  && !string.IsNullOrWhiteSpace(code);
             */

            _logger.Debug("{0}.CanExecute evaluates to {1}", GetType().Name, canExecute);
            return(canExecute);
        }
Пример #3
0
        protected override bool CanExecuteImpl(object parameter)
        {
            if (Vbe.ActiveCodePane == null || _state.Status != ParserState.Ready)
            {
                return(false);
            }

            var pane   = Vbe.ActiveCodePane;
            var module = pane.CodeModule;
            {
                var qualifiedSelection = pane.GetQualifiedSelection();
                if (!qualifiedSelection.HasValue || module.IsWrappingNullReference)
                {
                    return(false);
                }
                var selection = qualifiedSelection.Value.Selection;

                var code = module.GetLines(selection.StartLine, selection.LineCount);

                var allDeclarations         = _state.AllDeclarations;
                var extractMethodValidation = new ExtractMethodSelectionValidation(allDeclarations);
                //var parentProcedure = _state.AllDeclarations.FindSelectedDeclaration(qualifiedSelection.Value, DeclarationExtensions.ProcedureTypes, d => ((ParserRuleContext)d.Context.Parent).GetSelection());
                var canExecute = extractMethodValidation.withinSingleProcedure(qualifiedSelection.Value);

                /*
                 * var canExecute = parentProcedure != null
                 *  && selection.StartColumn != selection.EndColumn
                 *  && selection.LineCount > 0
                 *  && !string.IsNullOrWhiteSpace(code);
                 */

                return(canExecute);
            }
        }
        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);
                    }
                }
        }
Пример #5
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();
        }
        private bool SpecialEvaluateCanExecute(object parameter)
        {
            var qualifiedSelection = _vbe.GetActiveSelection();

            if (!qualifiedSelection.HasValue)
            {
                return(false);
            }
            if (_state.IsNewOrModified(qualifiedSelection.Value.QualifiedName))
            {
                return(false);
            }

            var allDeclarations         = _state.AllDeclarations;
            var extractMethodValidation = new ExtractMethodSelectionValidation(allDeclarations);

            var canExecute = extractMethodValidation.withinSingleProcedure(qualifiedSelection.Value);

            return(canExecute);
        }
Пример #7
0
                public void shouldReturnTrue()
                {
                    QualifiedModuleName   qualifiedModuleName;
                    RubberduckParserState state;
                    var inputCode = @"
Option Explicit
Private Sub Foo()
    Dim x As Integer
    x = 1 + 2
End Sub


Private Sub NewMethod
    dim a as string
    Debug.Print a
End Sub


Private Sub NewMethod4
    dim a as string

    Debug.Print a
End Sub";

                    MockParser.ParseString(inputCode, out qualifiedModuleName, out state);
                    var declarations = state.AllDeclarations;
                    var selection    = new Selection(4, 4, 5, 14);
                    QualifiedSelection?qSelection = new QualifiedSelection(qualifiedModuleName, selection);

                    var SUT = new ExtractMethodSelectionValidation(declarations);

                    var actual = SUT.withinSingleProcedure(qSelection.Value);

                    var expected = true;

                    Assert.AreEqual(expected, actual);
                }