1
+ using System ;
1
2
using System . Collections . Generic ;
3
+ using System . Linq ;
2
4
using Antlr4 . Runtime ;
5
+ using Rubberduck . Parsing . Grammar ;
3
6
using Rubberduck . Parsing . Symbols ;
4
7
using Rubberduck . VBEditor ;
5
8
@@ -16,7 +19,7 @@ public UntypedFunctionUsageInspectionResult(IInspection inspection, IdentifierRe
16
19
_reference = reference ;
17
20
_quickFixes = new CodeInspectionQuickFix [ ]
18
21
{
19
- new UntypedFunctionUsageQuickFix ( Context , QualifiedSelection ) ,
22
+ new UntypedFunctionUsageQuickFix ( ( ParserRuleContext ) GetFirst ( typeof ( VBAParser . IdentifierContext ) ) . Parent , QualifiedSelection ) ,
20
23
new IgnoreOnceQuickFix ( Context , QualifiedSelection , Inspection . AnnotationName ) ,
21
24
} ;
22
25
}
@@ -27,29 +30,53 @@ public override string Description
27
30
{
28
31
get { return string . Format ( Inspection . Description , _reference . Declaration . IdentifierName ) ; }
29
32
}
33
+
34
+ private ParserRuleContext GetFirst ( Type nodeType )
35
+ {
36
+ var unexploredNodes = new List < ParserRuleContext > { Context } ;
37
+
38
+ while ( unexploredNodes . Any ( ) )
39
+ {
40
+ if ( unexploredNodes [ 0 ] . GetType ( ) == nodeType )
41
+ {
42
+ return unexploredNodes [ 0 ] ;
43
+ }
44
+
45
+ unexploredNodes . AddRange ( unexploredNodes [ 0 ] . children . OfType < ParserRuleContext > ( ) ) ;
46
+ unexploredNodes . RemoveAt ( 0 ) ;
47
+ }
48
+
49
+ return null ;
50
+ }
30
51
}
31
52
32
53
public class UntypedFunctionUsageQuickFix : CodeInspectionQuickFix
33
54
{
34
55
public UntypedFunctionUsageQuickFix ( ParserRuleContext context , QualifiedSelection selection )
35
- : base ( context , selection ,
36
- string . Format ( InspectionsUI . QuickFixUseTypedFunction_ ,
37
- context . GetText ( ) , context . GetText ( ) . Insert ( context . GetText ( ) . IndexOf ( '(' ) , "$" ) ) )
56
+ : base ( context , selection , string . Format ( InspectionsUI . QuickFixUseTypedFunction_ , context . GetText ( ) , GetNewSignature ( context ) ) )
38
57
{
39
58
}
40
59
41
60
public override void Fix ( )
42
61
{
43
62
var originalInstruction = Context . GetText ( ) ;
44
- var newInstruction = originalInstruction . Insert ( originalInstruction . IndexOf ( '(' ) , "$" ) ;
63
+ var newInstruction = GetNewSignature ( Context ) ;
45
64
var selection = Selection . Selection ;
46
65
47
66
var module = Selection . QualifiedName . Component . CodeModule ;
48
67
var lines = module . Lines [ selection . StartLine , selection . LineCount ] ;
49
68
50
69
var result = lines . Replace ( originalInstruction , newInstruction ) ;
51
70
module . ReplaceLine ( selection . StartLine , result ) ;
52
- // FIXME trigger reparse
71
+ }
72
+
73
+ private static string GetNewSignature ( ParserRuleContext context )
74
+ {
75
+ return context . children . Aggregate ( string . Empty , ( current , member ) =>
76
+ {
77
+ var isIdentifierNode = member is VBAParser . IdentifierContext ;
78
+ return current + member . GetText ( ) + ( isIdentifierNode ? "$" : string . Empty ) ;
79
+ } ) ;
53
80
}
54
81
}
55
82
}
0 commit comments