4
4
using Antlr4 . Runtime . Misc ;
5
5
using Antlr4 . Runtime . Tree ;
6
6
using Rubberduck . Inspections . Abstract ;
7
+ using Rubberduck . Parsing ;
7
8
using Rubberduck . Parsing . Annotations ;
8
9
using Rubberduck . Parsing . Grammar ;
9
10
using Rubberduck . Parsing . Inspections ;
@@ -18,11 +19,13 @@ namespace Rubberduck.Inspections.QuickFixes
18
19
public sealed class IgnoreOnceQuickFix : QuickFixBase
19
20
{
20
21
private readonly RubberduckParserState _state ;
22
+ private readonly IAnnotationUpdater _annotationUpdater ;
21
23
22
- public IgnoreOnceQuickFix ( RubberduckParserState state , IEnumerable < IInspection > inspections )
24
+ public IgnoreOnceQuickFix ( IAnnotationUpdater annotationUpdater , RubberduckParserState state , IEnumerable < IInspection > inspections )
23
25
: base ( inspections . Select ( s => s . GetType ( ) ) . Where ( i => i . CustomAttributes . All ( a => a . AttributeType != typeof ( CannotAnnotateAttribute ) ) ) . ToArray ( ) )
24
26
{
25
27
_state = state ;
28
+ _annotationUpdater = annotationUpdater ;
26
29
}
27
30
28
31
public override bool CanFixInProcedure => false ;
@@ -43,102 +46,47 @@ public override void Fix(IInspectionResult result, IRewriteSession rewriteSessio
43
46
44
47
private void FixNonModule ( IInspectionResult result , IRewriteSession rewriteSession )
45
48
{
46
- int insertionIndex ;
47
- string insertText ;
48
- var annotationText = $ "'@Ignore { result . Inspection . AnnotationName } ";
49
-
50
49
var module = result . QualifiedSelection . QualifiedName ;
51
- var parseTree = _state . GetParseTree ( module , CodeKind . CodePaneCode ) ;
52
- var eolListener = new EndOfLineListener ( ) ;
53
- ParseTreeWalker . Default . Walk ( eolListener , parseTree ) ;
54
- var previousEol = eolListener . Contexts
55
- . OrderBy ( eol => eol . Start . TokenIndex )
56
- . LastOrDefault ( eol => eol . Start . Line < result . QualifiedSelection . Selection . StartLine ) ;
57
-
58
- var rewriter = rewriteSession . CheckOutModuleRewriter ( module ) ;
59
-
60
- if ( previousEol == null )
61
- {
62
- // The context to get annotated is on the first line; we need to insert before token index 0.
63
- insertionIndex = 0 ;
64
- insertText = annotationText + Environment . NewLine ;
65
- rewriter . InsertBefore ( insertionIndex , insertText ) ;
66
- return ;
67
- }
50
+ var lineToAnnotate = result . QualifiedSelection . Selection . StartLine ;
51
+ var existingIgnoreAnnotation = _state . DeclarationFinder . FindAnnotations ( module , lineToAnnotate )
52
+ . OfType < IgnoreAnnotation > ( )
53
+ . FirstOrDefault ( ) ;
68
54
69
- var commentContext = previousEol . commentOrAnnotation ( ) ;
70
- if ( commentContext = = null )
55
+ var annotationType = AnnotationType . Ignore ;
56
+ if ( existingIgnoreAnnotation ! = null )
71
57
{
72
- insertionIndex = previousEol . Start . TokenIndex ;
73
- var indent = WhitespaceAfter ( previousEol ) ;
74
- insertText = $ "{ Environment . NewLine } { indent } { annotationText } ";
75
- rewriter . InsertBefore ( insertionIndex , insertText ) ;
76
- return ;
58
+ var annotationValues = existingIgnoreAnnotation . InspectionNames . ToList ( ) ;
59
+ annotationValues . Insert ( 0 , result . Inspection . AnnotationName ) ;
60
+ _annotationUpdater . UpdateAnnotation ( rewriteSession , existingIgnoreAnnotation , annotationType , annotationValues ) ;
77
61
}
78
-
79
- var ignoreAnnotation = commentContext . annotationList ( ) ? . annotation ( )
80
- . FirstOrDefault ( annotationContext => annotationContext . annotationName ( ) . GetText ( ) == AnnotationType . Ignore . ToString ( ) ) ;
81
- if ( ignoreAnnotation == null )
62
+ else
82
63
{
83
- insertionIndex = commentContext . Stop . TokenIndex ;
84
- var indent = WhitespaceAfter ( previousEol ) ;
85
- insertText = $ "{ indent } { annotationText } { Environment . NewLine } ";
86
- rewriter . InsertAfter ( insertionIndex , insertText ) ;
87
- return ;
64
+ var annotationValues = new List < string > { result . Inspection . AnnotationName } ;
65
+ _annotationUpdater . AddAnnotation ( rewriteSession , new QualifiedContext ( module , result . Context ) , annotationType , annotationValues ) ;
88
66
}
89
-
90
- insertionIndex = ignoreAnnotation . annotationName ( ) . Stop . TokenIndex ;
91
- insertText = $ " { result . Inspection . AnnotationName } ,";
92
- rewriter . InsertAfter ( insertionIndex , insertText ) ;
93
- }
94
-
95
- private static string WhitespaceAfter ( VBAParser . EndOfLineContext endOfLine )
96
- {
97
- var individualEndOfStatement = ( VBAParser . IndividualNonEOFEndOfStatementContext ) endOfLine . Parent ;
98
- var whiteSpaceOnNextLine = individualEndOfStatement . whiteSpace ( 0 ) ;
99
- return whiteSpaceOnNextLine != null
100
- ? whiteSpaceOnNextLine . GetText ( )
101
- : string . Empty ;
102
67
}
103
68
104
69
private void FixModule ( IInspectionResult result , IRewriteSession rewriteSession )
105
70
{
106
- var module = result . QualifiedSelection . QualifiedName ;
107
- var moduleAnnotations = _state . GetModuleAnnotations ( module ) ;
108
- var firstIgnoreModuleAnnotation = moduleAnnotations
109
- . Where ( annotation => annotation . AnnotationType == AnnotationType . IgnoreModule )
110
- . OrderBy ( annotation => annotation . Context . Start . TokenIndex )
71
+ var moduleDeclaration = result . Target ;
72
+ var existingIgnoreModuleAnnotation = moduleDeclaration . Annotations
73
+ . OfType < IgnoreModuleAnnotation > ( )
111
74
. FirstOrDefault ( ) ;
112
75
113
- var rewriter = rewriteSession . CheckOutModuleRewriter ( module ) ;
114
-
115
- int insertionIndex ;
116
- string insertText ;
117
-
118
- if ( firstIgnoreModuleAnnotation == null )
76
+ var annotationType = AnnotationType . IgnoreModule ;
77
+ if ( existingIgnoreModuleAnnotation != null )
119
78
{
120
- insertionIndex = 0 ;
121
- insertText = $ "'@IgnoreModule { result . Inspection . AnnotationName } { Environment . NewLine } ";
122
- rewriter . InsertBefore ( insertionIndex , insertText ) ;
123
- return ;
79
+ var annotationValues = existingIgnoreModuleAnnotation . InspectionNames . ToList ( ) ;
80
+ annotationValues . Insert ( 0 , result . Inspection . AnnotationName ) ;
81
+ _annotationUpdater . UpdateAnnotation ( rewriteSession , existingIgnoreModuleAnnotation , annotationType , annotationValues ) ;
124
82
}
125
-
126
- insertionIndex = firstIgnoreModuleAnnotation . Context . annotationName ( ) . Stop . TokenIndex ;
127
- insertText = $ " { result . Inspection . AnnotationName } ,";
128
- rewriter . InsertAfter ( insertionIndex , insertText ) ;
129
- }
130
-
131
- public override string Description ( IInspectionResult result ) => Resources . Inspections . QuickFixes . IgnoreOnce ;
132
-
133
- private class EndOfLineListener : VBAParserBaseListener
134
- {
135
- private readonly IList < VBAParser . EndOfLineContext > _contexts = new List < VBAParser . EndOfLineContext > ( ) ;
136
- public IEnumerable < VBAParser . EndOfLineContext > Contexts => _contexts ;
137
-
138
- public override void ExitEndOfLine ( [ NotNull ] VBAParser . EndOfLineContext context )
83
+ else
139
84
{
140
- _contexts . Add ( context ) ;
85
+ var annotationValues = new List < string > { result . Inspection . AnnotationName } ;
86
+ _annotationUpdater . AddAnnotation ( rewriteSession , moduleDeclaration , annotationType , annotationValues ) ;
141
87
}
142
88
}
89
+
90
+ public override string Description ( IInspectionResult result ) => Resources . Inspections . QuickFixes . IgnoreOnce ;
143
91
}
144
92
}
0 commit comments