From: Yuan Fu Date: Thu, 27 Oct 2022 01:31:07 +0000 (-0700) Subject: * lisp/treesit.el (treesit-node-top-level-p): New argument TYPE. X-Git-Tag: emacs-29.0.90~1779 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=5532ae81cff138a2f6d30c3541210311a4894ac1;p=emacs.git * lisp/treesit.el (treesit-node-top-level-p): New argument TYPE. --- diff --git a/lisp/treesit.el b/lisp/treesit.el index f07fe7d609c..5c89a63bf61 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -195,15 +195,19 @@ that language in the current buffer, and use that." (treesit-buffer-root-node parser-or-lang)))) (treesit-node-descendant-for-range root beg (or end beg) named))) -(defun treesit-node-top-level-p (node) +(defun treesit-node-top-level-p (node &optional type) "Return non-nil if NODE is top-level, nil otherwise. Being top-level means there is no parent of NODE that has the -same type." +same type. + +If TYPE is non-nil, match each parent's type with TYPE as a regexp." (when node (catch 'term - (let ((type (treesit-node-type node))) + (let ((plain-type (treesit-node-type node))) (while (setq node (treesit-node-parent node)) - (when (equal (treesit-node-type node) type) + (when (if type + (string-match-p type (treesit-node-type node)) + (equal (treesit-node-type node) plain-type)) (throw 'term nil)))) t)))