-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMen005FileTooLong.cs
63 lines (45 loc) · 1.89 KB
/
Men005FileTooLong.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
namespace Menees.Analyzers;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class Men005FileTooLong : Analyzer
{
#region Public Constants
public const string DiagnosticId = "MEN005";
#endregion
#region Private Data Members
private static readonly LocalizableString Title =
new LocalizableResourceString(nameof(Resources.Men005Title), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat =
new LocalizableResourceString(nameof(Resources.Men005MessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description =
new LocalizableResourceString(nameof(Resources.Men005Description), Resources.ResourceManager, typeof(Resources));
private static readonly DiagnosticDescriptor Rule =
new(DiagnosticId, Title, MessageFormat, Rules.Layout, DiagnosticSeverity.Warning, Rules.EnabledByDefault, Description);
#endregion
#region Public Properties
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
#endregion
#region Public Methods
public override void Initialize(AnalysisContext context)
{
base.Initialize(context);
context.RegisterSyntaxTreeActionHonorExclusions(this, this.HandleSyntaxTree);
}
#endregion
#region Private Methods
private void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
{
SyntaxTree tree = context.Tree;
if (tree.TryGetText(out SourceText? text))
{
int maxLength = this.Settings.MaxFileLines;
int fileLength = text.Lines.Count;
if (fileLength > maxLength)
{
// Only mark the first line past the limit. Marking every line from maxLength to fileLength-1 was ugly.
var fileLocation = Rules.GetFileLocation(tree, text, maxLength, maxLength);
context.ReportDiagnostic(Diagnostic.Create(Rule, fileLocation.Item2, fileLocation.Item1, maxLength, fileLength));
}
}
}
#endregion
}