Subject: bug#66431: [PATCH] Fix reset treesit--explorer-last-node when explorer buffer was killed

From: nvp <[email-masked]>
Date: Sat, 14 Oct 2023 21:20:34 -0700

Hi,
The patch is supposed to reset `treesit--explorer-last-node` in the source
buffer, just before the `(with-current-buffer treesit--explorer-buffer
...)`.
Upon trying to reproduce it now, I realized it's harder to reproduce than
I had thought -- sorry about that.
I noticed the bug (if it is a bug) initially when I was adding a function
to jump b/w source and explorer buffers, like the following:

(defun my-treesit-explorer-jump ()
  "Pop b/w source and explorer buffers."
  (interactive)
  (let ((buf
         (cond
          ((eq major-mode 'treesit--explorer-tree-mode)
           (when (buffer-live-p treesit--explorer-source-buffer)
             treesit--explorer-source-buffer))
          (t
           (unless (and treesit-explore-mode
                        (buffer-live-p treesit--explorer-buffer))
             ;; *** Without the reset here, the explorer buffer doesn't
             ;; get redrawn the first time, when treesit--explorer-last-node
             ;; is non-nil in the source buffer ***
             ;; (setq-local treesit--explorer-last-node nil)
             (cl-letf (((symbol-function (function completing-read))
                        (lambda (&rest _) (symbol-name (treesit-language-at
(point))))))
               (treesit-explore-mode 1)))
           treesit--explorer-buffer))))
    (pop-to-buffer buf)))

Let me give a more precise recipe to reproduce:
1. From a c++-ts-mode buffer, call `treesit-explorer-mode`, select `cpp`.
Now there should be an explorer buffer.
2. Kill the associated explorer buffer.
3. Now, back in the c++-ts-mode buffer, `treesit--explorer-last-node`
should still have a value.
4. From that c++-ts-mode buffer, call `my-treesit-explorer-jump`, and the
explorer buffer should be empty, until
switching back to the source buffer.

This seems to me to be caused by `treesit--explorer-post-command` not
running until the source
buffer is active again.

On Sat, Oct 14, 2023 at 10:08 AM Yuan Fu <[email-masked]> wrote:

>
>
> > On Oct 14, 2023, at 1:17 AM, Eli Zaretskii <[email-masked]> wrote:
> >
> >> From: nvp <[email-masked]>
> >> Date: Mon, 9 Oct 2023 23:02:48 -0700
> >>
> >> Tags: patch
> >>
> >> Bug: After `treesit-explorer-mode` is enabled in a buffer and its
> >> associated `treesit--explorer-buffer` is killed, a subsequent call
> >> to `treesit-explorer-mode` initially displays an empty explorer
> >> buffer b/c `treesit--explorer-refresh` sees old value for
> >> `treesit--explorer-last-node`.
> >>
> >> * lisp/treesit.el (treesit-explorer-mode): reset
> >> `treesit--explorer-last-node` when `treesit--explorer-buffer` was killed
> >>
> >> In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
> >> 3.24.33, cairo version 1.16.0) of 2023-10-05 built on noah-X580VD
> >> Repository revision: 505c80623049d9e181918acdac8229c9a2041b1e
> >> Repository branch: master
> >> Windowing system distributor 'The X.Org Foundation', version
> 11.0.12101004
> >> System Description: Ubuntu 22.04.3 LTS
> >
> > Yuan, could you please look into this?  Is the patch OK to go in, and
> > if so, should it be installed on emacs-29?
>
> I’ll look at it, thanks for the reminder.
>
> Yuan
>
>