5
5
using Rubberduck . Parsing . Grammar ;
6
6
using Rubberduck . Parsing . Symbols ;
7
7
using Rubberduck . VBEditor ;
8
- using static Rubberduck . Parsing . Grammar . VBAParser ;
8
+ using System . Linq ;
9
9
10
10
namespace Rubberduck . Inspections . QuickFixes
11
11
{
@@ -24,80 +24,34 @@ public PassParameterByReferenceQuickFix(Declaration target, QualifiedSelection s
24
24
25
25
public override void Fix ( )
26
26
{
27
- var argCtxt = GetArgContextForIdentifier ( Context , _target . IdentifierName ) ;
27
+ var argCtxt = GetArgContextForIdentifier ( Context . Parent . Parent , _target . IdentifierName ) ;
28
28
29
- var terminalNodeImpl = GetByValNodeForArgCtx ( argCtxt ) ;
29
+ var terminalNode = argCtxt . BYVAL ( ) ;
30
30
31
- var replacementLine = GenerateByRefReplacementLine ( terminalNodeImpl ) ;
31
+ var replacementLine = GenerateByRefReplacementLine ( terminalNode ) ;
32
32
33
- ReplaceModuleLine ( terminalNodeImpl . Symbol . Line , replacementLine ) ;
33
+ ReplaceModuleLine ( terminalNode . Symbol . Line , replacementLine ) ;
34
34
35
35
}
36
- private ArgContext GetArgContextForIdentifier ( ParserRuleContext context , string identifier )
36
+ private VBAParser . ArgContext GetArgContextForIdentifier ( RuleContext context , string identifier )
37
37
{
38
- var procStmtCtx = ( ParserRuleContext ) context . Parent . Parent ;
39
- var procStmtCtxChildren = procStmtCtx . children ;
40
- for ( int idx = 0 ; idx < procStmtCtxChildren . Count ; idx ++ )
41
- {
42
- if ( procStmtCtxChildren [ idx ] is ArgListContext )
43
- {
44
- var argListContext = ( ArgListContext ) procStmtCtxChildren [ idx ] ;
45
- var arg = argListContext . children ;
46
- for ( int idxArgListCtx = 0 ; idxArgListCtx < arg . Count ; idxArgListCtx ++ )
47
- {
48
- if ( arg [ idxArgListCtx ] is ArgContext )
49
- {
50
- var name = GetIdentifierNameFor ( ( ArgContext ) arg [ idxArgListCtx ] ) ;
51
- if ( name . Equals ( identifier ) )
52
- {
53
- return ( ArgContext ) arg [ idxArgListCtx ] ;
54
- }
55
- }
56
- }
57
- }
58
- }
59
- return null ;
60
- }
61
- private string GetIdentifierNameFor ( ArgContext argCtxt )
62
- {
63
- var argCtxtChild = argCtxt . children ;
64
- var idRef = GetUnRestrictedIdentifierCtx ( argCtxt ) ;
65
- return idRef . GetText ( ) ;
38
+ var argList = GetArgListForContext ( context ) ;
39
+ return argList . arg ( ) . SingleOrDefault ( parameter =>
40
+ Identifier . GetName ( parameter ) . Equals ( identifier )
41
+ || Identifier . GetName ( parameter ) . Equals ( "[" + identifier + "]" ) ) ;
66
42
}
67
- private UnrestrictedIdentifierContext GetUnRestrictedIdentifierCtx ( ArgContext argCtxt )
43
+ private string GenerateByRefReplacementLine ( ITerminalNode terminalNode )
68
44
{
69
- var argCtxtChild = argCtxt . children ;
70
- for ( int idx = 0 ; idx < argCtxtChild . Count ; idx ++ )
71
- {
72
- if ( argCtxtChild [ idx ] is UnrestrictedIdentifierContext )
73
- {
74
- return ( UnrestrictedIdentifierContext ) argCtxtChild [ idx ] ;
75
- }
76
- }
77
- return null ;
78
- }
79
- private TerminalNodeImpl GetByValNodeForArgCtx ( ArgContext argCtxt )
80
- {
81
- var argCtxtChild = argCtxt . children ;
82
- for ( int idx = 0 ; idx < argCtxtChild . Count ; idx ++ )
83
- {
84
- if ( argCtxtChild [ idx ] is TerminalNodeImpl )
85
- {
86
- var candidate = ( TerminalNodeImpl ) argCtxtChild [ idx ] ;
87
- if ( candidate . Symbol . Text . Equals ( Tokens . ByVal ) )
88
- {
89
- return candidate ;
90
- }
91
- }
92
- }
93
- return null ;
45
+ var module = Selection . QualifiedName . Component . CodeModule ;
46
+ var byValTokenLine = module . GetLines ( terminalNode . Symbol . Line , 1 ) ;
47
+
48
+ return ReplaceAtIndex ( byValTokenLine , Tokens . ByVal , Tokens . ByRef , terminalNode . Symbol . Column ) ;
94
49
}
95
- private string GenerateByRefReplacementLine ( TerminalNodeImpl terminalNodeImpl )
50
+ private void ReplaceModuleLine ( int lineNumber , string replacementLine )
96
51
{
97
52
var module = Selection . QualifiedName . Component . CodeModule ;
98
- var byValTokenLine = module . GetLines ( terminalNodeImpl . Symbol . Line , 1 ) ;
99
-
100
- return ReplaceAtIndex ( byValTokenLine , Tokens . ByVal , Tokens . ByRef , terminalNodeImpl . Symbol . Column ) ;
53
+ module . DeleteLines ( lineNumber ) ;
54
+ module . InsertLines ( lineNumber , replacementLine ) ;
101
55
}
102
56
private string ReplaceAtIndex ( string input , string toReplace , string replacement , int startIndex )
103
57
{
@@ -107,11 +61,29 @@ private string ReplaceAtIndex(string input, string toReplace, string replacement
107
61
var tokenToBeReplaced = input . Substring ( startIndex , stopIndex - startIndex + 1 ) ;
108
62
return prefix + tokenToBeReplaced . Replace ( toReplace , replacement ) + suffix ;
109
63
}
110
- private void ReplaceModuleLine ( int lineNumber , string replacementLine )
64
+ private VBAParser . ArgListContext GetArgListForContext ( RuleContext context )
111
65
{
112
- var module = Selection . QualifiedName . Component . CodeModule ;
113
- module . DeleteLines ( lineNumber ) ;
114
- module . InsertLines ( lineNumber , replacementLine ) ;
66
+ if ( context is VBAParser . SubStmtContext )
67
+ {
68
+ return ( ( VBAParser . SubStmtContext ) context ) . argList ( ) ;
69
+ }
70
+ else if ( context is VBAParser . FunctionStmtContext )
71
+ {
72
+ return ( ( VBAParser . FunctionStmtContext ) context ) . argList ( ) ;
73
+ }
74
+ else if ( context is VBAParser . PropertyLetStmtContext )
75
+ {
76
+ return ( ( VBAParser . PropertyLetStmtContext ) context ) . argList ( ) ;
77
+ }
78
+ else if ( context is VBAParser . PropertyGetStmtContext )
79
+ {
80
+ return ( ( VBAParser . PropertyGetStmtContext ) context ) . argList ( ) ;
81
+ }
82
+ else if ( context is VBAParser . PropertySetStmtContext )
83
+ {
84
+ return ( ( VBAParser . PropertySetStmtContext ) context ) . argList ( ) ;
85
+ }
86
+ return null ;
115
87
}
116
88
}
117
89
}
0 commit comments