From: Eli Zaretskii Date: Sat, 30 Dec 2023 09:51:17 +0000 (-0500) Subject: Merge from origin/emacs-29 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=3b7198dc4703671fa8b00ed3bf159cb56d4d7ba3;p=emacs.git Merge from origin/emacs-29 53031528725 Revert "Fix treesit-node-field-name and friends (bug#66674)" fa0bb88302b ; * src/buffer.c (syms_of_buffer) : Do... 44517037aed ; Fix typo ccf46acefd2 ; Fix last change. c86b039dffc ; * etc/DEBUG: Improve advice for debugging native-compil... 9afba605bbc Explain status "r" in `epa-list-keys` 62714221968 ; * lisp/dired.el (dired--make-directory-clickable): Refo... fcbb0044899 Fix mouse clicks on directory line in Dired be8a7155b48 Fix 'split-root-window-right' and 'split-root-window-below' eb19984c4db Mark icalendar.el as maintained by emacs-devel 03dc914fd37 ; Fix footnotes in ELisp Intro manual ceacf753958 Fix usage of `setq-default' and offer more suggestions 2701da0eee5 Fix python-ts-mode triple quote syntax (bug#67262) 683c7c96871 Increment parser timestamp when narrowing changes (bug#67... 8ae42c825e1 ruby-ts-mode: Fix indentation for string_array closer 9cfa498e0ab treesit-major-mode-setup: Use 'treesit--syntax-propertize... da2e440462b ruby-ts-mode: Fix an out-of-bounds error with heredoc at eob 6ea507296a7 Correctly refontify changed region in tree-sitter modes (... --- 3b7198dc4703671fa8b00ed3bf159cb56d4d7ba3 diff --cc lisp/treesit.el index c6b9d8ff4bc,264b95dc3a3..d4857dea72e --- a/lisp/treesit.el +++ b/lisp/treesit.el @@@ -393,88 -367,9 +392,88 @@@ If NAMED is non-nil, count named child (defun treesit-node-field-name (node) "Return the field name of NODE as a child of its parent." (when-let ((parent (treesit-node-parent node)) - (idx (treesit-node-index node t))) + (idx (treesit-node-index node))) (treesit-node-field-name-for-child parent idx))) +(defun treesit-node-get (node instructions) + "Get things from NODE by INSTRUCTIONS. + +This is a convenience function that chains together multiple node +accessor functions together. For example, to get NODE's parent's +next sibling's second child's text, call + + (treesit-node-get node + \\='((parent 1) + (sibling 1 nil) + (child 1 nil) + (text nil))) + +INSTRUCTION is a list of INSTRUCTIONs of the form (FN ARG...). +The following FN's are supported: + +\(child IDX NAMED) Get the IDX'th child +\(parent N) Go to parent N times +\(field-name) Get the field name of the current node +\(type) Get the type of the current node +\(text NO-PROPERTY) Get the text of the current node +\(children NAMED) Get a list of children +\(sibling STEP NAMED) Get the nth prev/next sibling, negative STEP + means prev sibling, positive means next + +Note that arguments like NAMED and NO-PROPERTY can't be omitted, +unlike in their original functions." + (declare (indent 1)) + (while (and node instructions) + (pcase (pop instructions) + ('(field-name) (setq node (treesit-node-field-name node))) + ('(type) (setq node (treesit-node-type node))) + (`(child ,idx ,named) (setq node (treesit-node-child node idx named))) + (`(parent ,n) (dotimes (_ n) + (setq node (treesit-node-parent node)))) + (`(text ,no-property) (setq node (treesit-node-text node no-property))) + (`(children ,named) (setq node (treesit-node-children node named))) + (`(sibling ,step ,named) + (dotimes (_ (abs step)) + (setq node (if (> step 0) + (treesit-node-next-sibling node named) + (treesit-node-prev-sibling node named))))))) + node) + +(defun treesit-node-enclosed-p (smaller larger &optional strict) + "Return non-nil if SMALLER is enclosed in LARGER. +SMALLER and LARGER can be either (BEG . END) or a node. + +Return non-nil if LARGER's start <= SMALLER's start and LARGER's +end <= SMALLER's end. + +If STRICT is t, compare with < rather than <=. + +If STRICT is \\='partial, consider LARGER encloses SMALLER when +at least one side is strictly enclosing." + (unless (and (or (consp larger) (treesit-node-p larger)) + (or (consp smaller) (treesit-node-p smaller))) + (signal 'wrong-type-argument '((or cons treesit-node)))) + (let ((larger-start (if (consp larger) + (car larger) + (treesit-node-start larger))) + (larger-end (if (consp larger) + (cdr larger) + (treesit-node-end larger))) + (smaller-start (if (consp smaller) + (car smaller) + (treesit-node-start smaller))) + (smaller-end (if (consp smaller) + (cdr smaller) + (treesit-node-end smaller)))) + (pcase strict + ('t (and (< larger-start smaller-start) + (< smaller-end larger-end))) + ('partial (and (or (not (eq larger-start smaller-start)) + (not (eq larger-end smaller-end))) + (<= larger-start smaller-start + smaller-end larger-end))) + (_ (<= larger-start smaller-start smaller-end larger-end))))) + ;;; Query API supplement (defun treesit-query-string (string query language)