3
3
using System . Runtime . InteropServices ;
4
4
using System . Text . RegularExpressions ;
5
5
using System . Windows . Forms ;
6
+ using Antlr4 . Runtime ;
7
+ using Antlr4 . Runtime . Misc ;
6
8
using Microsoft . Vbe . Interop ;
7
9
using Rubberduck . Extensions ;
8
10
using Rubberduck . Parsing ;
@@ -17,14 +19,16 @@ public class RenamePresenter
17
19
private readonly IRenameView _view ;
18
20
private readonly Declarations _declarations ;
19
21
private readonly QualifiedSelection _selection ;
22
+ private readonly VBProjectParseResult _parseResult ;
20
23
21
- public RenamePresenter ( VBE vbe , IRenameView view , Declarations declarations , QualifiedSelection selection )
24
+ public RenamePresenter ( VBE vbe , IRenameView view , VBProjectParseResult parseResult , QualifiedSelection selection )
22
25
{
23
26
_vbe = vbe ;
24
27
_view = view ;
25
28
_view . OkButtonClicked += OnOkButtonClicked ;
26
29
27
- _declarations = declarations ;
30
+ _parseResult = parseResult ;
31
+ _declarations = parseResult . Declarations ;
28
32
_selection = selection ;
29
33
}
30
34
@@ -86,8 +90,7 @@ private void RenameDeclaration()
86
90
}
87
91
88
92
var module = _vbe . FindCodeModules ( _view . Target . QualifiedName . QualifiedModuleName ) . First ( ) ;
89
- var content = module . get_Lines ( _view . Target . Selection . StartLine , 1 ) ;
90
- var newContent = GetReplacementLine ( content , _view . Target . IdentifierName , _view . NewName ) ;
93
+ var newContent = GetReplacementLine ( module , _view . Target , _view . NewName ) ;
91
94
module . ReplaceLine ( _view . Target . Selection . StartLine , newContent ) ;
92
95
}
93
96
@@ -184,6 +187,89 @@ private string GetReplacementLine(string content, string target, string newName)
184
187
return Regex . Replace ( content , "\\ b" + target + "\\ b" , newName ) ;
185
188
}
186
189
190
+ private string GetReplacementLine ( CodeModule module , Declaration target , string newName )
191
+ {
192
+ var targetModule = _parseResult . ComponentParseResults . SingleOrDefault ( m => m . QualifiedName == _view . Target . QualifiedName . QualifiedModuleName ) ;
193
+ if ( targetModule == null )
194
+ {
195
+ return null ;
196
+ }
197
+
198
+ var content = module . get_Lines ( _view . Target . Selection . StartLine , 1 ) ;
199
+
200
+ if ( target . DeclarationType == DeclarationType . Parameter )
201
+ {
202
+ var argContext = ( VBAParser . ArgContext ) _view . Target . Context ;
203
+ targetModule . Rewriter . Replace ( argContext . ambiguousIdentifier ( ) . Start . TokenIndex , _view . NewName ) ;
204
+
205
+ // Target.Context is an ArgContext, its parent is an ArgsListContext;
206
+ // the ArgsListContext's parent is the procedure context and it includes the body.
207
+ var context = ( ParserRuleContext ) _view . Target . Context . Parent . Parent ;
208
+ var firstTokenIndex = context . Start . TokenIndex ;
209
+ var lastTokenIndex = - 1 ; // will blow up if this code runs for any context other than below
210
+
211
+ var subStmtContext = context as VBAParser . SubStmtContext ;
212
+ if ( subStmtContext != null )
213
+ {
214
+ lastTokenIndex = subStmtContext . argList ( ) . RPAREN ( ) . Symbol . TokenIndex ;
215
+ }
216
+
217
+ var functionStmtContext = context as VBAParser . FunctionStmtContext ;
218
+ if ( functionStmtContext != null )
219
+ {
220
+ lastTokenIndex = functionStmtContext . asTypeClause ( ) != null
221
+ ? functionStmtContext . asTypeClause ( ) . Stop . TokenIndex
222
+ : functionStmtContext . argList ( ) . RPAREN ( ) . Symbol . TokenIndex ;
223
+ }
224
+
225
+ var propertyGetStmtContext = context as VBAParser . PropertyGetStmtContext ;
226
+ if ( propertyGetStmtContext != null )
227
+ {
228
+ lastTokenIndex = propertyGetStmtContext . asTypeClause ( ) != null
229
+ ? propertyGetStmtContext . asTypeClause ( ) . Stop . TokenIndex
230
+ : propertyGetStmtContext . argList ( ) . RPAREN ( ) . Symbol . TokenIndex ;
231
+ }
232
+
233
+ var propertyLetStmtContext = context as VBAParser . PropertyLetStmtContext ;
234
+ if ( propertyLetStmtContext != null )
235
+ {
236
+ lastTokenIndex = propertyLetStmtContext . argList ( ) . RPAREN ( ) . Symbol . TokenIndex ;
237
+ }
238
+
239
+ var propertySetStmtContext = context as VBAParser . PropertySetStmtContext ;
240
+ if ( propertySetStmtContext != null )
241
+ {
242
+ lastTokenIndex = propertySetStmtContext . argList ( ) . RPAREN ( ) . Symbol . TokenIndex ;
243
+ }
244
+
245
+ var declareStmtContext = context as VBAParser . DeclareStmtContext ;
246
+ if ( declareStmtContext != null )
247
+ {
248
+ lastTokenIndex = declareStmtContext . STRINGLITERAL ( ) . Last ( ) . Symbol . TokenIndex ;
249
+ if ( declareStmtContext . argList ( ) != null )
250
+ {
251
+ lastTokenIndex = declareStmtContext . argList ( ) . RPAREN ( ) . Symbol . TokenIndex ;
252
+ }
253
+ if ( declareStmtContext . asTypeClause ( ) != null )
254
+ {
255
+ lastTokenIndex = declareStmtContext . asTypeClause ( ) . Stop . TokenIndex ;
256
+ }
257
+ }
258
+
259
+ var eventStmtContext = context as VBAParser . EventStmtContext ;
260
+ if ( eventStmtContext != null )
261
+ {
262
+ lastTokenIndex = eventStmtContext . argList ( ) . RPAREN ( ) . Symbol . TokenIndex ;
263
+ }
264
+
265
+ return targetModule . Rewriter . GetText ( new Interval ( firstTokenIndex , lastTokenIndex ) ) ;
266
+ }
267
+ else
268
+ {
269
+ return GetReplacementLine ( content , target . IdentifierName , newName ) ;
270
+ }
271
+ }
272
+
187
273
private static readonly DeclarationType [ ] ProcedureDeclarationTypes =
188
274
{
189
275
DeclarationType . Procedure ,
0 commit comments