summaryrefslogtreecommitdiff
path: root/admin/notes/tree-sitter
diff options
context:
space:
mode:
authorYuan Fu <casouri@gmail.com>2022-10-10 11:00:51 -0700
committerYuan Fu <casouri@gmail.com>2022-10-10 11:00:51 -0700
commit2f6b017e3dc646d317b56fb453c90aaa44c27089 (patch)
tree59bc4ffa2c12d9bd3b016af340b14adb53ce91a5 /admin/notes/tree-sitter
parent2a762336da4d886a4f1b3ee463fd66393d739309 (diff)
downloademacs-2f6b017e3dc646d317b56fb453c90aaa44c27089.tar.gz
emacs-2f6b017e3dc646d317b56fb453c90aaa44c27089.tar.bz2
emacs-2f6b017e3dc646d317b56fb453c90aaa44c27089.zip
* admin/notes/tree-sitter/starter-guide (Navigation): Improve demo.
Diffstat (limited to 'admin/notes/tree-sitter')
-rw-r--r--admin/notes/tree-sitter/starter-guide38
1 files changed, 26 insertions, 12 deletions
diff --git a/admin/notes/tree-sitter/starter-guide b/admin/notes/tree-sitter/starter-guide
index 378ff581afa..e5736038622 100644
--- a/admin/notes/tree-sitter/starter-guide
+++ b/admin/notes/tree-sitter/starter-guide
@@ -350,24 +350,38 @@ node.
Something like this should suffice:
#+begin_src elisp
-(defun xxx-beginning-of-defun (&optional arg)
- (if (> arg 0)
- ;; Go backward.
+(defun js--treesit-beginning-of-defun (&optional arg)
+ (let ((arg (or arg 1)))
+ (if (> arg 0)
+ ;; Go backward.
+ (while (and (> arg 0)
+ (treesit-search-forward-goto
+ "function_definition" 'start nil t))
+ (setq arg (1- arg)))
+ ;; Go forward.
+ (while (and (< arg 0)
+ (treesit-search-forward-goto
+ "function_definition" 'start))
+ (setq arg (1+ arg))))))
+
+(defun xxx-end-of-defun (&optional arg)
+ (let ((arg (or arg 1)))
+ (if (< arg 0)
+ ;; Go backward.
+ (while (and (< arg 0)
+ (treesit-search-forward-goto
+ "function_definition" 'end nil t))
+ (setq arg (1+ arg)))
+ ;; Go forward.
(while (and (> arg 0)
(treesit-search-forward-goto
- "function_definition" 'start nil t))
- (setq arg (1- arg)))
- ;; Go forward.
- (while (and (< arg 0)
- (treesit-search-forward-goto
- "function_definition" 'start))
- (setq arg (1+ arg)))))
+ "function_definition" 'end))
+ (setq arg (1- arg))))))
(setq-local beginning-of-defun-function #'xxx-beginning-of-defun)
+(setq-local end-of-defun-function #'xxx-end-of-defun)
#+end_src
-And the same for end-of-defun.
-
* Which-func
You can find the current function by going up the tree and looking for