File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed
Rubberduck.Refactorings/MoveCloserToUsage
RubberduckTests/Refactoring Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -155,9 +155,10 @@ private void InsertNewDeclaration()
155
155
{
156
156
var subscripts = _target . Context . GetDescendent < VBAParser . SubscriptsContext > ( ) ? . GetText ( ) ?? string . Empty ;
157
157
var identifier = _target . IsArray ? $ "{ _target . IdentifierName } ({ subscripts } )" : _target . IdentifierName ;
158
+
158
159
var newVariable = _target . AsTypeContext is null
159
160
? $ "{ Tokens . Dim } { identifier } { Tokens . As } { Tokens . Variant } { Environment . NewLine } "
160
- : $ "{ Tokens . Dim } { identifier } { Tokens . As } { _target . AsTypeNameWithoutArrayDesignator } { Environment . NewLine } ";
161
+ : $ "{ Tokens . Dim } { identifier } { Tokens . As } { ( _target . IsSelfAssigned ? Tokens . New + " " : string . Empty ) } { _target . AsTypeNameWithoutArrayDesignator } { Environment . NewLine } ";
161
162
162
163
var firstReference = _target . References . OrderBy ( r => r . Selection . StartLine ) . First ( ) ;
163
164
Original file line number Diff line number Diff line change
1
+ using System ;
1
2
using System . Linq ;
2
3
using System . Windows . Forms ;
3
4
using NUnit . Framework ;
@@ -1182,5 +1183,39 @@ Dim bar(1, 1) As Boolean
1182
1183
Assert . AreEqual ( expectedCode , rewriter . GetText ( ) ) ;
1183
1184
}
1184
1185
}
1186
+
1187
+ [ Test ]
1188
+ [ Category ( "Refactorings" ) ]
1189
+ [ Category ( "Move Closer" ) ]
1190
+ public void MoveCloserToUsageRefactoring_SelfAssigned ( )
1191
+ {
1192
+ //Input
1193
+ const string inputCode =
1194
+ @"Private bar As New Collection
1195
+ Private Sub Foo()
1196
+ bar.Add 42
1197
+ End Sub" ;
1198
+ var selection = new Selection ( 1 , 1 ) ;
1199
+
1200
+ //Expectation
1201
+ const string expectedCode =
1202
+ @"Private Sub Foo()
1203
+ Dim bar As New Collection
1204
+ bar.Add 42
1205
+ End Sub" ;
1206
+
1207
+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out var component , selection ) ;
1208
+ using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
1209
+ {
1210
+
1211
+ var qualifiedSelection = new QualifiedSelection ( new QualifiedModuleName ( component ) , selection ) ;
1212
+
1213
+ var refactoring = new MoveCloserToUsageRefactoring ( vbe . Object , state , null ) ;
1214
+ refactoring . Refactor ( qualifiedSelection ) ;
1215
+
1216
+ var rewriter = state . GetRewriter ( component ) ;
1217
+ Assert . AreEqual ( expectedCode , rewriter . GetText ( ) ) ;
1218
+ }
1219
+ }
1185
1220
}
1186
1221
}
You can’t perform that action at this time.
0 commit comments