@@ -161,8 +161,9 @@ def get_function(path_or_url: str, function_name: str, git_revision: Optional[st
161161 return {"error" : f"Language '{ lang_name } ' not supported" }
162162
163163 source = get_file_content (path_or_url , git_revision )
164+ source_bytes = source .encode ('utf-8' ) if isinstance (source , str ) else source
164165
165- tree = parser .parse (source )
166+ tree = parser .parse (source_bytes )
166167
167168 # Define function node types for different languages
168169 func_types = {
@@ -189,23 +190,29 @@ def find_function(node):
189190 name = None
190191 for child in node .children :
191192 if child .type == 'identifier' :
192- name = source [child .start_byte :child .end_byte ].decode (
193- 'utf-8' ) if isinstance (source , bytes ) else source [child .start_byte :child .end_byte ]
193+ if isinstance (source , str ):
194+ name = source [child .start_byte :child .end_byte ]
195+ else :
196+ name = source [child .start_byte :child .end_byte ].decode ('utf-8' ) if isinstance (source , bytes ) else source [child .start_byte :child .end_byte ]
194197 break
195198 elif hasattr (child , 'children' ):
196199 for grandchild in child .children :
197200 if grandchild .type == 'identifier' :
198- name = source [grandchild .start_byte :grandchild .end_byte ].decode (
199- 'utf-8' ) if isinstance (source , bytes ) else source [grandchild .start_byte :grandchild .end_byte ]
201+ if isinstance (source , str ):
202+ name = source [grandchild .start_byte :grandchild .end_byte ]
203+ else :
204+ name = source [grandchild .start_byte :grandchild .end_byte ].decode ('utf-8' ) if isinstance (source , bytes ) else source [grandchild .start_byte :grandchild .end_byte ]
200205 break
201206 if name :
202207 break
203208
204209 # Use field name if available (more reliable)
205210 name_node = node .child_by_field_name ('name' )
206211 if name_node :
207- name = source [name_node .start_byte :name_node .end_byte ].decode (
208- 'utf-8' ) if isinstance (source , bytes ) else source [name_node .start_byte :name_node .end_byte ]
212+ if isinstance (source , str ):
213+ name = source [name_node .start_byte :name_node .end_byte ]
214+ else :
215+ name = source [name_node .start_byte :name_node .end_byte ].decode ('utf-8' ) if isinstance (source , bytes ) else source [name_node .start_byte :name_node .end_byte ]
209216
210217 if name == function_name :
211218 return node
@@ -264,8 +271,9 @@ def get_class(path_or_url: str, class_name: str, git_revision: Optional[str] = N
264271 return {"error" : f"Language '{ lang_name } ' not supported" }
265272
266273 source = get_file_content (path_or_url , git_revision )
274+ source_bytes = source .encode ('utf-8' ) if isinstance (source , str ) else source
267275
268- tree = parser .parse (source )
276+ tree = parser .parse (source_bytes )
269277
270278 # Define class node types for different languages
271279 class_types = {
@@ -293,16 +301,20 @@ def find_class(node):
293301 # Extract class name
294302 for child in node .children :
295303 if child .type == 'identifier' :
296- name = source [child .start_byte :child .end_byte ].decode (
297- 'utf-8' ) if isinstance (source , bytes ) else source [child .start_byte :child .end_byte ]
304+ if isinstance (source , str ):
305+ name = source [child .start_byte :child .end_byte ]
306+ else :
307+ name = source [child .start_byte :child .end_byte ].decode ('utf-8' ) if isinstance (source , bytes ) else source [child .start_byte :child .end_byte ]
298308 if name == class_name :
299309 return node
300310
301311 # Use field name if available (more reliable)
302312 name_node = node .child_by_field_name ('name' )
303313 if name_node :
304- name = source [name_node .start_byte :name_node .end_byte ].decode (
305- 'utf-8' ) if isinstance (source , bytes ) else source [name_node .start_byte :name_node .end_byte ]
314+ if isinstance (source , str ):
315+ name = source [name_node .start_byte :name_node .end_byte ]
316+ else :
317+ name = source [name_node .start_byte :name_node .end_byte ].decode ('utf-8' ) if isinstance (source , bytes ) else source [name_node .start_byte :name_node .end_byte ]
306318 if name == class_name :
307319 return node
308320
@@ -418,7 +430,7 @@ def get_signature(path_or_url: str, function_name: str, git_revision: Optional[s
418430 return {
419431 "signature" : signature ,
420432 "function" : function_name ,
421- "file" : file_path ,
433+ "file" : path_or_url ,
422434 "start_line" : result ["start_line" ]
423435 }
424436
0 commit comments