Skip to content

Commit f50ea1a

Browse files
committed
2 parents 5915c79 + dc44a16 commit f50ea1a

21 files changed

+1186
-599
lines changed

RetailCoder.VBE/Refactorings/RemoveParameters/RemoveParametersPresenter.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using System.Linq;
2-
using System.Windows.Forms;
3-
using Rubberduck.Parsing.Symbols;
1+
using System.Windows.Forms;
42
using Rubberduck.UI;
53

64
namespace Rubberduck.Refactorings.RemoveParameters
75
{
8-
public class RemoveParametersPresenter
6+
public interface IRemoveParametersPresenter
7+
{
8+
RemoveParametersModel Show();
9+
}
10+
11+
public class RemoveParametersPresenter : IRemoveParametersPresenter
912
{
1013
private readonly IRemoveParametersView _view;
1114
private readonly RemoveParametersModel _model;

RetailCoder.VBE/Refactorings/RemoveParameters/RemoveParametersRefactoring.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ namespace Rubberduck.Refactorings.RemoveParameters
1313
{
1414
public class RemoveParametersRefactoring : IRefactoring
1515
{
16-
private readonly IRefactoringPresenterFactory<RemoveParametersPresenter> _factory;
16+
private readonly IRefactoringPresenterFactory<IRemoveParametersPresenter> _factory;
1717
private RemoveParametersModel _model;
1818

19-
public RemoveParametersRefactoring(IRefactoringPresenterFactory<RemoveParametersPresenter> factory)
19+
public RemoveParametersRefactoring(IRefactoringPresenterFactory<IRemoveParametersPresenter> factory)
2020
{
2121
_factory = factory;
2222
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Linq;
2+
using System.Windows.Forms;
3+
using Microsoft.Vbe.Interop;
4+
using Rubberduck.Parsing;
5+
using Rubberduck.Parsing.Symbols;
6+
using Rubberduck.UI;
7+
using Rubberduck.VBEditor;
8+
9+
namespace Rubberduck.Refactorings.Rename
10+
{
11+
public class RenameModel
12+
{
13+
private readonly VBE _vbe;
14+
public VBE VBE { get { return _vbe; } }
15+
16+
private readonly Declarations _declarations;
17+
public Declarations Declarations { get { return _declarations; } }
18+
19+
private Declaration _target;
20+
public Declaration Target
21+
{
22+
get { return _target; }
23+
set { _target = value; }
24+
}
25+
26+
private readonly QualifiedSelection _selection;
27+
public QualifiedSelection Selection { get { return _selection; } }
28+
29+
private readonly VBProjectParseResult _parseResult;
30+
public VBProjectParseResult ParseResult { get { return _parseResult; } }
31+
32+
public string NewName { get; set; }
33+
34+
public RenameModel(VBE vbe, VBProjectParseResult parseResult, QualifiedSelection selection)
35+
{
36+
_vbe = vbe;
37+
_parseResult = parseResult;
38+
_declarations = parseResult.Declarations;
39+
_selection = selection;
40+
41+
AcquireTarget(out _target, Selection);
42+
}
43+
44+
private void AcquireTarget(out Declaration target, QualifiedSelection selection)
45+
{
46+
target = _declarations.Items
47+
.Where(item => !item.IsBuiltIn && item.DeclarationType != DeclarationType.ModuleOption)
48+
.FirstOrDefault(item => IsSelectedDeclaration(selection, item)
49+
|| IsSelectedReference(selection, item));
50+
51+
PromptIfTargetImplementsInterface(ref target);
52+
}
53+
54+
public void PromptIfTargetImplementsInterface(ref Declaration target)
55+
{
56+
var declaration = target;
57+
var interfaceImplementation = _declarations.FindInterfaceImplementationMembers().SingleOrDefault(m => m.Equals(declaration));
58+
if (target == null || interfaceImplementation == null)
59+
{
60+
return;
61+
}
62+
63+
var interfaceMember = _declarations.FindInterfaceMember(interfaceImplementation);
64+
var message = string.Format(RubberduckUI.RenamePresenter_TargetIsInterfaceMemberImplementation, target.IdentifierName, interfaceMember.ComponentName, interfaceMember.IdentifierName);
65+
66+
var confirm = MessageBox.Show(message, RubberduckUI.RenameDialog_TitleText, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
67+
if (confirm == DialogResult.No)
68+
{
69+
target = null;
70+
return;
71+
}
72+
73+
target = interfaceMember;
74+
}
75+
76+
private bool IsSelectedReference(QualifiedSelection selection, Declaration declaration)
77+
{
78+
return declaration.References.Any(r =>
79+
r.QualifiedModuleName.Project == selection.QualifiedName.Project
80+
&& r.QualifiedModuleName.ComponentName == selection.QualifiedName.ComponentName
81+
&& r.Selection.ContainsFirstCharacter(selection.Selection));
82+
}
83+
84+
private bool IsSelectedDeclaration(QualifiedSelection selection, Declaration declaration)
85+
{
86+
return declaration.QualifiedName.QualifiedModuleName.Project == selection.QualifiedName.Project
87+
&& declaration.QualifiedName.QualifiedModuleName.ComponentName == selection.QualifiedName.ComponentName
88+
&& (declaration.Selection.ContainsFirstCharacter(selection.Selection));
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)