|
| 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