Skip to content

Commit 26bd6f2

Browse files
committed
Further tests and refinements, including new method name safety
1 parent 83b71af commit 26bd6f2

File tree

14 files changed

+764
-932
lines changed

14 files changed

+764
-932
lines changed

Rubberduck.Core/UI/Command/Refactorings/Notifiers/ExtractMethodFailedNotifier.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Rubberduck.Interaction;
22
using Rubberduck.Refactorings;
3+
using Rubberduck.Refactorings.Exceptions;
4+
using Rubberduck.Refactorings.Exceptions.ExtractMethod;
35

46
namespace Rubberduck.UI.Command.Refactorings.Notifiers
57
{
@@ -10,5 +12,17 @@ public ExtractMethodFailedNotifier(IMessageBox messageBox)
1012
{}
1113

1214
protected override string Caption => RefactoringsUI.ExtractMethod_Caption;
15+
16+
protected override string Message(RefactoringException exception)
17+
{
18+
switch (exception)
19+
{
20+
case UnableToMoveVariableDeclarationException unableToMoveVariableDeclaration:
21+
Logger.Warn(unableToMoveVariableDeclaration);
22+
return RefactoringsUI.ExtractMethod_InvalidSelectionMessage; //TODO - improve this message
23+
default:
24+
return base.Message(exception);
25+
}
26+
}
1327
}
1428
}
Lines changed: 1 addition & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
using System.Linq;
2-
using System.Runtime.InteropServices;
31
using Rubberduck.Parsing.VBA;
42
using Rubberduck.Refactorings.ExtractMethod;
5-
using Rubberduck.Refactorings;
6-
using Rubberduck.Settings;
7-
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
83
using Rubberduck.UI.Command.Refactorings.Notifiers;
94
using Rubberduck.VBEditor.Utility;
10-
using Rubberduck.Parsing.Symbols;
115

