12
12
13
13
namespace Rubberduck . Inspections . Abstract
14
14
{
15
- public abstract class ParseTreeInspectionBase : InspectionBase , IParseTreeInspection
15
+ public abstract class ParseTreeInspectionBase < TContext > : InspectionBase , IParseTreeInspection
16
+ where TContext : ParserRuleContext
16
17
{
17
18
protected ParseTreeInspectionBase ( IDeclarationFinderProvider declarationFinderProvider )
18
19
: base ( declarationFinderProvider )
19
20
{ }
20
21
21
- public abstract IInspectionListener Listener { get ; }
22
- protected abstract string ResultDescription ( QualifiedContext < ParserRuleContext > context ) ;
22
+ public IInspectionListener Listener => ContextListener ;
23
23
24
- protected virtual bool IsResultContext ( QualifiedContext < ParserRuleContext > context ) => true ;
24
+ protected abstract IInspectionListener < TContext > ContextListener { get ; }
25
+
26
+ protected abstract string ResultDescription ( QualifiedContext < TContext > context ) ;
27
+
28
+ protected virtual bool IsResultContext ( QualifiedContext < TContext > context ) => true ;
25
29
26
30
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( )
27
31
{
28
- return DoGetInspectionResults ( Listener . Contexts ( ) ) ;
32
+ return DoGetInspectionResults ( ContextListener . Contexts ( ) ) ;
29
33
}
30
34
31
35
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module )
32
36
{
33
- return DoGetInspectionResults ( Listener . Contexts ( module ) ) ;
37
+ return DoGetInspectionResults ( ContextListener . Contexts ( module ) ) ;
34
38
}
35
39
36
- private IEnumerable < IInspectionResult > DoGetInspectionResults ( IEnumerable < QualifiedContext < ParserRuleContext > > contexts )
40
+ private IEnumerable < IInspectionResult > DoGetInspectionResults ( IEnumerable < QualifiedContext < TContext > > contexts )
37
41
{
38
42
var objectionableContexts = contexts
39
43
. Where ( IsResultContext ) ;
@@ -43,7 +47,7 @@ private IEnumerable<IInspectionResult> DoGetInspectionResults(IEnumerable<Qualif
43
47
. ToList ( ) ;
44
48
}
45
49
46
- protected virtual IInspectionResult InspectionResult ( QualifiedContext < ParserRuleContext > context )
50
+ protected virtual IInspectionResult InspectionResult ( QualifiedContext < TContext > context )
47
51
{
48
52
return new QualifiedContextInspectionResult (
49
53
this ,
@@ -52,32 +56,35 @@ protected virtual IInspectionResult InspectionResult(QualifiedContext<ParserRule
52
56
DisabledQuickFixes ( context ) ) ;
53
57
}
54
58
55
- protected virtual ICollection < string > DisabledQuickFixes ( QualifiedContext < ParserRuleContext > context ) => new List < string > ( ) ;
59
+ protected virtual ICollection < string > DisabledQuickFixes ( QualifiedContext < TContext > context ) => new List < string > ( ) ;
56
60
public virtual CodeKind TargetKindOfCode => CodeKind . CodePaneCode ;
57
61
}
58
62
59
63
60
- public abstract class ParseTreeInspectionBase < T > : InspectionBase , IParseTreeInspection
64
+ public abstract class ParseTreeInspectionBase < TContext , TProperties > : InspectionBase , IParseTreeInspection
65
+ where TContext : ParserRuleContext
61
66
{
62
67
protected ParseTreeInspectionBase ( IDeclarationFinderProvider declarationFinderProvider )
63
68
: base ( declarationFinderProvider )
64
69
{ }
65
70
66
- public abstract IInspectionListener Listener { get ; }
67
- protected abstract string ResultDescription ( QualifiedContext < ParserRuleContext > context , T properties ) ;
68
- protected abstract ( bool isResult , T properties ) IsResultContextWithAdditionalProperties ( QualifiedContext < ParserRuleContext > context ) ;
71
+ public IInspectionListener Listener => ContextListener ;
72
+
73
+ protected abstract IInspectionListener < TContext > ContextListener { get ; }
74
+ protected abstract string ResultDescription ( QualifiedContext < TContext > context , TProperties properties ) ;
75
+ protected abstract ( bool isResult , TProperties properties ) IsResultContextWithAdditionalProperties ( QualifiedContext < TContext > context ) ;
69
76
70
77
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( )
71
78
{
72
- return DoGetInspectionResults ( Listener . Contexts ( ) ) ;
79
+ return DoGetInspectionResults ( ContextListener . Contexts ( ) ) ;
73
80
}
74
81
75
82
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module )
76
83
{
77
- return DoGetInspectionResults ( Listener . Contexts ( module ) ) ;
84
+ return DoGetInspectionResults ( ContextListener . Contexts ( module ) ) ;
78
85
}
79
86
80
- private IEnumerable < IInspectionResult > DoGetInspectionResults ( IEnumerable < QualifiedContext < ParserRuleContext > > contexts )
87
+ private IEnumerable < IInspectionResult > DoGetInspectionResults ( IEnumerable < QualifiedContext < TContext > > contexts )
81
88
{
82
89
var objectionableContexts = contexts
83
90
. Select ( ContextsWithResultProperties )
@@ -89,49 +96,50 @@ private IEnumerable<IInspectionResult> DoGetInspectionResults(IEnumerable<Qualif
89
96
. ToList ( ) ;
90
97
}
91
98
92
- private ( QualifiedContext < ParserRuleContext > context , T properties ) ? ContextsWithResultProperties ( QualifiedContext < ParserRuleContext > context )
99
+ private ( QualifiedContext < TContext > context , TProperties properties ) ? ContextsWithResultProperties ( QualifiedContext < TContext > context )
93
100
{
94
101
var ( isResult , properties ) = IsResultContextWithAdditionalProperties ( context ) ;
95
102
return isResult
96
103
? ( context , properties )
97
- : ( ( QualifiedContext < ParserRuleContext > context , T properties ) ? ) null ;
104
+ : ( ( QualifiedContext < TContext > context , TProperties properties ) ? ) null ;
98
105
}
99
106
100
- protected virtual IInspectionResult InspectionResult ( QualifiedContext < ParserRuleContext > context , T properties )
107
+ protected virtual IInspectionResult InspectionResult ( QualifiedContext < TContext > context , TProperties properties )
101
108
{
102
- return new QualifiedContextInspectionResult < T > (
109
+ return new QualifiedContextInspectionResult < TProperties > (
103
110
this ,
104
111
ResultDescription ( context , properties ) ,
105
112
context ,
106
113
properties ,
107
114
DisabledQuickFixes ( context , properties ) ) ;
108
115
}
109
116
110
- protected virtual ICollection < string > DisabledQuickFixes ( QualifiedContext < ParserRuleContext > context , T properties ) => new List < string > ( ) ;
117
+ protected virtual ICollection < string > DisabledQuickFixes ( QualifiedContext < TContext > context , TProperties properties ) => new List < string > ( ) ;
111
118
public virtual CodeKind TargetKindOfCode => CodeKind . CodePaneCode ;
112
119
}
113
120
114
- public class InspectionListenerBase : VBAParserBaseListener , IInspectionListener
121
+ public class InspectionListenerBase < TContext > : VBAParserBaseListener , IInspectionListener < TContext >
122
+ where TContext : ParserRuleContext
115
123
{
116
- private readonly IDictionary < QualifiedModuleName , List < QualifiedContext < ParserRuleContext > > > _contexts ;
124
+ private readonly IDictionary < QualifiedModuleName , List < QualifiedContext < TContext > > > _contexts ;
117
125
118
126
public InspectionListenerBase ( )
119
127
{
120
- _contexts = new Dictionary < QualifiedModuleName , List < QualifiedContext < ParserRuleContext > > > ( ) ;
128
+ _contexts = new Dictionary < QualifiedModuleName , List < QualifiedContext < TContext > > > ( ) ;
121
129
}
122
130
123
131
public QualifiedModuleName CurrentModuleName { get ; set ; }
124
132
125
- public IReadOnlyList < QualifiedContext < ParserRuleContext > > Contexts ( )
133
+ public IReadOnlyList < QualifiedContext < TContext > > Contexts ( )
126
134
{
127
135
return _contexts . AllValues ( ) . ToList ( ) ;
128
136
}
129
137
130
- public IReadOnlyList < QualifiedContext < ParserRuleContext > > Contexts ( QualifiedModuleName module )
138
+ public IReadOnlyList < QualifiedContext < TContext > > Contexts ( QualifiedModuleName module )
131
139
{
132
140
return _contexts . TryGetValue ( module , out var contexts )
133
141
? contexts
134
- : new List < QualifiedContext < ParserRuleContext > > ( ) ;
142
+ : new List < QualifiedContext < TContext > > ( ) ;
135
143
}
136
144
137
145
public virtual void ClearContexts ( )
@@ -144,17 +152,17 @@ public virtual void ClearContexts(QualifiedModuleName module)
144
152
_contexts . Remove ( module ) ;
145
153
}
146
154
147
- protected void SaveContext ( ParserRuleContext context )
155
+ protected void SaveContext ( TContext context )
148
156
{
149
157
var module = CurrentModuleName ;
150
- var qualifiedContext = new QualifiedContext < ParserRuleContext > ( module , context ) ;
158
+ var qualifiedContext = new QualifiedContext < TContext > ( module , context ) ;
151
159
if ( _contexts . TryGetValue ( module , out var contexts ) )
152
160
{
153
161
contexts . Add ( qualifiedContext ) ;
154
162
}
155
163
else
156
164
{
157
- _contexts . Add ( module , new List < QualifiedContext < ParserRuleContext > > { qualifiedContext } ) ;
165
+ _contexts . Add ( module , new List < QualifiedContext < TContext > > { qualifiedContext } ) ;
158
166
}
159
167
}
160
168
}
0 commit comments