Skip to content

Commit b47f460

Browse files
committed
Merge pull request #1316 from autoboosh/annotationssplitparser
Integrate annotations into parse tree
2 parents b0cea34 + 6a9bf51 commit b47f460

38 files changed

+4586
-3340
lines changed

RetailCoder.VBE/Inspections/IgnoreOnceQuickFix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void Fix()
3232
int commentStart;
3333
if (codeLine.HasComment(out commentStart) && codeLine.Substring(commentStart).StartsWith(ignoreAnnotation))
3434
{
35-
annotationText = codeLine + ' ' + _inspectionName;
35+
annotationText = codeLine + ", " + _inspectionName;
3636
codeModule.ReplaceLine(insertLine - 1, annotationText);
3737
}
3838
else

RetailCoder.VBE/Inspections/MultipleFolderAnnotationsInspection.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using Rubberduck.Parsing.Symbols;
44
using Rubberduck.Parsing.VBA;
5+
using Rubberduck.Parsing.Annotations;
56

67
namespace Rubberduck.Inspections
78
{
@@ -21,9 +22,7 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
2122
var issues = UserDeclarations.Where(declaration =>
2223
(declaration.DeclarationType == DeclarationType.Class
2324
|| declaration.DeclarationType == DeclarationType.Module)
24-
&& declaration.Annotations.Split('\n').Count(annotation =>
25-
annotation.StartsWith(Parsing.Grammar.Annotations.AnnotationMarker +
26-
Parsing.Grammar.Annotations.Folder)) > 1);
25+
&& declaration.Annotations.Count(annotation => annotation.AnnotationType == AnnotationType.Folder) > 1);
2726
return issues.Select(issue =>
2827
new MultipleFolderAnnotationsInspectionResult(this, issue));
2928
}

RetailCoder.VBE/Navigation/CodeExplorer/CodeExplorerComponentViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Rubberduck.Parsing.Symbols;
88
using Rubberduck.VBEditor;
99
using resx = Rubberduck.UI.CodeExplorer.CodeExplorer;
10+
using Rubberduck.Parsing.Annotations;
1011

