private static Mock<IRefactoringPresenterFactory<IExtractMethodPresenter>> SetupFactory(ExtractMethodModel model) { var presenter = new Mock<IExtractMethodPresenter>(); presenter.Setup(p => p.Show()).Returns(model); var factory = new Mock<IRefactoringPresenterFactory<IExtractMethodPresenter>>(); factory.Setup(f => f.Create()).Returns(presenter.Object); return factory; }
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()); }
public static string GetExtractedMethod(ExtractMethodModel model) { var newLine = Environment.NewLine; var access = model.Method.Accessibility.ToString(); var keyword = Tokens.Sub; var asTypeClause = string.Empty; var isFunction = model.Method.ReturnValue != null && model.Method.ReturnValue.Name != ExtractedParameter.None; if (isFunction) { keyword = Tokens.Function; asTypeClause = Tokens.As + ' ' + model.Method.ReturnValue.TypeName; } var parameters = "(" + string.Join(", ", model.Method.Parameters) + ")"; var result = access + ' ' + keyword + ' ' + model.Method.MethodName + parameters + ' ' + asTypeClause + newLine; var localConsts = model.Locals.Where(e => e.DeclarationType == DeclarationType.Constant) .Cast <ValuedDeclaration>() .Select(e => " " + Tokens.Const + ' ' + e.IdentifierName + ' ' + Tokens.As + ' ' + e.AsTypeName + " = " + e.Value); var localVariables = model.Locals.Where(e => e.DeclarationType == DeclarationType.Variable) .Where(e => model.Method.Parameters.All(param => param.Name != e.IdentifierName)) .Select(e => e.Context) .Cast <VBAParser.VariableSubStmtContext>() .Select(e => " " + Tokens.Dim + ' ' + e.identifier().GetText() + (e.LPAREN() == null ? string.Empty : e.LPAREN().GetText() + (e.subscripts() == null ? string.Empty : e.subscripts().GetText()) + e.RPAREN().GetText()) + ' ' + (e.asTypeClause() == null ? string.Empty : e.asTypeClause().GetText())); var locals = string.Join(newLine, localConsts.Union(localVariables) .Where(local => !model.SelectedCode.Contains(local)).ToArray()) + newLine; result += locals + model.SelectedCode + newLine; if (isFunction) { // return value by assigning the method itself: var setter = model.Method.SetReturnValue ? Tokens.Set + ' ' : string.Empty; result += " " + setter + model.Method.MethodName + " = " + model.Method.ReturnValue.Name + newLine; } result += Tokens.End + ' ' + keyword + newLine; return(newLine + result + newLine); }
private string GetMethodCall(ExtractMethodModel model) { string result; var returnValueName = model.Method.ReturnValue.Name; var argsList = string.Join(", ", model.Method.Parameters.Select(p => p.Name)); if (returnValueName != ExtractedParameter.None) { var setter = model.Method.SetReturnValue ? Tokens.Set + ' ' : string.Empty; result = setter + returnValueName + " = " + model.Method.MethodName + '(' + argsList + ')'; } else { result = model.Method.MethodName + ' ' + argsList; } return " " + result; // todo: smarter indentation }
private string GetMethodCall(ExtractMethodModel model) { string result; var returnValueName = model.Method.ReturnValue.Name; var argsList = string.Join(", ", model.Method.Parameters.Select(p => p.Name)); if (returnValueName != ExtractedParameter.None) { var setter = model.Method.SetReturnValue ? Tokens.Set + ' ' : string.Empty; result = setter + returnValueName + " = " + model.Method.MethodName + '(' + argsList + ')'; } else { result = model.Method.MethodName + ' ' + argsList; } return(" " + result); // todo: smarter indentation }
public IExtractMethodPresenter Create() { var selection = _editor.GetSelection(); if (selection == null) { return(null); } ExtractMethodModel model; try { model = new ExtractMethodModel(_editor, _declarations, selection.Value); } catch (InvalidOperationException) { return(null); } var view = new ExtractMethodDialog(); return(new ExtractMethodPresenter(view, model, _indenter)); }
private void ExtractMethod(ExtractMethodModel model) { var selection = model.Selection.Selection; _editor.DeleteLines(selection); _editor.InsertLines(selection.StartLine, GetMethodCall(model)); var insertionLine = model.SourceMember.Context.GetSelection().EndLine - selection.LineCount + 2; _editor.InsertLines(insertionLine, GetExtractedMethod(model)); // assumes these are declared *before* the selection... var offset = 0; foreach (var declaration in model.DeclarationsToMove.OrderBy(e => e.Selection.StartLine)) { var target = new Selection( declaration.Selection.StartLine - offset, declaration.Selection.StartColumn, declaration.Selection.EndLine - offset, declaration.Selection.EndColumn); _editor.DeleteLines(target); offset += declaration.Selection.LineCount; } }
public ExtractMethodPresenter(IExtractMethodDialog view, ExtractMethodModel model) { _view = view; _model = model; }
public ExtractMethodPresenter(IExtractMethodDialog view, ExtractMethodModel model, IIndenter indenter) { _view = view; _model = model; _indenter = indenter; }
public static string GetExtractedMethod(ExtractMethodModel model) { var newLine = Environment.NewLine; var access = model.Method.Accessibility.ToString(); var keyword = Tokens.Sub; var asTypeClause = string.Empty; var isFunction = model.Method.ReturnValue != null && model.Method.ReturnValue.Name != ExtractedParameter.None; if (isFunction) { keyword = Tokens.Function; asTypeClause = Tokens.As + ' ' + model.Method.ReturnValue.TypeName; } var parameters = "(" + string.Join(", ", model.Method.Parameters) + ")"; var result = access + ' ' + keyword + ' ' + model.Method.MethodName + parameters + ' ' + asTypeClause + newLine; var localConsts = model.Locals.Where(e => e.DeclarationType == DeclarationType.Constant) .Cast<ValuedDeclaration>() .Select(e => " " + Tokens.Const + ' ' + e.IdentifierName + ' ' + Tokens.As + ' ' + e.AsTypeName + " = " + e.Value); var localVariables = model.Locals.Where(e => e.DeclarationType == DeclarationType.Variable) .Where(e => model.Method.Parameters.All(param => param.Name != e.IdentifierName)) .Select(e => e.Context) .Cast<VBAParser.VariableSubStmtContext>() .Select(e => " " + Tokens.Dim + ' ' + e.ambiguousIdentifier().GetText() + (e.LPAREN() == null ? string.Empty : e.LPAREN().GetText() + (e.subscripts() == null ? string.Empty : e.subscripts().GetText()) + e.RPAREN().GetText()) + ' ' + (e.asTypeClause() == null ? string.Empty : e.asTypeClause().GetText())); var locals = string.Join(newLine, localConsts.Union(localVariables) .Where(local => !model.SelectedCode.Contains(local)).ToArray()) + newLine; result += locals + model.SelectedCode + newLine; if (isFunction) { // return value by assigning the method itself: var setter = model.Method.SetReturnValue ? Tokens.Set + ' ' : string.Empty; result += " " + setter + model.Method.MethodName + " = " + model.Method.ReturnValue.Name + newLine; } result += Tokens.End + ' ' + keyword + newLine; return newLine + result + newLine; }
public ExtractMethodPresenter(IExtractMethodDialog view, ExtractMethodModel model, IIndenter indenter) { _view = view; _model = model; _indenter = indenter; }
public ExtractMethodPresenter(IExtractMethodDialog view, ExtractMethodModel model) { _view = view; _model = model; }