From: "João Paulo Labegalini de Carvalho" <jaopaulolc@gmail.com>
To: Alan Mackenzie <acm@muc.de>
Cc: Yuan Fu <casouri@gmail.com>,
Stefan Monnier <monnier@iro.umontreal.ca>,
emacs-devel@gnu.org
Subject: Re: Code navigation for sh-mode with Tree-sitter
Date: Tue, 6 Dec 2022 14:55:55 -0700 [thread overview]
Message-ID: <CAGjvy28hjW9ONfz8vJ4tLGTDOMjgO2j=q2-azG-pf_xG2AxpnQ@mail.gmail.com> (raw)
In-Reply-To: <CAGjvy2-0ZQF0x0cXbw_tHYAqimO_fb7m7vHXwSc=rMMxGxzihg@mail.gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 2762 bytes --]
I completed my patch to add basic navigation to bash-ts-mode.
It works consistently when the new navigation
functions (`sh-mode--treesit-beginning-of-defun' and
`sh-mode--treesit-end-of-defun') are bound directly to C-M-a/e. However, if
those functions are bound to `beginning-of-defun-function' and
`end-of-defun-function', C-M-e does not work with negative arguments inside
of functions.
In the example below C-u - 1 C-M-e goes to the beginning of foo and not to
end of bar when point is inside foo, and anywhere after bar:
function foo {
function bar {
echo "Hello from bar"
}
if [[ $1 > 0 ]]; then
return 42
else
return 24
fi
echo bar
}
Any comments and feedback on the patch are welcomed.
On Tue, Dec 6, 2022 at 2:46 PM João Paulo Labegalini de Carvalho <
jaopaulolc@gmail.com> wrote:
> > > Should we make it configurable, then? A variable that makes
>> > > tree-sitter defun navigation switch between two modes: top-level only
>> > > and not top-level only.
>>
>> In CC Mode, it has been configurable via the user option c-defun-tactic
>> for somewhere between ten and fifteen years. When c-defun-tactic is t,
>> C-M-a/e go to the start/end of the top level defuns. When it is the
>> symbol go-outward, C-M-a/e move to the next start/end of defun, if any,
>> at the current level of class/namespace nesting, and move outwards to
>> the next level of class/namespace nesting when a class/namespace boundary
>> is reached.
>>
>
> The 'go-outward behavior is the one I have implemented.
>
> I don't remember any complaints about this mechanism.
>>
>> > And for functions nested in a class: if you type C-M-e at the beginning
>> > of a class, should it go to the end of the first function in that
>> > class, or should it go to the end of the class? Right now because of
>> > how end-of-defun works, it will jump to the end of the class if point
>> > is at the beginning of the class (1), and jump to the first function if
>> > point is before the beginning of the class (2).
>>
>> This doesn't seem sensible.
>>
>
> I tend to agree. I would expect it to go to the nearest node (either class
> or function) and not bypass a class node in favor of function nodes.
>
> --
> João Paulo L. de Carvalho
> Ph.D Computer Science | IC-UNICAMP | Campinas , SP - Brazil
> Postdoctoral Research Fellow | University of Alberta | Edmonton, AB -
> Canada
> joao.carvalho@ic.unicamp.br
> joao.carvalho@ualberta.ca
>
--
João Paulo L. de Carvalho
Ph.D Computer Science | IC-UNICAMP | Campinas , SP - Brazil
Postdoctoral Research Fellow | University of Alberta | Edmonton, AB - Canada
joao.carvalho@ic.unicamp.br
joao.carvalho@ualberta.ca
[-- Attachment #1.2: Type: text/html, Size: 4132 bytes --]
[-- Attachment #2: 0001-Basic-navigation-for-bash-ts-mode.patch --]
[-- Type: text/x-patch, Size: 8933 bytes --]
From e3691d9261233241d349dd4d39b72ddb144f6f1b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20P=2E=20L=2E=20de=20Carvalho?=
<jaopaulolc@gmail.com>
Date: Sat, 3 Dec 2022 12:55:27 -0700
Subject: [PATCH] Basic navigation for bash-ts-mode
Enables navigation to beginning/end of function in bash-ts-mode.
* lisp/progmodes/sh-script.el (bash-ts-mode): enable navegation via
new internal navigation functions.
(sh-mode--treesit-beginning-of-defun)
(sh-mode--treesit-end-of-defun)
(sh-mode--treesit-defun-p)
(sh-mode--treesit-not-sc-p)
(sh-mode--treesit-next-sibling-defun)
(sh-mode--treesit-prev-sibling-defun): New functions.
(sh-mode--treesit-parent-defun): New macro.
---
lisp/progmodes/sh-script.el | 171 +++++++++++++++++++++++++++++++++++-
1 file changed, 170 insertions(+), 1 deletion(-)
diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index 408ebfc045..751e5d6993 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -1619,7 +1619,12 @@ bash-ts-mode
( bracket delimiter misc-punctuation operator)))
(setq-local treesit-font-lock-settings
sh-mode--treesit-settings)
- (treesit-major-mode-setup)))
+ (setq-local treesit-defun-type-regexp "function_definition")
+ (treesit-major-mode-setup)
+ ;; (keymap-set bash-ts-mode-map "C-M-a" #'sh-mode--treesit-beginning-of-defun)
+ ;; (keymap-set bash-ts-mode-map "C-M-e" #'sh-mode--treesit-end-of-defun)
+ (setq-local beginning-of-defun-function #'sh-mode--treesit-beginning-of-defun)
+ (setq-local end-of-defun-function #'sh-mode--treesit-end-of-defun)))
(advice-add 'bash-ts-mode :around #'sh--redirect-bash-ts-mode
;; Give it lower precedence than normal advice, so other
@@ -3364,5 +3369,169 @@ sh-mode--treesit-settings
'((["$"]) @font-lock-misc-punctuation-face))
"Tree-sitter font-lock settings for `sh-mode'.")
+\f
+;;; Tree-sitter navigation
+
+(defun sh-mode--treesit-defun-p (node)
+ "Return t if NODE is a function and nil otherwise."
+ (string-match treesit-defun-type-regexp
+ (treesit-node-type node)))
+
+(defun sh-mode-treesit-not-cs-p (node)
+ "Returns t if NODE is *not* a compound-statement
+and nil otherwise."
+ (lambda (p)
+ (not (string-match "compound_statement"
+ (treesit-node-type p)))))
+
+(defun sh-mode--treesit-next-sibling-defun (node)
+ "Returns the next sibling function of NODE, if any, or nil."
+ (let ((sibling node))
+ (while (and sibling
+ (not (sh-mode--treesit-defun-p sibling)))
+ (setq sibling (treesit-node-next-sibling sibling)))
+ sibling))
+
+(defun sh-mode--treesit-prev-sibling-defun (node)
+ "Returns the previous sibling function of NODE, if any, or nil."
+ (let ((sibling (treesit-node-prev-sibling node)))
+ (while (and sibling
+ (not (sh-mode--treesit-defun-p sibling)))
+ (setq sibling (treesit-node-prev-sibling sibling)))
+ sibling))
+
+(defmacro sh-mode--treesit-parent-defun (node)
+ "Returns nearest function-node that surrounds NODE, if any, or nil.
+
+This macro can be used to determine if NODE is within a function. If
+so, the macro evaluates to the nearest function-node and parent of NODE.
+Otherwise it evaluates to NIL."
+ `(treesit-parent-until ,node 'sh-mode--treesit-defun-p))
+
+(defmacro sh-mode--treesit-oldest-parent-in-defun (node)
+ "Returns oldest parent of NODE in common function, if any, or NIL.
+
+This function returns the oldest parent of NODE such that the common
+parent is the nearest function-node."
+ `(treesit-parent-while ,node 'sh-mode--treesit-not-cp-p))
+
+(defun sh-mode--treesit-beginning-of-defun (&optional arg)
+ "Tree-sitter `beginning-of-defun' function.
+ARG is the same as in `beginning-of-defun'.
+
+This function works the same way the non-tree-sitter
+`beginning-of-defun' when point is not within a function. It diverges
+from `beginning-of-defun' when inside a function by moving point to
+the beginning of the closest enclosing function when ARG is positive.
+When ARG is negative and inside a function, point is moved to the
+beggining of closest sibling function, recursively."
+ (interactive "P")
+ (let ((arg (or arg 1))
+ (target nil)
+ (curr (treesit-node-at (point)))
+ (function treesit-defun-type-regexp))
+ (if (> arg 0)
+ ;; Go backward.
+ (while (and (> arg 0) curr)
+ (if (string= (treesit-node-type curr) "function")
+ (setq curr (treesit-node-parent curr)))
+ (setq target (sh-mode--treesit-parent-defun curr))
+ (unless target
+ (let ((maybe-target nil))
+ (setq maybe-target (treesit-search-forward curr
+ function
+ t))
+ (setq target (or (treesit-node-top-level maybe-target)
+ maybe-target))))
+ (when target
+ (setq curr target))
+ (setq arg (1- arg)))
+ ;; Go forward.
+ (while (and (< arg 0) curr)
+ (setq target nil)
+ (if (sh-mode--treesit-defun-p curr)
+ (setq curr (treesit-node-at
+ (treesit-node-end
+ (treesit-node-parent curr)))))
+ (let ((parent-defun (sh-mode--treesit-parent-defun curr)))
+ (while (and (not target)
+ parent-defun)
+ (setq target (sh-mode--treesit-next-sibling-defun curr))
+ (unless target
+ (setq curr (treesit-node-next-sibling parent-defun))
+ (setq parent-defun
+ (sh-mode--treesit-parent-defun curr))))
+ (unless target
+ (let ((maybe-target nil))
+ (setq maybe-target (treesit-search-forward curr
+ function))
+ (setq target (or (treesit-node-top-level maybe-target)
+ maybe-target))))
+ (when target
+ (setq curr target)))
+ (setq arg (1+ arg))))
+ (when target
+ (goto-char (treesit-node-start target)))))
+
+(defun sh-mode--treesit-end-of-defun (&optional arg)
+ "Tree-sitter `end-of-defun' function.
+
+This function works the same way the non-tree-sitter
+`end-of-defun' when point is not within a function. It diverges
+from `end-of-defun' when inside a function by moving point to
+the end of the closest enclosing function when ARG is positive.
+When ARG is negative and inside a function, point is moved to the
+end of closest sibling function, recursively."
+ (interactive "P")
+ (let ((arg (or arg 1))
+ (curr (treesit-node-at (point)))
+ (target nil)
+ (function treesit-defun-type-regexp))
+ (if (> arg 0)
+ ;; Go forward.
+ (while (and (> arg 0) curr)
+ (setq target (sh-mode--treesit-parent-defun curr))
+ (unless target
+ (setq target (treesit-search-forward curr
+ function))
+ (when (and target
+ (sh-mode--treesit-parent-defun target))
+ (setq target (treesit-node-top-level target))))
+ (when target
+ (setq curr target))
+ (setq arg (1- arg)))
+ ;; Go backward.
+ (while (and (< arg 0) curr)
+ (setq target nil)
+ (if (sh-mode--treesit-parent-defun curr)
+ (setq curr
+ (or (sh-mode--treesit-oldest-parent-in-defun curr)
+ curr)))
+ (let* ((prev-defun (sh-mode--treesit-prev-sibling-defun curr))
+ (prev-defun-end (treesit-node-at
+ (treesit-node-end prev-defun))))
+ (if (and prev-defun (treesit-node-eq curr prev-defun-end))
+ (setq curr prev-defun)))
+ (let ((parent-defun (sh-mode--treesit-parent-defun curr)))
+ (while (and (not target)
+ parent-defun)
+ (setq target (sh-mode--treesit-prev-sibling-defun curr))
+ (unless target
+ (setq curr (treesit-node-prev-sibling parent-defun))
+ (setq parent-defun
+ (sh-mode--treesit-parent-defun curr))))
+ (unless target
+ (let ((maybe-target nil))
+ (setq maybe-target (treesit-search-forward curr
+ function
+ t))
+ (setq target (or (treesit-node-top-level maybe-target)
+ maybe-target))))
+ (when target
+ (setq curr target)))
+ (setq arg (1+ arg))))
+ (when target
+ (goto-char (treesit-node-end target)))))
+
(provide 'sh-script)
;;; sh-script.el ends here
--
2.31.1
next prev parent reply other threads:[~2022-12-06 21:55 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-03 20:23 Code navigation for sh-mode with Tree-sitter João Paulo Labegalini de Carvalho
2022-12-03 21:46 ` Alan Mackenzie
2022-12-05 15:24 ` João Paulo Labegalini de Carvalho
2022-12-05 20:12 ` Stefan Monnier
2022-12-05 21:29 ` Alan Mackenzie
2022-12-05 21:56 ` Stefan Monnier
2022-12-06 15:51 ` João Paulo Labegalini de Carvalho
2022-12-06 16:48 ` Stefan Monnier
2022-12-06 21:04 ` Yuan Fu
2022-12-06 21:08 ` Yuan Fu
2022-12-06 21:40 ` Alan Mackenzie
2022-12-06 21:46 ` João Paulo Labegalini de Carvalho
2022-12-06 21:55 ` João Paulo Labegalini de Carvalho [this message]
2022-12-06 22:35 ` Stefan Monnier
2022-12-06 22:41 ` João Paulo Labegalini de Carvalho
2022-12-06 22:57 ` Stefan Monnier
2022-12-06 23:43 ` João Paulo Labegalini de Carvalho
2022-12-06 23:50 ` Stefan Monnier
2022-12-07 1:12 ` João Paulo Labegalini de Carvalho
2022-12-07 17:20 ` João Paulo Labegalini de Carvalho
2022-12-10 4:58 ` Yuan Fu
2022-12-13 4:55 ` Yuan Fu
2022-12-13 16:00 ` João Paulo Labegalini de Carvalho
2022-12-13 5:20 ` New defun navigation for tree-sitter (Was: Code navigation for sh-mode with Tree-sitter) Yuan Fu
2022-12-13 16:11 ` João Paulo Labegalini de Carvalho
2022-12-13 16:38 ` Eli Zaretskii
2022-12-13 18:03 ` João Paulo Labegalini de Carvalho
2022-12-13 18:07 ` Yuan Fu
2022-12-13 18:48 ` João Paulo Labegalini de Carvalho
2022-12-13 18:56 ` Yuan Fu
2022-12-13 19:46 ` João Paulo Labegalini de Carvalho
2022-12-16 1:49 ` Yuan Fu
2022-12-16 16:24 ` João Paulo Labegalini de Carvalho
2022-12-17 23:32 ` Yuan Fu
2022-12-07 0:41 ` Code navigation for sh-mode with Tree-sitter Yuan Fu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAGjvy28hjW9ONfz8vJ4tLGTDOMjgO2j=q2-azG-pf_xG2AxpnQ@mail.gmail.com' \
--to=jaopaulolc@gmail.com \
--cc=acm@muc.de \
--cc=casouri@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).