]> git.eshelyaron.com Git - emacs.git/commitdiff
Make treesit-defun-prefer-top-level more flexible
authorYuan Fu <casouri@gmail.com>
Wed, 30 Nov 2022 23:05:07 +0000 (15:05 -0800)
committerYuan Fu <casouri@gmail.com>
Thu, 1 Dec 2022 00:57:08 +0000 (16:57 -0800)
* doc/lispref/positions.texi (List Motion): Update manual.
* lisp/treesit.el (treesit-defun-prefer-top-level): Update docstring.
(treesit--defun-maybe-top-level): Change to accept new format.

doc/lispref/positions.texi
lisp/treesit.el

index a01e568de07dbeb386acd1dbde4cacc2d9a3c2a3..c065cc11e2524a1f4e8a6b3635b43e6a03d95317 100644 (file)
@@ -850,6 +850,23 @@ For example, @code{python-mode} sets this variable to a regexp that
 matches either @code{function_definition} or @code{class_definition}.
 @end defvar
 
+@defvar treesit-defun-prefer-top-level
+If this variable is non-@code{nil}, Emacs skips nested defun and
+prefers the top-level defun.
+
+In some languages, a defun could be nested in another one.  Normally
+Emacs stops at the first defun it encounters.  If this variable's
+value is @code{t}, whenever Emacs finds a defun node, it tries to go
+up the parse tree and find the top-level defun.
+
+This variable can also be a list of cons cells of the form
+@w{@code{(@var{from} . @var{to}))}}, where @var{from} and @var{to} are
+regexp matching tree-sitter node types.  When Emacs finds a defun node
+whose type matches any of the @var{from} regexp in the list, Emacs
+then tries to go up the parse tree to find the top-level node matching
+the corresponding @var{to} regexp.
+@end defvar
+
 @node Skipping Characters
 @subsection Skipping Characters
 @cindex skipping characters
index 8f092f475de68a11cefe69410542eb2460fc8f49..dd7895683ff01116d52481dabb1eec54bb4dbc69 100644 (file)
@@ -1554,20 +1554,18 @@ For example, \"(function|class)_definition\".
 This is used by `treesit-beginning-of-defun' and friends.")
 
 (defvar-local treesit-defun-prefer-top-level nil
-  "When non-nil, `treesit-beginning-of-defun' prefers top-level defun.
+  "When non-nil, Emacs prefers top-level defun.
 
-In some languages, a defun (function, class, struct) could be
-nested in another one.  Normally `treesit-beginning-of-defun'
-just finds the first defun it encounter.  If this variable's
-value is t, `treesit-beginning-of-defun' tries to find the
-top-level defun, and ignores nested ones.
+In some languages, a defun could be nested in another one.
+Normally Emacs stops at the first defun it encounters.  If this
+variable's value is t, Emacs tries to find the top-level defun,
+and ignores nested ones.
 
-This variable can also be a list of tree-sitter node type
-regexps.  Then, when `treesit-beginning-of-defun' finds a defun
-node and that node's type matches one in the list,
-`treesit-beginning-of-defun' finds the top-level node matching
-that particular regexp (as opposed to any node matched by
-`treesit-defun-type-regexp').")
+This variable can also be a list of cons cells of the form (FROM
+. TO), where FROM and TO are tree-sitter node type regexps.  When
+Emacs finds a defun node whose type matches any of the FROM
+regexp in the list, Emacs then tries to find the top-level node
+matching the corresponding TO regexp.")
 
 (defun treesit--defun-maybe-top-level (node)
   "Maybe return the top-level equivalent of NODE.
@@ -1579,9 +1577,11 @@ For the detailed semantic see `treesit-defun-prefer-top-level'."
             node))
     ((pred consp)
      (cl-loop
-      for re in treesit-defun-prefer-top-level
-      if (string-match-p re (treesit-node-type node))
-      return (or (treesit-node-top-level node re)
+      for con in treesit-defun-prefer-top-level
+      for from = (car con)
+      for to = (cdr con)
+      if (string-match-p from (treesit-node-type node))
+      return (or (treesit-node-top-level node to)
                  node)
       finally return node))))