diff options
| author | Yuan Fu | 2022-10-10 11:00:51 -0700 |
|---|---|---|
| committer | Yuan Fu | 2022-10-10 11:00:51 -0700 |
| commit | 2f6b017e3dc646d317b56fb453c90aaa44c27089 (patch) | |
| tree | 59bc4ffa2c12d9bd3b016af340b14adb53ce91a5 | |
| parent | 2a762336da4d886a4f1b3ee463fd66393d739309 (diff) | |
| download | emacs-2f6b017e3dc646d317b56fb453c90aaa44c27089.tar.gz emacs-2f6b017e3dc646d317b56fb453c90aaa44c27089.zip | |
* admin/notes/tree-sitter/starter-guide (Navigation): Improve demo.
| -rw-r--r-- | admin/notes/tree-sitter/starter-guide | 38 |
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. | |||
| 350 | Something like this should suffice: | 350 | Something like this should suffice: |
| 351 | 351 | ||
| 352 | #+begin_src elisp | 352 | #+begin_src elisp |
| 353 | (defun xxx-beginning-of-defun (&optional arg) | 353 | (defun js--treesit-beginning-of-defun (&optional arg) |
| 354 | (if (> arg 0) | 354 | (let ((arg (or arg 1))) |
| 355 | ;; Go backward. | 355 | (if (> arg 0) |
| 356 | ;; Go backward. | ||
| 357 | (while (and (> arg 0) | ||
| 358 | (treesit-search-forward-goto | ||
| 359 | "function_definition" 'start nil t)) | ||
| 360 | (setq arg (1- arg))) | ||
| 361 | ;; Go forward. | ||
| 362 | (while (and (< arg 0) | ||
| 363 | (treesit-search-forward-goto | ||
| 364 | "function_definition" 'start)) | ||
| 365 | (setq arg (1+ arg)))))) | ||
| 366 | |||
| 367 | (defun xxx-end-of-defun (&optional arg) | ||
| 368 | (let ((arg (or arg 1))) | ||
| 369 | (if (< arg 0) | ||
| 370 | ;; Go backward. | ||
| 371 | (while (and (< arg 0) | ||
| 372 | (treesit-search-forward-goto | ||
| 373 | "function_definition" 'end nil t)) | ||
| 374 | (setq arg (1+ arg))) | ||
| 375 | ;; Go forward. | ||
| 356 | (while (and (> arg 0) | 376 | (while (and (> arg 0) |
| 357 | (treesit-search-forward-goto | 377 | (treesit-search-forward-goto |
| 358 | "function_definition" 'start nil t)) | 378 | "function_definition" 'end)) |
| 359 | (setq arg (1- arg))) | 379 | (setq arg (1- arg)))))) |
| 360 | ;; Go forward. | ||
| 361 | (while (and (< arg 0) | ||
| 362 | (treesit-search-forward-goto | ||
| 363 | "function_definition" 'start)) | ||
| 364 | (setq arg (1+ arg))))) | ||
| 365 | 380 | ||
| 366 | (setq-local beginning-of-defun-function #'xxx-beginning-of-defun) | 381 | (setq-local beginning-of-defun-function #'xxx-beginning-of-defun) |
| 382 | (setq-local end-of-defun-function #'xxx-end-of-defun) | ||
| 367 | #+end_src | 383 | #+end_src |
| 368 | 384 | ||
| 369 | And the same for end-of-defun. | ||
| 370 | |||
| 371 | * Which-func | 385 | * Which-func |
| 372 | 386 | ||
| 373 | You can find the current function by going up the tree and looking for | 387 | You can find the current function by going up the tree and looking for |