1
1
using System . Collections . Generic ;
2
+ using System . Linq ;
2
3
using Antlr4 . Runtime ;
4
+ using Rubberduck . Common ;
5
+ using Rubberduck . Parsing ;
3
6
using Rubberduck . Parsing . Grammar ;
4
7
using Rubberduck . Parsing . Symbols ;
8
+ using Rubberduck . Parsing . VBA ;
5
9
using Rubberduck . VBEditor ;
6
10
7
11
namespace Rubberduck . Inspections
@@ -10,12 +14,12 @@ public class ParameterCanBeByValInspectionResult : InspectionResultBase
10
14
{
11
15
private readonly IEnumerable < CodeInspectionQuickFix > _quickFixes ;
12
16
13
- public ParameterCanBeByValInspectionResult ( IInspection inspection , Declaration target , ParserRuleContext context , QualifiedMemberName qualifiedName )
17
+ public ParameterCanBeByValInspectionResult ( IInspection inspection , RubberduckParserState state , Declaration target , ParserRuleContext context , QualifiedMemberName qualifiedName )
14
18
: base ( inspection , qualifiedName . QualifiedModuleName , context , target )
15
19
{
16
20
_quickFixes = new CodeInspectionQuickFix [ ]
17
21
{
18
- new PassParameterByValueQuickFix ( Context , QualifiedSelection ) ,
22
+ new PassParameterByValueQuickFix ( state , Target , Context , QualifiedSelection ) ,
19
23
new IgnoreOnceQuickFix ( Context , QualifiedSelection , inspection . AnnotationName )
20
24
} ;
21
25
}
@@ -30,21 +34,66 @@ public override string Description
30
34
31
35
public class PassParameterByValueQuickFix : CodeInspectionQuickFix
32
36
{
33
- public PassParameterByValueQuickFix ( ParserRuleContext context , QualifiedSelection selection )
37
+ private readonly RubberduckParserState _state ;
38
+ private readonly Declaration _target ;
39
+
40
+ public PassParameterByValueQuickFix ( RubberduckParserState state , Declaration target , ParserRuleContext context , QualifiedSelection selection )
34
41
: base ( context , selection , InspectionsUI . PassParameterByValueQuickFix )
35
42
{
43
+ _state = state ;
44
+ _target = target ;
36
45
}
37
46
38
47
public override void Fix ( )
39
48
{
40
- var selection = Selection . Selection ;
41
- var selectionLength = ( ( VBAParser . ArgContext ) Context ) . BYREF ( ) == null ? 0 : 6 ;
49
+ if ( ! _state . AllUserDeclarations . FindInterfaceMembers ( ) . Contains ( _target . ParentDeclaration ) )
50
+ {
51
+ FixMethod ( ( VBAParser . ArgContext ) Context , Selection ) ;
52
+ }
53
+ else
54
+ {
55
+ var declarationParameters =
56
+ _state . AllUserDeclarations . Where ( declaration => declaration . DeclarationType == DeclarationType . Parameter &&
57
+ declaration . ParentDeclaration == _target . ParentDeclaration )
58
+ . OrderBy ( o => o . Selection . StartLine )
59
+ . ThenBy ( t => t . Selection . StartColumn )
60
+ . ToList ( ) ;
61
+
62
+ var parameterIndex = declarationParameters . IndexOf ( _target ) ;
63
+
64
+ if ( parameterIndex == - 1 )
65
+ {
66
+ return ; // should only happen if the parse results are stale; prevents a crash in that case
67
+ }
68
+
69
+ var implementations = _state . AllUserDeclarations . FindInterfaceImplementationMembers ( _target . ParentDeclaration ) ;
70
+ foreach ( var member in implementations )
71
+ {
72
+ var parameters =
73
+ _state . AllUserDeclarations . Where ( declaration => declaration . DeclarationType == DeclarationType . Parameter &&
74
+ declaration . ParentDeclaration == member )
75
+ . OrderBy ( o => o . Selection . StartLine )
76
+ . ThenBy ( t => t . Selection . StartColumn )
77
+ . ToList ( ) ;
78
+
79
+ FixMethod ( ( VBAParser . ArgContext ) parameters [ parameterIndex ] . Context ,
80
+ parameters [ parameterIndex ] . QualifiedSelection ) ;
81
+ }
82
+
83
+ FixMethod ( ( VBAParser . ArgContext ) declarationParameters [ parameterIndex ] . Context ,
84
+ declarationParameters [ parameterIndex ] . QualifiedSelection ) ;
85
+ }
86
+ }
87
+
88
+ private void FixMethod ( VBAParser . ArgContext context , QualifiedSelection qualifiedSelection )
89
+ {
90
+ var selectionLength = context . BYREF ( ) == null ? 0 : 6 ;
42
91
43
- var module = Selection . QualifiedName . Component . CodeModule ;
44
- var lines = module . Lines [ selection . StartLine , 1 ] ;
92
+ var module = qualifiedSelection . QualifiedName . Component . CodeModule ;
93
+ var lines = module . Lines [ context . Start . Line , 1 ] ;
45
94
46
- var result = lines . Remove ( selection . StartColumn - 1 , selectionLength ) . Insert ( selection . StartColumn - 1 , Tokens . ByVal + ' ' ) ;
47
- module . ReplaceLine ( selection . StartLine , result ) ;
95
+ var result = lines . Remove ( context . Start . Column , selectionLength ) . Insert ( context . Start . Column , Tokens . ByVal + ' ' ) ;
96
+ module . ReplaceLine ( context . Start . Line , result ) ;
48
97
}
49
98
}
50
99
}
0 commit comments