1112
namespace Rubberduck.Navigation.CodeExplorer
1213
{
@@ -50,8 +51,8 @@ public bool IsTestModule
5051
{
5152
get
5253
{
53-
return _declaration.DeclarationType == DeclarationType.Module
54-
&& _declaration.Annotations.Split('\n').Contains(Parsing.Grammar.Annotations.TestModule);
54+
return _declaration.DeclarationType == DeclarationType.Module
55+
&& _declaration.Annotations.Any(annotation => annotation.AnnotationType == AnnotationType.TestModule);
5556
}
5657
}
5758

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Rubberduck.VBEditor;
2+
3+
namespace Rubberduck.Parsing.Annotations
4+
{
5+
public abstract class AnnotationBase : IAnnotation
6+
{
7+
private readonly AnnotationType _annotationType;
8+
private readonly QualifiedSelection _qualifiedSelection;
9+
10+
public const string ANNOTATION_MARKER = "'@";
11+
12+
public AnnotationBase(AnnotationType annotationType, QualifiedSelection qualifiedSelection)
13+
{
14+
_annotationType = annotationType;
15+
_qualifiedSelection = qualifiedSelection;
16+
}
17+
18+
public AnnotationType AnnotationType
19+
{
20+
get
21+
{
22+
return _annotationType;
23+
}
24+
}
25+
26+
public QualifiedSelection QualifiedSelection
27+
{
28+
get
29+
{
30+
return _qualifiedSelection;
31+
}
32+
}
33+
34+
public override string ToString()
35+
{
36+
return string.Format("Annotation Type: {0}", _annotationType);
37+
}
38+
}
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Antlr4.Runtime.Misc;
2+
using Rubberduck.Parsing.Grammar;
3+
using Rubberduck.VBEditor;
4+
using System.Collections.Generic;
5+
6+
namespace Rubberduck.Parsing.Annotations
7+
{
8+
public sealed class AnnotationListener : VBAParserBaseListener
9+
{
10+
private readonly List<IAnnotation> _annotations;
11+
private readonly IAnnotationFactory _factory;
12+
private readonly QualifiedModuleName _qualifiedName;
13+
14+
public AnnotationListener(IAnnotationFactory factory, QualifiedModuleName qualifiedName)
15+
{
16+
_annotations = new List<IAnnotation>();
17+
_factory = factory;
18+
_qualifiedName = qualifiedName;
19+
}
20+
21+
public IEnumerable<IAnnotation> Annotations
22+
{
23+
get
24+
{
25+
return _annotations;
26+
}
27+
}
28+
29+
public override void ExitAnnotation([NotNull] VBAParser.AnnotationContext context)
30+
{
31+
var newAnnotation = _factory.Create(context, new QualifiedSelection(_qualifiedName, context.GetSelection()));
32+
_annotations.Add(newAnnotation);
33+
}
34+
}
35+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Rubberduck.Parsing.Annotations
2+
{
3+
public enum AnnotationType
4+
{
5+
TestModule,
6+
ModuleInitialize,
7+
ModuleCleanup,
8+
TestMethod,
9+
TestInitialize,
10+
TestCleanup,
11+
Ignore,
12+
Folder
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Rubberduck.VBEditor;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Rubberduck.Parsing.Annotations
6+
{
7+
public sealed class FolderAnnotation : AnnotationBase
8+
{
9+
private readonly string _folderName;
10+
11+
public FolderAnnotation(
12+
QualifiedSelection qualifiedSelection,
13+
IEnumerable<string> parameters)
14+
: base(AnnotationType.Folder, qualifiedSelection)
15+
{
16+
if (parameters.Count() != 1)
17+
{
18+
throw new InvalidAnnotationArgumentException(string.Format("{0} expects exactly one argument, the folder, but none or more than one were passed.", this.GetType().Name));
19+
}
20+
_folderName = parameters.First();
21+
}
22+
23+
public string FolderName
24+
{
25+
get
26+
{
27+
return _folderName;
28+
}
29+
}
30+
31+
public override string ToString()
32+
{
33+
return string.Format("Folder: {0}", _folderName);
34+
}
35+
}
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Rubberduck.Parsing.Grammar;
2+
using Rubberduck.VBEditor;
3+
4+
namespace Rubberduck.Parsing.Annotations
5+
{
6+
public interface IAnnotation
7+
{
8+
AnnotationType AnnotationType { get; }
9+
QualifiedSelection QualifiedSelection { get; }
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Rubberduck.Parsing.Grammar;
2+
using Rubberduck.VBEditor;
3+
4+
namespace Rubberduck.Parsing.Annotations
5+
{
6+
public interface IAnnotationFactory
7+
{
8+
IAnnotation Create(VBAParser.AnnotationContext context, QualifiedSelection qualifiedSelection);
9+
}
10+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Rubberduck.Parsing.Grammar;
2+
using Rubberduck.VBEditor;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace Rubberduck.Parsing.Annotations
7+
{
8+
public sealed class IgnoreAnnotation : AnnotationBase
9+
{
10+
private readonly IEnumerable<string> _inspectionNames;
11+
12+
public IgnoreAnnotation(
13+
QualifiedSelection qualifiedSelection,
14+
IEnumerable<string> parameters)
15+
: base(AnnotationType.Ignore, qualifiedSelection)
16+
{
17+
if (!parameters.Any())
18+
{
19+
throw new InvalidAnnotationArgumentException(string.Format("{0} expects at least one argument but none were given.", this.GetType().Name));
20+
}
21+
_inspectionNames = parameters.ToList();
22+
}
23+
24+
public IEnumerable<string> InspectionNames
25+
{
26+
get
27+
{
28+
return _inspectionNames;
29+
}
30+
}
31+
32+
public bool IsIgnored(string inspectionName)
33+
{
34+
return _inspectionNames.Contains(inspectionName);
35+
}
36+
37+
public override string ToString()
38+
{
39+
return string.Format("Ignored inspections: {0}", string.Join(", ", _inspectionNames));
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)