1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using System . Windows . Forms ;
4
5
using Antlr4 . Runtime . Misc ;
5
6
using Microsoft . Vbe . Interop ;
6
7
using Rubberduck . Common ;
@@ -69,10 +70,34 @@ public void Refactor(Declaration target)
69
70
70
71
private void PromoteVariable ( Declaration target )
71
72
{
73
+ if ( ! PromptIfMethodImplementsInterface ( target ) )
74
+ {
75
+ return ;
76
+ }
77
+
72
78
RemoveVariable ( target ) ;
73
79
UpdateSignature ( target ) ;
74
80
}
75
81
82
+ private bool PromptIfMethodImplementsInterface ( Declaration targetVariable )
83
+ {
84
+ var functionDeclaration = _declarations . FindSelection ( targetVariable . QualifiedSelection , ValidDeclarationTypes ) ;
85
+ var interfaceImplementation = GetInterfaceImplementation ( functionDeclaration ) ;
86
+
87
+ if ( interfaceImplementation == null )
88
+ {
89
+ return true ;
90
+ }
91
+
92
+ var message = string . Format ( RubberduckUI . IntroduceParameter_PromptIfTargetIsInterface ,
93
+ functionDeclaration . IdentifierName , interfaceImplementation . ComponentName ,
94
+ interfaceImplementation . IdentifierName ) ;
95
+ var introduceParamToInterface = _messageBox . Show ( message , RubberduckUI . IntroduceParameter_Caption ,
96
+ MessageBoxButtons . YesNo , MessageBoxIcon . Question ) ;
97
+
98
+ return introduceParamToInterface != DialogResult . No ;
99
+ }
100
+
76
101
private void UpdateSignature ( Declaration targetVariable )
77
102
{
78
103
var functionDeclaration = _declarations . FindSelection ( targetVariable . QualifiedSelection , ValidDeclarationTypes ) ;
@@ -91,9 +116,20 @@ private void UpdateSignature(Declaration targetVariable)
91
116
}
92
117
93
118
var interfaceImplementation = GetInterfaceImplementation ( functionDeclaration ) ;
94
- if ( interfaceImplementation != null )
119
+ if ( interfaceImplementation == null )
120
+ {
121
+ return ;
122
+ }
123
+ UpdateSignature ( interfaceImplementation , targetVariable ) ;
124
+
125
+ var interfaceImplementations = _declarations . FindInterfaceImplementationMembers ( )
126
+ . Where ( item => item . Project . Equals ( interfaceImplementation . Project )
127
+ && item . IdentifierName == interfaceImplementation . ComponentName + "_" + interfaceImplementation . IdentifierName
128
+ && ! item . Equals ( functionDeclaration ) ) ;
129
+
130
+ foreach ( var implementation in interfaceImplementations )
95
131
{
96
- UpdateSignature ( interfaceImplementation , targetVariable ) ;
132
+ UpdateSignature ( implementation , targetVariable ) ;
97
133
}
98
134
}
99
135
0 commit comments