Subject: Re: bug#62825: 29.0.90; c-ts-mode doesn't recognize DEFUN functions

From: Eli Zaretskii <[email-masked]>
Date: Fri, 14 Apr 2023 09:59:42 +0300

> Date: Fri, 14 Apr 2023 09:44:22 +0300
> From: Eli Zaretskii <[email-masked]>
> 
> Evaluating '(treesit-defun-at-point)' inside a function defined via
> the DEFUN macro yields nil, when the buffer is under c-ts-mode.  This
> gets in the way of significant functionalities, like
> c-ts-mode-indent-defun and generating ChangeLog-style log entries
> using "C-x 4 a" and the likes.
> 
> To reproduce, visit any of the Emacs C source files, like treesit.c,
> with c-ts-mode, move point to a function defined via DEFUN, and type
> "C-x 4 a".  The resulting ChangeLog entry will have the file name, but
> not the expected function name in parentheses.
> 
> Please fix this ASAP, this is a bad regression for those, like myself,
> who want to use c-ts-mode.

Using treesit-explore-mode I see that c-ts-mode regards DEFUN
functions as a sequence of 2 expression_statement nodes.  Here's the
tree corresponding to the DEFUN macro call:

  (expression_statement
   (call_expression
    function: 
     (call_expression function: (identifier) <<<<<<<<<<<<<<<<<<<
      arguments: 
       (argument_list (
	(string_literal " ")
	, (identifier) , (identifier) , (number_literal) , (number_literal) ,
		      (number_literal)
	, (identifier)
	(ERROR :)
	(comment) )))
    arguments: 
     (argument_list ( (identifier)
      (ERROR (identifier))
      , (identifier)
      (ERROR (identifier))
      )))
   ;)

The identifier node on the line indicated with "<<<<<" is "DEFUN", so
maybe we can recognize this.  The body of the DEFUN is another
compound_statement node, following the DEFUN's node.