1
+ using System ;
1
2
using Antlr4 . Runtime ;
3
+ using Rubberduck . Parsing . Grammar ;
2
4
using Rubberduck . VBEditor ;
3
5
4
6
namespace Rubberduck . Inspections
@@ -15,16 +17,38 @@ public ChangeParameterByRefByValQuickFix(ParserRuleContext context, QualifiedSel
15
17
16
18
public override void Fix ( )
17
19
{
18
- var parameter = Context . GetText ( ) ;
19
- var newContent = string . Concat ( _newToken , " " , parameter ) ;
20
- var selection = Selection . Selection ;
21
-
22
- var module = Selection . QualifiedName . Component . CodeModule ;
20
+ try
23
21
{
24
- var lines = module . GetLines ( selection . StartLine , selection . LineCount ) ;
25
- var result = lines . Replace ( parameter , newContent ) ;
26
- module . ReplaceLine ( selection . StartLine , result ) ;
22
+ dynamic context = Context ;
23
+ var parameter = Context . GetText ( ) ;
24
+ dynamic args = Context . parent ;
25
+ var argList = args . GetText ( ) ;
26
+ var module = Selection . QualifiedName . Component . CodeModule ;
27
+ {
28
+ string result ;
29
+ if ( context . OPTIONAL ( ) != null )
30
+ {
31
+ result = parameter . Replace ( Tokens . Optional , Tokens . Optional + ' ' + _newToken ) ;
32
+ }
33
+ else
34
+ {
35
+ result = _newToken + ' ' + parameter ;
36
+ }
37
+
38
+ dynamic proc = args . parent ;
39
+ var startLine = proc . GetType ( ) . GetProperty ( "Start" ) . GetValue ( proc ) . Line ;
40
+ var stopLine = proc . GetType ( ) . GetProperty ( "Stop" ) . GetValue ( proc ) . Line ;
41
+ var code = module . GetLines ( startLine , stopLine - startLine + 1 ) ;
42
+ result = code . Replace ( argList , argList . Replace ( parameter , result ) ) ;
43
+
44
+ foreach ( var line in result . Split ( new [ ] { "\r \n " } , StringSplitOptions . None ) )
45
+ {
46
+ module . ReplaceLine ( startLine ++ , line ) ;
47
+ }
48
+ }
27
49
}
50
+ // ReSharper disable once EmptyGeneralCatchClause
51
+ catch { }
28
52
}
29
53
}
30
54
}
0 commit comments