@@ -20,6 +20,7 @@ public class MoveCloserToUsageRefactoring : IRefactoring
20
20
private readonly VBE _vbe ;
21
21
private readonly RubberduckParserState _state ;
22
22
private readonly IMessageBox _messageBox ;
23
+ private Declaration _target ;
23
24
24
25
public MoveCloserToUsageRefactoring ( VBE vbe , RubberduckParserState state , IMessageBox messageBox )
25
26
{
@@ -45,16 +46,16 @@ public void Refactor()
45
46
46
47
public void Refactor ( QualifiedSelection selection )
47
48
{
48
- var target = _declarations . FindVariable ( selection ) ;
49
+ _target = _declarations . FindVariable ( selection ) ;
49
50
50
- if ( target == null )
51
+ if ( _target == null )
51
52
{
52
53
_messageBox . Show ( RubberduckUI . MoveCloserToUsage_InvalidSelection , RubberduckUI . IntroduceParameter_Caption ,
53
54
MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
54
55
return ;
55
56
}
56
57
57
- MoveCloserToUsage ( target ) ;
58
+ MoveCloserToUsage ( ) ;
58
59
}
59
60
60
61
public void Refactor ( Declaration target )
@@ -68,7 +69,8 @@ public void Refactor(Declaration target)
68
69
throw new ArgumentException ( "Invalid Argument. DeclarationType must be 'Variable'" , "target" ) ;
69
70
}
70
71
71
- MoveCloserToUsage ( target ) ;
72
+ _target = target ;
73
+ MoveCloserToUsage ( ) ;
72
74
}
73
75
74
76
private bool TargetIsReferencedFromMultipleMethods ( Declaration target )
@@ -78,69 +80,64 @@ private bool TargetIsReferencedFromMultipleMethods(Declaration target)
78
80
return firstReference != null && target . References . Any ( r => r . ParentScoping != firstReference . ParentScoping ) ;
79
81
}
80
82
81
- private void MoveCloserToUsage ( Declaration target )
83
+ private void MoveCloserToUsage ( )
82
84
{
83
- if ( ! target . References . Any ( ) )
85
+ if ( ! _target . References . Any ( ) )
84
86
{
85
- var message = string . Format ( RubberduckUI . MoveCloserToUsage_TargetHasNoReferences , target . IdentifierName ) ;
87
+ var message = string . Format ( RubberduckUI . MoveCloserToUsage_TargetHasNoReferences , _target . IdentifierName ) ;
86
88
87
89
_messageBox . Show ( message , RubberduckUI . MoveCloserToUsage_Caption , MessageBoxButtons . OK ,
88
90
MessageBoxIcon . Exclamation ) ;
89
91
90
92
return ;
91
93
}
92
94
93
- if ( TargetIsReferencedFromMultipleMethods ( target ) )
95
+ if ( TargetIsReferencedFromMultipleMethods ( _target ) )
94
96
{
95
- var message = string . Format ( RubberduckUI . MoveCloserToUsage_TargetIsUsedInMultipleMethods , target . IdentifierName ) ;
97
+ var message = string . Format ( RubberduckUI . MoveCloserToUsage_TargetIsUsedInMultipleMethods , _target . IdentifierName ) ;
96
98
_messageBox . Show ( message , RubberduckUI . MoveCloserToUsage_Caption , MessageBoxButtons . OK ,
97
99
MessageBoxIcon . Exclamation ) ;
98
100
99
101
return ;
100
102
}
101
103
102
104
// it doesn't make sense to do it backwards, but we need to work from the bottom up so our selections are accurate
103
- InsertDeclaration ( target ) ;
105
+ InsertDeclaration ( ) ;
104
106
105
- if ( target . QualifiedName . QualifiedModuleName . Component !=
106
- target . References . First ( ) . QualifiedModuleName . Component )
107
- {
108
- _state . StateChanged += ( sender , e ) =>
109
- {
110
- if ( e . State == ParserState . Ready )
111
- {
112
- foreach ( var newTarget in _state . AllUserDeclarations . Where ( newTarget => newTarget . ComponentName == target . ComponentName &&
113
- newTarget . IdentifierName == target . IdentifierName &&
114
- newTarget . ParentScope == target . ParentScope &&
115
- newTarget . Project == target . Project &&
116
- Equals ( newTarget . Selection , target . Selection ) ) )
117
- {
118
- UpdateCallsToOtherModule ( newTarget . References ) ;
119
- RemoveField ( newTarget ) ;
120
- break ;
121
- }
122
- }
123
- } ;
124
-
125
- _state . OnParseRequested ( this ) ;
126
- }
127
- else
107
+ _state . StateChanged += _state_StateChanged ;
108
+ _state . OnParseRequested ( this ) ;
109
+ }
110
+
111
+ private void _state_StateChanged ( object sender , ParserStateEventArgs e )
112
+ {
113
+ if ( e . State != ParserState . Ready ) { return ; }
114
+
115
+ var newTarget = _state . AllUserDeclarations . FirstOrDefault (
116
+ item => item . ComponentName == _target . ComponentName &&
117
+ item . IdentifierName == _target . IdentifierName &&
118
+ item . ParentScope == _target . ParentScope &&
119
+ item . Project == _target . Project &&
120
+ Equals ( item . Selection , _target . Selection ) ) ;
121
+
122
+ if ( newTarget != null )
128
123
{
129
- RemoveField ( target ) ;
124
+ UpdateCallsToOtherModule ( newTarget . References ) ;
125
+ RemoveField ( newTarget ) ;
130
126
}
131
127
128
+ _state . StateChanged -= _state_StateChanged ;
132
129
_state . OnParseRequested ( this ) ;
133
130
}
134
131
135
- private void InsertDeclaration ( Declaration target )
132
+ private void InsertDeclaration ( )
136
133
{
137
- var module = target . References . First ( ) . QualifiedModuleName . Component . CodeModule ;
134
+ var module = _target . References . First ( ) . QualifiedModuleName . Component . CodeModule ;
138
135
139
- var firstReference = target . References . OrderBy ( r => r . Selection . StartLine ) . First ( ) ;
136
+ var firstReference = _target . References . OrderBy ( r => r . Selection . StartLine ) . First ( ) ;
140
137
var beginningOfInstructionSelection = GetBeginningOfInstructionSelection ( firstReference ) ;
141
138
142
139
var oldLines = module . Lines [ beginningOfInstructionSelection . StartLine , beginningOfInstructionSelection . LineCount ] ;
143
- var newLines = oldLines . Insert ( beginningOfInstructionSelection . StartColumn - 1 , GetDeclarationString ( target ) ) ;
140
+ var newLines = oldLines . Insert ( beginningOfInstructionSelection . StartColumn - 1 , GetDeclarationString ( ) ) ;
144
141
145
142
var newLinesWithoutStringLiterals = newLines . StripStringLiterals ( ) ;
146
143
@@ -187,9 +184,9 @@ private Selection GetBeginningOfInstructionSelection(IdentifierReference referen
187
184
return new Selection ( currentLine , index , currentLine , index ) ;
188
185
}
189
186
190
- private string GetDeclarationString ( Declaration target )
187
+ private string GetDeclarationString ( )
191
188
{
192
- return Environment . NewLine + " Dim " + target . IdentifierName + " As " + target . AsTypeName + Environment . NewLine ;
189
+ return Environment . NewLine + " Dim " + _target . IdentifierName + " As " + _target . AsTypeName + Environment . NewLine ;
193
190
}
194
191
195
192
private void RemoveField ( Declaration target )
@@ -276,42 +273,42 @@ private string RemoveExtraComma(string str, int numParams, int indexRemoved)
276
273
return str . Remove ( str . NthIndexOf ( ',' , commaToRemove ) , 1 ) ;
277
274
}
278
275
279
- private void UpdateCallsToOtherModule ( IEnumerable < IdentifierReference > references )
276
+ private void UpdateCallsToOtherModule ( IEnumerable < IdentifierReference > references )
280
277
{
281
278
var identifierReferences = references . ToList ( ) ;
282
279
283
280
var module = identifierReferences [ 0 ] . QualifiedModuleName . Component . CodeModule ;
284
281
285
282
foreach ( var reference in identifierReferences . OrderByDescending ( o => o . Selection . StartLine ) . ThenByDescending ( t => t . Selection . StartColumn ) )
286
283
{
287
- // var parent = reference.Context.Parent;
288
- // while (!(parent is VBAParser.ICS_S_MembersCallContext ))
289
- // {
290
- // parent = parent.Parent;
291
- // }
292
-
293
- // var parentSelection = ((VBAParser.ICS_S_MembersCallContext )parent).GetSelection();
294
-
295
- // var oldText = module.Lines[parentSelection.StartLine, parentSelection.LineCount];
296
- // string newText;
297
-
298
- // if (parentSelection.LineCount == 1)
299
- // {
300
- // newText = oldText.Remove(parentSelection.StartColumn - 1,
301
- // parentSelection.EndColumn - parentSelection.StartColumn);
302
- // }
303
- // else
304
- // {
305
- // var lines = oldText.Split(new[] { " _" + Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
306
-
307
- // newText = lines.First().Remove(parentSelection.StartColumn - 1);
308
- // newText += lines.Last().Remove(0, parentSelection.EndColumn - 1);
309
- // }
310
-
311
- // newText = newText.Insert(parentSelection.StartColumn - 1, reference.IdentifierName);
312
-
313
- // module.DeleteLines(parentSelection.StartLine, parentSelection.LineCount);
314
- // module.InsertLines(parentSelection.StartLine, newText);
284
+ var parent = reference . Context . Parent ;
285
+ while ( ! ( parent is VBAParser . MemberAccessExprContext ) )
286
+ {
287
+ parent = parent . Parent ;
288
+ }
289
+
290
+ var parentSelection = ( ( VBAParser . MemberAccessExprContext ) parent ) . GetSelection ( ) ;
291
+
292
+ var oldText = module . Lines [ parentSelection . StartLine , parentSelection . LineCount ] ;
293
+ string newText ;
294
+
295
+ if ( parentSelection . LineCount == 1 )
296
+ {
297
+ newText = oldText . Remove ( parentSelection . StartColumn - 1 ,
298
+ parentSelection . EndColumn - parentSelection . StartColumn ) ;
299
+ }
300
+ else
301
+ {
302
+ var lines = oldText . Split ( new [ ] { " _" + Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries ) ;
303
+
304
+ newText = lines . First ( ) . Remove ( parentSelection . StartColumn - 1 ) ;
305
+ newText += lines . Last ( ) . Remove ( 0 , parentSelection . EndColumn - 1 ) ;
306
+ }
307
+
308
+ newText = newText . Insert ( parentSelection . StartColumn - 1 , reference . IdentifierName ) ;
309
+
310
+ module . DeleteLines ( parentSelection . StartLine , parentSelection . LineCount ) ;
311
+ module . InsertLines ( parentSelection . StartLine , newText ) ;
315
312
}
316
313
}
317
314
}
0 commit comments