@@ -111,11 +111,12 @@ def _get_docstring(self, body_node):
111111 return self ._get_node_text (first_child .children [0 ])
112112 return None
113113
114- def parse (self , file_path : Path , is_dependency : bool = False , is_notebook : bool = False ) -> Dict :
114+ def parse (self , file_path : Path , is_dependency : bool = False , is_notebook : bool = False , index_source : bool = False ) -> Dict :
115115 """Parses a file and returns its structure in a standardized dictionary format."""
116116 original_file_path = file_path
117117 temp_py_file = None
118118 source_code = None
119+ self .index_source = index_source
119120
120121 try :
121122 if is_notebook :
@@ -140,7 +141,7 @@ def parse(self, file_path: Path, is_dependency: bool = False, is_notebook: bool
140141 root_node = tree .root_node
141142
142143 functions = self ._find_functions (root_node )
143- functions .extend (self ._find_lambda_assignments (root_node ))
144+ functions .extend (self ._find_lambda_assignments (root_node , index_source ))
144145 classes = self ._find_classes (root_node )
145146 imports = self ._find_imports (root_node )
146147 function_calls = self ._find_calls (root_node )
@@ -164,7 +165,7 @@ def parse(self, file_path: Path, is_dependency: bool = False, is_notebook: bool
164165 os .remove (temp_py_file )
165166 info_logger (f"Removed temporary file: { temp_py_file } " )
166167
167- def _find_lambda_assignments (self , root_node ):
168+ def _find_lambda_assignments (self , root_node , index_source : bool = False ):
168169 functions = []
169170 query_str = PY_QUERIES .get ('lambda_assignments' )
170171 if not query_str : return []
@@ -187,9 +188,6 @@ def _find_lambda_assignments(self, root_node):
187188 "line_number" : node .start_point [0 ] + 1 ,
188189 "end_line" : assignment_node .end_point [0 ] + 1 ,
189190 "args" : [p for p in [self ._get_node_text (p ) for p in params_node .children if p .type == 'identifier' ] if p ] if params_node else [],
190- "source" : self ._get_node_text (assignment_node ),
191-
192- "docstring" : None ,
193191 "cyclomatic_complexity" : 1 ,
194192 "context" : context ,
195193 "context_type" : context_type ,
@@ -198,10 +196,14 @@ def _find_lambda_assignments(self, root_node):
198196 "lang" : self .language_name ,
199197 "is_dependency" : False ,
200198 }
199+ if self .index_source :
200+ func_data ["source" ] = self ._get_node_text (assignment_node )
201+ func_data ["docstring" ] = None
202+
201203 functions .append (func_data )
202204 return functions
203205
204- def _find_functions (self , root_node ):
206+ def _find_functions (self , root_node , index_source : bool = False ):
205207 functions = []
206208 query_str = PY_QUERIES ['functions' ]
207209 for match in execute_query (self .language , query_str , root_node ):
@@ -253,9 +255,6 @@ def _find_functions(self, root_node):
253255 "line_number" : node .start_point [0 ] + 1 ,
254256 "end_line" : func_node .end_point [0 ] + 1 ,
255257 "args" : args ,
256- "source" : self ._get_node_text (func_node ),
257-
258- "docstring" : self ._get_docstring (body_node ),
259258 "cyclomatic_complexity" : self ._calculate_complexity (func_node ),
260259 "context" : context ,
261260 "context_type" : context_type ,
@@ -264,10 +263,15 @@ def _find_functions(self, root_node):
264263 "lang" : self .language_name ,
265264 "is_dependency" : False ,
266265 }
266+
267+ if self .index_source :
268+ func_data ["source" ] = self ._get_node_text (func_node )
269+ func_data ["docstring" ] = self ._get_docstring (body_node )
270+
267271 functions .append (func_data )
268272 return functions
269273
270- def _find_classes (self , root_node ):
274+ def _find_classes (self , root_node , index_source : bool = False ):
271275 classes = []
272276 query_str = PY_QUERIES ['classes' ]
273277 for match in execute_query (self .language , query_str , root_node ):
@@ -293,13 +297,15 @@ def _find_classes(self, root_node):
293297 "line_number" : node .start_point [0 ] + 1 ,
294298 "end_line" : class_node .end_point [0 ] + 1 ,
295299 "bases" : [b for b in bases if b ],
296- "source" : self ._get_node_text (class_node ),
297- "docstring" : self ._get_docstring (body_node ),
298300 "context" : context ,
299301 "decorators" : [d for d in decorators if d ],
300302 "lang" : self .language_name ,
301303 "is_dependency" : False ,
302304 }
305+ if self .index_source :
306+ class_data ["source" ] = self ._get_node_text (class_node )
307+ class_data ["docstring" ] = self ._get_docstring (body_node )
308+
303309 classes .append (class_data )
304310 return classes
305311
0 commit comments