]> git.eshelyaron.com Git - emacs.git/commitdiff
* admin/notes/tree-sitter/starter-guide (Navigation): Improve demo.
authorYuan Fu <casouri@gmail.com>
Mon, 10 Oct 2022 18:00:51 +0000 (11:00 -0700)
committerYuan Fu <casouri@gmail.com>
Mon, 10 Oct 2022 18:00:51 +0000 (11:00 -0700)
admin/notes/tree-sitter/starter-guide

index 378ff581afa3ce3adbf6fa91de6729c835994f9e..e57360386223094ffaf1319e72acf8728de084ae 100644 (file)
@@ -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