-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNamespaceAsFolders.cs
99 lines (77 loc) · 3.98 KB
/
NamespaceAsFolders.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Linq;
using System.Collections.Immutable;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using _1313.Omnisharp.Extensions.Helpers;
namespace _1313.Omnisharp.Extensions
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class NamespaceAsFoldersAnalyzer : DiagnosticAnalyzer
{
internal const string Title = "Namespace should match folder structure";
internal const string MessageFormat = "Make namespace match folder hierarchy: '{0}'";
const string Description = "In order to improve project navigation, namespaces should match folder structure";
internal static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
"NAMESPACES01",
Title,
MessageFormat,
"NAMESPACES01",
DiagnosticSeverity.Info,
true,
description: Description
);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCompilationStartAction(c =>
{
var rootFolders = c.Compilation.SyntaxTrees.Where(x => !x.FilePath.Contains("AssemblyAttributes.cs"))
.Select(s => new
{
Path = Path.GetDirectoryName(s.FilePath),
NumParts = s.FilePath.Split(Path.AltDirectorySeparatorChar).Length
})
.Distinct()
.OrderBy(a => a.NumParts)
.Take(2);
string root = null;
if (rootFolders.Count() == 2 && rootFolders.First().NumParts == rootFolders.Last().NumParts)
{
root = string.Join(Path.AltDirectorySeparatorChar, rootFolders.First().Path.Split(Path.AltDirectorySeparatorChar).SkipLast(1));
}
else
{
root = rootFolders.FirstOrDefault()?.Path;
}
c.RegisterSyntaxNodeAction((syntaxTreeContext) =>
{
var semModel = syntaxTreeContext.SemanticModel;
var filePath = syntaxTreeContext.Node.SyntaxTree.FilePath;
if (filePath == null)
return;
var parentDirectory = Path.GetDirectoryName(filePath)
.Replace(root, "")
.Replace(Path.DirectorySeparatorChar, '.')
.Trim('.');
var assemblyName = syntaxTreeContext.Compilation.AssemblyName.SafeIdentifier();
var requiredNamespace = $"{assemblyName}.{parentDirectory}".Trim('.').SafeIdentifier();
var symbolInfo = semModel.GetDeclaredSymbol(syntaxTreeContext.Node) as INamespaceSymbol;
var name = symbolInfo.ToDisplayString();
if (requiredNamespace != name)
{
syntaxTreeContext.ReportDiagnostic(Diagnostic.Create(
Rule, (syntaxTreeContext.Node as NamespaceDeclarationSyntax).Name.GetLocation(), requiredNamespace));
}
}, SyntaxKind.NamespaceDeclaration);
});
}
}
}