summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuan Fu <casouri@gmail.com>2023-01-19 14:22:56 -0800
committerYuan Fu <casouri@gmail.com>2023-01-19 14:47:25 -0800
commit7ca71d66dc7365a3dd8dd5f20638b4fa612fdc5e (patch)
tree4ea3d5289ec03be5444c6abe7f04447152964539
parentb7d6bb47ee5f6a459a873c5053c2dde9df4f2e2f (diff)
downloademacs-7ca71d66dc7365a3dd8dd5f20638b4fa612fdc5e.tar.gz
Fix various problems in treesit-explore-mode (bug#60800)
* lisp/treesit.el: (treesit--explorer-kill-explorer-buffer): New function. (treesit-explore-mode): 1. Move prompt for language earlier, and terminate early if language not available. 2. Make sure desktop-save doesn't save the explorer buffer. 3. Kill the explorer buffer when the source buffer is killed.
-rw-r--r--lisp/treesit.el59
1 files changed, 40 insertions, 19 deletions
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 5a65d565236..660039cc7cc 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -2621,6 +2621,11 @@ leaves point at the end of the last line of NODE."
(when (not named)
(overlay-put ov 'face 'treesit-explorer-anonymous-node)))))
+(defun treesit--explorer-kill-explorer-buffer ()
+ "Kill the explorer buffer of this buffer."
+ (when (buffer-live-p treesit--explorer-buffer)
+ (kill-buffer treesit--explorer-buffer)))
+
(define-derived-mode treesit--explorer-tree-mode special-mode
"TS Explorer"
"Mode for displaying syntax trees for `treesit-explore-mode'."
@@ -2632,30 +2637,46 @@ Pops up a window showing the syntax tree of the source in the
current buffer in real time. The corresponding node enclosing
the text in the active region is highlighted in the explorer
window."
- :lighter " TSplay"
+ :lighter " TSexplore"
(if treesit-explore-mode
- (progn
- (unless (buffer-live-p treesit--explorer-buffer)
- (setq-local treesit--explorer-buffer
- (get-buffer-create
- (format "*tree-sitter explorer for %s*"
- (buffer-name))))
- (setq-local treesit--explorer-language
- (intern (completing-read
+ (let ((language (intern (completing-read
"Language: "
(mapcar #'treesit-parser-language
- (treesit-parser-list)))))
- (with-current-buffer treesit--explorer-buffer
- (treesit--explorer-tree-mode)))
- (display-buffer treesit--explorer-buffer
- (cons nil '((inhibit-same-window . t))))
- (treesit--explorer-refresh)
- (add-hook 'post-command-hook
- #'treesit--explorer-post-command 0 t)
- (setq-local treesit--explorer-last-node nil))
+ (treesit-parser-list))))))
+ (if (not (treesit-language-available-p language))
+ (user-error "Cannot find tree-sitter grammar for %s: %s"
+ language (cdr (treesit-language-available-p
+ language t)))
+ ;; Create explorer buffer.
+ (unless (buffer-live-p treesit--explorer-buffer)
+ (setq-local treesit--explorer-buffer
+ (get-buffer-create
+ (format "*tree-sitter explorer for %s*"
+ (buffer-name))))
+ (setq-local treesit--explorer-language language)
+ (with-current-buffer treesit--explorer-buffer
+ (treesit--explorer-tree-mode)))
+ (display-buffer treesit--explorer-buffer
+ (cons nil '((inhibit-same-window . t))))
+ (treesit--explorer-refresh)
+ ;; Setup variables and hooks.
+ (add-hook 'post-command-hook
+ #'treesit--explorer-post-command 0 t)
+ (add-hook 'kill-buffer-hook
+ #'treesit--explorer-kill-explorer-buffer 0 t)
+ (setq-local treesit--explorer-last-node nil)
+ ;; Tell `desktop-save' to not save explorer buffers.
+ (when (boundp 'desktop-modes-not-to-save)
+ (unless (memq 'treesit--explorer-tree-mode
+ desktop-modes-not-to-save)
+ (push 'treesit--explorer-tree-mode
+ desktop-modes-not-to-save)))))
+ ;; Turn off explore mode.
(remove-hook 'post-command-hook
#'treesit--explorer-post-command t)
- (kill-buffer treesit--explorer-buffer)))
+ (remove-hook 'post-command-hook
+ #'treesit--explorer-kill-explorer-buffer t)
+ (treesit--explorer-kill-explorer-buffer)))
;;; Install & build language grammar