@@ -103,87 +103,116 @@ def build(self, fragments: list[Fragment], repo_root: Path | None = None) -> Edg
103103 return {}
104104
105105 edges : EdgeDict = {}
106+ indices = self ._build_indices (dotnet_frags )
106107
108+ for df in dotnet_frags :
109+ self ._link_fragment (df , indices , edges )
110+
111+ return edges
112+
113+ def _build_indices (
114+ self , dotnet_frags : list [Fragment ]
115+ ) -> tuple [dict [str , list [FragmentId ]], dict [str , list [FragmentId ]], dict [str , list [FragmentId ]]]:
107116 namespace_to_frags : dict [str , list [FragmentId ]] = defaultdict (list )
108117 type_to_frags : dict [str , list [FragmentId ]] = defaultdict (list )
109118 fqn_to_frags : dict [str , list [FragmentId ]] = defaultdict (list )
110119
111120 for f in dotnet_frags :
112121 ns = _extract_namespace (f .content , f .path )
113122 if ns :
114- namespace_to_frags [ ns .lower ()]. append ( f . id )
115- for i in range (len (ns . split ( "." ) )):
116- partial_ns = "." .join (ns . split ( "." ) [: i + 1 ])
123+ ns_parts = ns .split ( "." )
124+ for i in range (len (ns_parts )):
125+ partial_ns = "." .join (ns_parts [: i + 1 ])
117126 namespace_to_frags [partial_ns .lower ()].append (f .id )
118127
119- types = _extract_types (f .content , f .path )
120- for t in types :
128+ for t in _extract_types (f .content , f .path ):
121129 type_to_frags [t .lower ()].append (f .id )
122130 if ns :
123- fqn = f"{ ns } .{ t } "
124- fqn_to_frags [fqn .lower ()].append (f .id )
125-
126- for df in dotnet_frags :
127- usings = _extract_usings (df .content , df .path )
128- inheritance = _extract_inheritance (df .content )
129- type_refs = _extract_type_refs (df .content )
130- attributes = _extract_attributes (df .content )
131- current_ns = _extract_namespace (df .content , df .path )
132- current_types = _extract_types (df .content , df .path )
133-
134- for using in usings :
135- using_lower = using .lower ()
136- if using_lower in namespace_to_frags :
137- for fid in namespace_to_frags [using_lower ]:
138- if fid != df .id :
139- self .add_edge (edges , df .id , fid , self .using_weight )
140-
141- if using_lower in fqn_to_frags :
142- for fid in fqn_to_frags [using_lower ]:
143- if fid != df .id :
144- self .add_edge (edges , df .id , fid , self .using_weight )
145-
146- parts = using .split ("." )
147- if parts :
148- type_name = parts [- 1 ].lower ()
149- if type_name in type_to_frags :
150- for fid in type_to_frags [type_name ]:
151- if fid != df .id :
152- self .add_edge (edges , df .id , fid , self .using_weight )
153-
154- for parent in inheritance :
155- parent_lower = parent .lower ()
156- if parent_lower in type_to_frags :
157- for fid in type_to_frags [parent_lower ]:
158- if fid != df .id :
159- self .add_edge (edges , df .id , fid , self .inheritance_weight )
160-
161- for type_ref in type_refs :
162- ref_lower = type_ref .lower ()
163- if ref_lower in type_to_frags :
164- for fid in type_to_frags [ref_lower ]:
165- if fid != df .id :
166- self .add_edge (edges , df .id , fid , self .type_weight )
167-
168- for attr in attributes :
169- attr_lower = attr .lower ()
170- attr_full = (attr_lower + "attribute" ) if not attr_lower .endswith ("attribute" ) else attr_lower
171- for lookup in [attr_lower , attr_full ]:
172- if lookup in type_to_frags :
173- for fid in type_to_frags [lookup ]:
174- if fid != df .id :
175- self .add_edge (edges , df .id , fid , self .attribute_weight )
176-
177- if current_ns and current_ns .lower () in namespace_to_frags :
178- for fid in namespace_to_frags [current_ns .lower ()]:
131+ fqn_to_frags [f"{ ns } .{ t } " .lower ()].append (f .id )
132+
133+ return namespace_to_frags , type_to_frags , fqn_to_frags
134+
135+ def _link_fragment (
136+ self ,
137+ df : Fragment ,
138+ indices : tuple [dict [str , list [FragmentId ]], dict [str , list [FragmentId ]], dict [str , list [FragmentId ]]],
139+ edges : EdgeDict ,
140+ ) -> None :
141+ namespace_to_frags , type_to_frags , fqn_to_frags = indices
142+
143+ self ._link_usings (df , namespace_to_frags , fqn_to_frags , type_to_frags , edges )
144+ self ._link_refs (df , type_to_frags , edges )
145+ self ._link_same_namespace (df , namespace_to_frags , edges )
146+ self ._link_partial_classes (df , type_to_frags , edges )
147+
148+ def _link_usings (
149+ self ,
150+ df : Fragment ,
151+ namespace_to_frags : dict [str , list [FragmentId ]],
152+ fqn_to_frags : dict [str , list [FragmentId ]],
153+ type_to_frags : dict [str , list [FragmentId ]],
154+ edges : EdgeDict ,
155+ ) -> None :
156+ for using in _extract_usings (df .content , df .path ):
157+ using_lower = using .lower ()
158+ for fid in namespace_to_frags .get (using_lower , []):
159+ if fid != df .id :
160+ self .add_edge (edges , df .id , fid , self .using_weight )
161+
162+ for fid in fqn_to_frags .get (using_lower , []):
163+ if fid != df .id :
164+ self .add_edge (edges , df .id , fid , self .using_weight )
165+
166+ parts = using .split ("." )
167+ if parts :
168+ for fid in type_to_frags .get (parts [- 1 ].lower (), []):
179169 if fid != df .id :
180- self .add_edge (edges , df .id , fid , self .same_namespace_weight )
181-
182- for current_type in current_types :
183- ct_lower = current_type .lower ()
184- if ct_lower in type_to_frags :
185- for fid in type_to_frags [ct_lower ]:
186- if fid != df .id :
187- self .add_edge (edges , df .id , fid , self .partial_class_weight )
188-
189- return edges
170+ self .add_edge (edges , df .id , fid , self .using_weight )
171+
172+ def _link_refs (
173+ self ,
174+ df : Fragment ,
175+ type_to_frags : dict [str , list [FragmentId ]],
176+ edges : EdgeDict ,
177+ ) -> None :
178+ for parent in _extract_inheritance (df .content ):
179+ for fid in type_to_frags .get (parent .lower (), []):
180+ if fid != df .id :
181+ self .add_edge (edges , df .id , fid , self .inheritance_weight )
182+
183+ for type_ref in _extract_type_refs (df .content ):
184+ for fid in type_to_frags .get (type_ref .lower (), []):
185+ if fid != df .id :
186+ self .add_edge (edges , df .id , fid , self .type_weight )
187+
188+ for attr in _extract_attributes (df .content ):
189+ attr_lower = attr .lower ()
190+ attr_full = attr_lower if attr_lower .endswith ("attribute" ) else attr_lower + "attribute"
191+ for lookup in [attr_lower , attr_full ]:
192+ for fid in type_to_frags .get (lookup , []):
193+ if fid != df .id :
194+ self .add_edge (edges , df .id , fid , self .attribute_weight )
195+
196+ def _link_same_namespace (
197+ self ,
198+ df : Fragment ,
199+ namespace_to_frags : dict [str , list [FragmentId ]],
200+ edges : EdgeDict ,
201+ ) -> None :
202+ current_ns = _extract_namespace (df .content , df .path )
203+ if not current_ns :
204+ return
205+ for fid in namespace_to_frags .get (current_ns .lower (), []):
206+ if fid != df .id :
207+ self .add_edge (edges , df .id , fid , self .same_namespace_weight )
208+
209+ def _link_partial_classes (
210+ self ,
211+ df : Fragment ,
212+ type_to_frags : dict [str , list [FragmentId ]],
213+ edges : EdgeDict ,
214+ ) -> None :
215+ for current_type in _extract_types (df .content , df .path ):
216+ for fid in type_to_frags .get (current_type .lower (), []):
217+ if fid != df .id :
218+ self .add_edge (edges , df .id , fid , self .partial_class_weight )
0 commit comments