44using Microsoft . CodeAnalysis . CSharp ;
55using System . Collections . Generic ;
66using Strazh . Domain ;
7+ using System . IO ;
78
89namespace Strazh . Analysis
910{
1011 public static class Extractor
1112 {
12- private static CodeNode CreateNode ( this ISymbol symbol , TypeDeclarationSyntax declaration )
13+ private static TypeNode CreateTypeNode ( this ISymbol symbol , TypeDeclarationSyntax declaration )
1314 {
1415 ( string fullName , string name ) = ( symbol . ContainingNamespace . ToString ( ) + '.' + symbol . Name , symbol . Name ) ;
1516 switch ( declaration )
@@ -31,7 +32,7 @@ private static InterfaceNode CreateInterfaceNode(this TypeInfo typeInfo)
3132 private static string [ ] MapModifiers ( this SyntaxTokenList syntaxTokens )
3233 => syntaxTokens . Select ( x => x . ValueText ) . ToArray ( ) ;
3334
34- private static Node CreateNode ( this TypeInfo typeInfo )
35+ private static TypeNode CreateTypeNode ( this TypeInfo typeInfo )
3536 {
3637 switch ( typeInfo . ConvertedType . TypeKind )
3738 {
@@ -68,20 +69,20 @@ private static MethodNode CreateMethodNode(this IMethodSymbol symbol, MethodDecl
6869 var fullName = symbol . ContainingNamespace . GetNamespaceName ( $ "{ symbol . ContainingType . Name } .{ symbol . Name } ") ;
6970 var args = symbol . Parameters . Select ( x => ( name : x . Name , type : x . Type . ToString ( ) ) ) . ToArray ( ) ;
7071 var returnType = symbol . ReturnType . ToString ( ) ;
71- return new MethodNode ( fullName ,
72+ return new MethodNode ( fullName ,
7273 symbol . Name ,
7374 args ,
7475 returnType ,
7576 declaration ? . Modifiers . MapModifiers ( ) ) ;
7677 }
7778
7879 private static string GetName ( string filePath )
79- => filePath . Split ( " \\ " ) . Reverse ( ) . FirstOrDefault ( ) ;
80+ => filePath . Split ( Path . DirectorySeparatorChar ) . Reverse ( ) . FirstOrDefault ( ) ;
8081
8182 private static List < TripleIncludedIn > GetFolderChain ( string filePath , FileNode file )
8283 {
8384 var triples = new List < TripleIncludedIn > ( ) ;
84- var chain = filePath . Split ( " \\ " ) ;
85+ var chain = filePath . Split ( Path . DirectorySeparatorChar ) ;
8586 FolderNode prev = null ;
8687 var path = string . Empty ;
8788 foreach ( var item in chain )
@@ -99,7 +100,7 @@ private static List<TripleIncludedIn> GetFolderChain(string filePath, FileNode f
99100 }
100101 else
101102 {
102- path = $ "{ path } \\ { item } ";
103+ path = Path . DirectorySeparatorChar == '/' ? $ " { path } / { item } " : $ "{ path } \\ { item } ";
103104 triples . Add ( new TripleIncludedIn ( new FolderNode ( path , item ) , new FolderNode ( prev . FullName , prev . Name ) ) ) ;
104105 prev = new FolderNode ( path , item ) ;
105106 }
@@ -116,14 +117,14 @@ public static void AnalyzeTree<T>(IList<Triple> triples, SyntaxTree st, Semantic
116117 var root = st . GetRoot ( ) ;
117118 var filePath = root . SyntaxTree . FilePath ;
118119 var index = filePath . IndexOf ( rootFolder . Name ) ;
119- filePath = index < 0 ? filePath : filePath . Substring ( index ) ;
120+ filePath = index < 0 ? filePath : filePath [ index .. ] ;
120121 var fileName = GetName ( filePath ) ;
121122 var fileNode = new FileNode ( filePath , fileName ) ;
122123 GetFolderChain ( filePath , fileNode ) . ForEach ( triples . Add ) ;
123124 var declarations = root . DescendantNodes ( ) . OfType < T > ( ) ;
124125 foreach ( var declaration in declarations )
125126 {
126- var node = sem . GetDeclaredSymbol ( declaration ) . CreateNode ( declaration ) ;
127+ var node = sem . GetDeclaredSymbol ( declaration ) . CreateTypeNode ( declaration ) ;
127128 if ( node != null )
128129 {
129130 triples . Add ( new TripleDeclaredAt ( node , fileNode ) ) ;
@@ -136,29 +137,33 @@ public static void AnalyzeTree<T>(IList<Triple> triples, SyntaxTree st, Semantic
136137 /// <summary>
137138 /// Member (field, property) initialization
138139 /// </summary>
139- public static void GetConstructsWithinClass ( IList < Triple > triples , ClassDeclarationSyntax declaration , SemanticModel sem , ClassNode classNode )
140- {
141- var creates = declaration . DescendantNodes ( ) . OfType < ObjectCreationExpressionSyntax > ( ) ;
142- foreach ( var creation in creates )
143- {
144- var node = sem . GetTypeInfo ( creation ) . CreateClassNode ( ) ;
145- triples . Add ( new TripleConstruct ( classNode , node ) ) ;
146- }
147- }
140+ // public static void GetConstructsWithinClass(IList<Triple> triples, ClassDeclarationSyntax declaration, SemanticModel sem, ClassNode classNode)
141+ // {
142+ // var creates = declaration.DescendantNodes().OfType<ObjectCreationExpressionSyntax>();
143+ // foreach (var creation in creates)
144+ // {
145+ // var node = sem.GetTypeInfo(creation).CreateClassNode();
146+ // triples.Add(new TripleConstruct(classNode, node));
147+ // }
148+ // }
148149
149150 /// <summary>
150151 /// Type inherited from BaseType
151152 /// </summary>
152- public static void GetInherits ( IList < Triple > triples , TypeDeclarationSyntax declaration , SemanticModel sem , Node node )
153+ public static void GetInherits ( IList < Triple > triples , TypeDeclarationSyntax declaration , SemanticModel sem , TypeNode node )
153154 {
154155 if ( declaration . BaseList != null )
155156 {
156157 foreach ( var baseTypeSyntax in declaration . BaseList . Types )
157158 {
158- var parentNode = sem . GetTypeInfo ( baseTypeSyntax . Type ) . CreateNode ( ) ;
159- if ( parentNode != null )
159+ var parentNode = sem . GetTypeInfo ( baseTypeSyntax . Type ) . CreateTypeNode ( ) ;
160+ if ( node is ClassNode classNode )
161+ {
162+ triples . Add ( new TripleOfType ( classNode , parentNode ) ) ;
163+ }
164+ if ( node is InterfaceNode interfaceNode && parentNode is InterfaceNode parentInterfaceNode )
160165 {
161- triples . Add ( new TripleInherit ( node , parentNode ) ) ;
166+ triples . Add ( new TripleOfType ( interfaceNode , parentInterfaceNode ) ) ;
162167 }
163168 }
164169 }
@@ -167,7 +172,7 @@ public static void GetInherits(IList<Triple> triples, TypeDeclarationSyntax decl
167172 /// <summary>
168173 /// Class or Interface have some method AND some method can call another method AND some method can creates an object of class
169174 /// </summary>
170- public static void GetMethodsAll ( IList < Triple > triples , TypeDeclarationSyntax declaration , SemanticModel sem , Node node )
175+ public static void GetMethodsAll ( IList < Triple > triples , TypeDeclarationSyntax declaration , SemanticModel sem , TypeNode node )
171176 {
172177 var methods = declaration . DescendantNodes ( ) . OfType < MethodDeclarationSyntax > ( ) ;
173178 foreach ( var method in methods )
@@ -185,8 +190,7 @@ public static void GetMethodsAll(IList<Triple> triples, TypeDeclarationSyntax de
185190 break ;
186191
187192 case InvocationExpressionSyntax invocation :
188- var invokedSymbol = sem . GetSymbolInfo ( invocation ) . Symbol as IMethodSymbol ;
189- if ( invokedSymbol != null )
193+ if ( sem . GetSymbolInfo ( invocation ) . Symbol is IMethodSymbol invokedSymbol )
190194 {
191195 var invokedMethod = invokedSymbol . CreateMethodNode ( ) ;
192196 triples . Add ( new TripleInvoke ( methodNode , invokedMethod ) ) ;
0 commit comments