Subject: Re: bug#61617: M-x mark-defun doesn't work correctly in tree-sitter modes when comments exist between functions

From: Yuan Fu <[email-masked]>
Date: Mon, 27 Feb 2023 01:24:42 -0800

> On Feb 26, 2023, at 4:42 PM, Yuan Fu <[email-masked]> wrote:
> 
> 
> 
>> On Feb 24, 2023, at 11:27 PM, Evgeni Kolev <[email-masked]> wrote:
>> 
>> On Sat, Feb 25, 2023 at 7:08 AM Yuan Fu <[email-masked]> wrote:
>> 
>>> Huh, with or without comments, mark-defun always includes the empty
>>> lines before the defun for me. I get the same behavior in rust-ts-mode.
>>> This seems intentional, because this is at the end of the definition of
>>> mark-defun:
>>> 
>>> (skip-chars-backward "[:space:]\n")
>>> (unless (bobp)
>>> (forward-line 1))
>> 
>> Did you check with my example or another example? With my example, the
>> issue I get is that only the empty lines are marked, without the
>> defun, without the comments. I'm attaching two screenshots - before
>> and after mark-defun.
>> 
>> Note: I get the correct behaviour when there is just one defun in the
>> file. But if there are more, or the defun at point is not at the top
>> of the file - mark-defun does not work as expected.
> 
> Sorry, I used wrong go mode when testing your recipe. I used go-mode thinking it is go-ts-mode :-( Using go-ts-mode, I can reproduce what you see, yes.
> 
> The problem you discovered revealed some shortcoming in our tree-sitter navigation function, and requires quite a few non-trivial changes, I working on fixing it.
> 
> The direct cause is that (treesit-beginning-of-defun -1) is supposed to go to the beginning of next defun, but instead goes to the beginning of next next defun. That was a design decision I made but I can see it causing old functions to misbehave. 
> 
> Yuan

The fix is brain-twisting but don’t embody a lot of code, yay! This now should be fixed.

Yuan