unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "João Paulo Labegalini de Carvalho" <jaopaulolc@gmail.com>
To: Yuan Fu <casouri@gmail.com>
Cc: Stefan Monnier <monnier@iro.umontreal.ca>,
	Alan Mackenzie <acm@muc.de>,
	emacs-devel@gnu.org
Subject: Re: Code navigation for sh-mode with Tree-sitter
Date: Tue, 13 Dec 2022 09:00:44 -0700	[thread overview]
Message-ID: <CAGjvy29B_KTp4ouyNVxOcVt32U-FtCa1Jt8HLGjvYL9nL0v2dA@mail.gmail.com> (raw)
In-Reply-To: <F1B08341-D782-411E-BE6D-A2A6185C6BF4@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3234 bytes --]

>
> Ok, I fianlly finished the defun navigation work, thanks to Alan’s
> suggestion, your experiment, and honorable sacrifices of my many brain
> cells. Now tree-sitter should give you both nested and top-level defun
> navigation for free, provided that the major mode sets
> treesit-defun-type-regexp. I’ll start a new thread about it.
>
> I hope you can try and see if it works for bash-ts-mode, when you have
> time. You should only need to apply the attached patch and (goto-char
> (treesit--navigate-defun (point) …)) should just work. You can switch
> between top-level/nested with treesit-defun-tactic.
>

Cool. Thanks for all the work. I will test it and report my experience in
the other thread.


>
> Anyway, here is some minor points of the patch:
>
> +;;; 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)
> +  "Return t if NODE is *not* a compound-statement and nil otherwise."
> +  (lambda (p)
> +    (not (string-match "compound_statement"
> +                       (treesit-node-type p)))))
>
> We probably want this to be an internal function, too, right?
>

I agree. Those are generic enough to be useful to other use cases.

+(defmacro sh-mode--treesit-parent-defun (node)
> +  "Return 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)
> +  "Return 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))
>
> I'd prefer we use functions when functions will do, unless you have
> particular reasons to use macros (for performance?).


Yes, I was thinking about performance but out of intuition and not evidence.


> Diff artifact?
>

Yes, my bad.

Looks good to me, but I didn't scrutinize it line-by-line. If the new
> system works well, bash-ts-mode (and other major modes) wouldn't need to
> implemente its own navigation function. Sorry for not end up using your
> hard work, but your work definitely helped me to implement the more general
> version of defun navigation!
>

No problem at all. Trying things out has been a great opportunity for me to
learn. Thank you for taking the time to review my patch.

Regarding the new navigation functions the do work as documented for
bash-ts-mode. However, the overall behavior feels awkward to me. I will
elaborate on the new thread.

-- 
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 #2: Type: text/html, Size: 4478 bytes --]

  reply	other threads:[~2022-12-13 16:00 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
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 [this message]
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=CAGjvy29B_KTp4ouyNVxOcVt32U-FtCa1Jt8HLGjvYL9nL0v2dA@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).