126
namespace Rubberduck.UI.Command.Refactorings
137
{
148
public class RefactorExtractMethodCommand : RefactorCodePaneCommandBase
159
{
1610
private readonly RubberduckParserState _state;
17-
private readonly IRefactoringFactory<ExtractMethodRefactoring> _refactoringFactory;
18-
private readonly IVBE _vbe;
1911
private readonly ISelectedDeclarationProvider _selectedDeclarationProvider;
2012
private readonly ISelectionProvider _selectionProvider;
2113

@@ -36,7 +28,7 @@ public RefactorExtractMethodCommand(
3628
private bool SpecializedEvaluateCanExecute(object parameter)
3729
{
3830
var member = _selectedDeclarationProvider.SelectedDeclaration();
39-
var moduleContext = _selectedDeclarationProvider.SelectedModule().Context;
31+
//var moduleContext = _selectedDeclarationProvider.SelectedModule().Context;
4032
var moduleName = _selectedDeclarationProvider.SelectedModule().QualifiedModuleName;
4133

4234
if (member == null || _state.IsNewOrModified(member.QualifiedModuleName) || !_selectionProvider.Selection(moduleName).HasValue)
@@ -55,118 +47,5 @@ private bool SpecializedEvaluateCanExecute(object parameter)
5547
// ? parameters.Count > 2
5648
// : parameters.Count > 1;
5749
}
58-
59-
//protected bool EvaluateCanExecute(object parameter)
60-
//{
61-
// if (_vbe.ActiveCodePane == null || _state.Status != ParserState.Ready)
62-
// {
63-
// return false;
64-
// }
65-
66-
// var pane = _vbe.ActiveCodePane;
67-
// var module = pane.CodeModule;
68-
// {
69-
// var qualifiedSelection = pane.GetQualifiedSelection();
70-
// if (!qualifiedSelection.HasValue || module.IsWrappingNullReference)
71-
// {
72-
// return false;
73-
// }
74-
75-
// var validator = new ExtractMethodSelectionValidation(_state.AllDeclarations, module);
76-
// var canExecute = validator.ValidateSelection(qualifiedSelection.Value);
77-
78-
// return canExecute;
79-
// }
80-
//}
81-
82-
//protected override void OnExecute(object parameter)
83-
//{
84-
// var qualifiedSelection = _vbe.ActiveCodePane.GetQualifiedSelection();
85-
86-
// if (qualifiedSelection == null)
87-
// {
88-
// return;
89-
// }
90-
91-
// var pane = _vbe.ActiveCodePane;
92-
// if (pane == null)
93-
// {
94-
// return;
95-
// }
96-
97-
// var module = pane.CodeModule;
98-
// var selection = module.GetQualifiedSelection();
99-
100-
// if (selection == null)
101-
// {
102-
// return;
103-
// }
104-
105-
// var validator = new ExtractMethodSelectionValidation(_state.AllDeclarations, module);
106-
// var canExecute = validator.ValidateSelection(qualifiedSelection.Value);
107-
108-
// if (!canExecute)
109-
// {
110-
// return;
111-
// }
112-
113-
// /* TODO: Refactor the section to make command ignorant of data
114-
// * This section needs to be refactored. The way it is, the command knows too much
115-
// * about the validator and the refactoring. Getting data from validator should
116-
// * be refactoring's responsibility, which implies the validation is refactoring's
117-
// * responsiblity. Note where indicated.
118-
// */
119-
120-
121-
// var refactoring = _refactoringFactory.Create();
122-
// refactoring.Validator = validator; //TODO: Refactor
123-
// refactoring.Refactor(selection.Value);
124-
// _refactoringFactory.Release(refactoring);
125-
126-
// /*
127-
// using (var view = new ExtractMethodDialog(new ExtractMethodViewModel()))
128-
// {
129-
// var factory = new ExtractMethodPresenterFactory(Vbe, view, _indenter, _state, qualifiedSelection.Value);
130-
// var refactoring = new ExtractMethodRefactoring(Vbe, module, factory);
131-
// refactoring.Refactor(qualifiedSelection.Value);
132-
// }
133-
// */
134-
135-
// /*
136-
// {
137-
// Func<QualifiedSelection?, string, IExtractMethodModel> createMethodModel = (qs, code) =>
138-
// {
139-
// if (qs == null)
140-
// {
141-
// return null;
142-
// }
143-
// //TODO: Pull these even further back;
144-
// // and implement with IProvider<IExtractMethodRule>
145-
// var rules = new List<IExtractMethodRule>
146-
// {
147-
// new ExtractMethodRuleInSelection(),
148-
// new ExtractMethodRuleIsAssignedInSelection(),
149-
// new ExtractMethodRuleUsedAfter(),
150-
// new ExtractMethodRuleUsedBefore()
151-
// };
152-
153-
// var paramClassify = new ExtractMethodParameterClassification(rules);
154-
155-
// var extractedMethod = new ExtractedMethod();
156-
// var extractedMethodModel = new ExtractMethodModel(extractedMethod, paramClassify);
157-
// extractedMethodModel.extract(declarations, qs.Value, code);
158-
// return extractedMethodModel;
159-
// };
160-
161-
// var extraction = new ExtractMethodExtraction();
162-
// // bug: access to disposed closure - todo: make ExtractMethodRefactoring request reparse like everyone else.
163-
// Action<object> parseRequest = obj => _state.OnParseRequested(obj);
164-
165-
// var refactoring = new ExtractMethodRefactoring(module, parseRequest, createMethodModel, extraction);
166-
// refactoring.InvalidSelection += HandleInvalidSelection;
167-
// refactoring.Refactor();
168-
// }
169-
// */
170-
//}
17150
}
17251
}

Rubberduck.Core/UI/Refactorings/ExtractMethod/ExtractMethodViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public ExtractMethodParameter ReturnParameter
9595

9696
public string SourceMethodName => Model.SourceMethodName;
9797
public string PreviewCaption => string.Format(RefactoringsUI.ExtractMethod_CodePreviewCaption, SourceMethodName);
98-
public string PreviewCode => Model.PreviewCode; //Model.ReplacementCode + Environment.NewLine + Model.PreviewCode; //any way to get a divider for showing both sections of code in one textbox?
98+
public string PreviewCode => Model.NewMethodCode; //Model.ReplacementCode + Environment.NewLine + Model.PreviewCode; //any way to get a divider for showing both sections of code in one textbox?
9999
public IEnumerable<ExtractMethodParameter> Inputs;
100100
public IEnumerable<ExtractMethodParameter> Outputs;
101101
public IEnumerable<ExtractMethodParameter> Locals;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Rubberduck.Parsing.Symbols;
2+
3+
namespace Rubberduck.Refactorings.Exceptions.ExtractMethod
4+
{
5+
public class UnableToMoveVariableDeclarationException : RefactoringException
6+
{
7+
public override string Message { get; }
8+
public UnableToMoveVariableDeclarationException(Declaration problemDeclaration)
9+
{
10+
Message = problemDeclaration.Context.GetText();
11+
}
12+
}
13+
}
14+

0 commit comments

Comments
 (0)