11using System.Collections.Generic;
16 internal static class ContainerDefinitionsExtractor
25 public static IReadOnlyList<ExtractedContainerDefinition> GetContainerDefinitions(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
27 NamespaceDeclarationSyntax namespaceDeclaration;
28 TypeDeclarationSyntax containingTypeDeclaration;
29 List<ExtractedContainerDefinition> containers =
new List<ExtractedContainerDefinition>();
32 containingTypeDeclaration = targetTypeDeclaration;
33 while (containingTypeDeclaration.Parent is TypeDeclarationSyntax)
35 containingTypeDeclaration = (TypeDeclarationSyntax)containingTypeDeclaration.Parent;
36 containers.Add(GetContainerDefinition(extractionContext, containingTypeDeclaration));
39 namespaceDeclaration = containingTypeDeclaration.Parent as NamespaceDeclarationSyntax;
40 if (namespaceDeclaration is not
null)
42 containers.Add(GetContainerDefinition(extractionContext, namespaceDeclaration));
50 private static ExtractedContainerDefinition GetContainerDefinition(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax typeDeclarationSyntax)
52 StringBuilder containerDefinitionBuilder =
new StringBuilder();
53 ExtractedContainerDefinition containerDefinition;
55 foreach (SyntaxToken modifier
in typeDeclarationSyntax.Modifiers)
57 containerDefinitionBuilder.Append(modifier.ToString());
58 containerDefinitionBuilder.Append(
" ");
61 containerDefinitionBuilder.Append(typeDeclarationSyntax.Keyword.ToString());
62 containerDefinitionBuilder.Append(
" ");
63 containerDefinitionBuilder.Append(typeDeclarationSyntax.Identifier.ToString());
65 containerDefinition =
new ExtractedContainerDefinition()
67 Name = typeDeclarationSyntax.Identifier.ToString(),
68 FullDefinition = containerDefinitionBuilder.ToString()
72 return containerDefinition;
75 private static ExtractedContainerDefinition GetContainerDefinition(DefinitionExtractionContext extractionContext, NamespaceDeclarationSyntax namespaceDeclarationSyntax)
77 StringBuilder containerDefinitionBuilder =
new StringBuilder();
78 ExtractedContainerDefinition containerDefinition;
80 foreach (SyntaxToken modifier
in namespaceDeclarationSyntax.Modifiers)
82 containerDefinitionBuilder.Append(modifier.ToString());
83 containerDefinitionBuilder.Append(
" ");
86 containerDefinitionBuilder.Append(
"namespace ");
87 containerDefinitionBuilder.Append(namespaceDeclarationSyntax.Name.ToString());
89 containerDefinition =
new ExtractedContainerDefinition()
91 Name = namespaceDeclarationSyntax.Name.ToString(),
92 FullDefinition = containerDefinitionBuilder.ToString()
96 return containerDefinition;