11using System . Collections . Generic ;
2+ using System . Linq ;
23using Antlr4 . Runtime ;
4+ using Rubberduck . Common ;
5+ using Rubberduck . Parsing ;
36using Rubberduck . Parsing . Grammar ;
47using Rubberduck . Parsing . Symbols ;
8+ using Rubberduck . Parsing . VBA ;
59using Rubberduck . VBEditor ;
610
711namespace Rubberduck . Inspections
@@ -10,12 +14,12 @@ public class ParameterCanBeByValInspectionResult : InspectionResultBase
1014 {
1115 private readonly IEnumerable < CodeInspectionQuickFix > _quickFixes ;
1216
13- public ParameterCanBeByValInspectionResult ( IInspection inspection , Declaration target , ParserRuleContext context , QualifiedMemberName qualifiedName )
17+ public ParameterCanBeByValInspectionResult ( IInspection inspection , RubberduckParserState state , Declaration target , ParserRuleContext context , QualifiedMemberName qualifiedName )
1418 : base ( inspection , qualifiedName . QualifiedModuleName , context , target )
1519 {
1620 _quickFixes = new CodeInspectionQuickFix [ ]
1721 {
18- new PassParameterByValueQuickFix ( Context , QualifiedSelection ) ,
22+ new PassParameterByValueQuickFix ( state , Target , Context , QualifiedSelection ) ,
1923 new IgnoreOnceQuickFix ( Context , QualifiedSelection , inspection . AnnotationName )
2024 } ;
2125 }
@@ -30,21 +34,66 @@ public override string Description
3034
3135 public class PassParameterByValueQuickFix : CodeInspectionQuickFix
3236 {
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 )
3441 : base ( context , selection , InspectionsUI . PassParameterByValueQuickFix )
3542 {
43+ _state = state ;
44+ _target = target ;
3645 }
3746
3847 public override void Fix ( )
3948 {
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 ;
4291
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 ] ;
4594
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 ) ;
4897 }
4998 }
5099}
0 commit comments