@@ -64,7 +64,7 @@ def _outer_indent(self, code):
6464 return min (spaces_arr ) if spaces_arr else 4
6565
6666
67- def get_all_class_method_names (self ) -> List :
67+ def get_all_class_method_names (self ) -> Dict :
6868 """
6969 Gets all the method names from a file.
7070
@@ -164,6 +164,63 @@ def get_all_method_documentations(self, strip_quotes: bool=False) -> Dict:
164164 def _get_var_names_as_string_from_param_list (self , param_list ):
165165 return "(" + ", " .join ([var_name for var_name , _ , _ in param_list ]) + ")"
166166
167+ def get_all_class_method_bodies (self , strip_docstr : bool = False , get_index : bool = False ) -> Dict :
168+ class_method_names = self .get_all_class_method_names ()
169+ methods_and_params = self .get_all_function_names_with_params ()
170+ method_and_docstr = self .get_all_method_docstrings ()
171+
172+ captures = self ._run_query_and_get_captures ('all_function_bodies' , self .root_node )
173+
174+ ret_struct = {}
175+ pp = {}
176+
177+ for i in range (0 , len (captures ), 2 ):
178+ func_name = match_from_span (captures [i ][0 ], self .splitted_code )
179+ for class_name , method_name_list in class_method_names .items ():
180+ if func_name in method_name_list and (func_name != "__init__" and func_name != "__new__" ):
181+ if pp .get (class_name ) is None :
182+ pp [class_name ] = {}
183+ pp [class_name ] = {func_name :
184+ (match_from_span (captures [i + 1 ][0 ], self .splitted_code ), captures [i + 1 ][0 ].start_point , captures [i + 1 ][0 ].end_point )
185+ }
186+ else :
187+ if func_name != "__init__" and func_name != "__new__" :
188+ pp [class_name ][func_name ] = (match_from_span (captures [i + 1 ][0 ], self .splitted_code ), captures [i + 1 ][0 ].start_point , captures [i + 1 ][0 ].end_point )
189+
190+ if strip_docstr :
191+ for class_name , func_data_struct in pp .items ():
192+ ret_struct [class_name ] = {}
193+ for k , (v ,sp ,ep ) in func_data_struct .items ():
194+ if method_and_docstr [class_name ].get (k ) is not None and method_and_docstr [class_name ].get (k ) is not '' :
195+ code = v .replace (method_and_docstr [class_name ][k ], "" )
196+ outer_indent = self ._outer_indent (code )
197+ spaces = " " .join (['' ] * (outer_indent + 1 ))
198+ if code .startswith ("\n " ):
199+ ret_struct [class_name ][k ] = (f"def { k } { self ._get_var_names_as_string_from_param_list (methods_and_params [k ])} :{ code } " ,
200+ method_and_docstr [class_name ][k ])
201+ else :
202+ ret_struct [class_name ][k ] = (f"def { k } { self ._get_var_names_as_string_from_param_list (methods_and_params [k ])} :\n { spaces } { code } " ,
203+ method_and_docstr [class_name ][k ])
204+ else :
205+ outer_indent = self ._outer_indent (v )
206+ spaces = " " .join (['' ] * (outer_indent + 1 ))
207+ ret_struct [class_name ][k ] = (f"def { k } { self ._get_var_names_as_string_from_param_list (methods_and_params [k ])} :\n { spaces } { v } " , "" )
208+ if get_index :
209+ ret_struct [class_name ][k ] = ret_struct [class_name ][k ],sp ,ep
210+ else :
211+ for class_name , func_data_struct in pp .items ():
212+ for k , (v ,sp ,ep ) in func_data_struct .items ():
213+ outer_indent = self ._outer_indent (v )
214+ spaces = " " .join (['' ] * (outer_indent + 1 ))
215+ if ret_struct .get (class_name ) is None :
216+ ret_struct [class_name ] = {}
217+ ret_struct [class_name ][k ] = f"def { k } { self ._get_var_names_as_string_from_param_list (methods_and_params [k ])} :\n { spaces } { v } "
218+ else :
219+ ret_struct [class_name ][k ] = f"def { k } { self ._get_var_names_as_string_from_param_list (methods_and_params [k ])} :\n { spaces } { v } "
220+ if get_index :
221+ ret_struct [class_name ][k ] = (ret_struct [class_name ][k ], sp , ep )
222+ return ret_struct
223+
167224 def get_all_function_bodies (self , strip_docstr : bool = False , get_index : bool = False ) -> Dict :
168225 """
169226 Returns a dict where function names are the key and the whole function code are the values
0 commit comments