> > 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