diff --git a/README.md b/README.md index e6817bd..7d6bcee 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +**This is a WIP port of the original ts-fold library to work with Emacs 29's built in treesit.el. It is not fully tested, and will likely be eventually folded into the original library sometime after the Emacs 29 release** + [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) [![JCS-ELPA](https://raw.githubusercontent.com/jcs-emacs/badges/master/elpa/v/ts-fold.svg)](https://jcs-emacs.github.io/jcs-elpa/#/ts-fold) diff --git a/ts-fold-indicators.el b/ts-fold-indicators.el index 467c253..0fb3ee0 100644 --- a/ts-fold-indicators.el +++ b/ts-fold-indicators.el @@ -278,16 +278,16 @@ Argument FOLDED holds folding state; it's a boolean." "Refresh indicators for all folding range." (when ts-fold-indicators-mode (ts-fold--ensure-ts - (when-let* ((node (tsc-root-node tree-sitter-tree)) + (when-let* ((node (treesit-buffer-root-node)) (patterns (seq-mapcat (lambda (fold-range) `((,(car fold-range)) @name)) (alist-get major-mode ts-fold-range-alist) 'vector)) (query (ignore-errors - (tsc-make-query tree-sitter-language patterns))) - (nodes-to-fold (tsc-query-captures query node #'ignore))) + (treesit-query-compile (treesit-node-language node) + (append patterns nil)))) + (nodes-to-fold (treesit-query-capture node query nil nil t))) (ts-fold-indicators--remove-overlays) (thread-last nodes-to-fold - (mapcar #'cdr) (mapc #'ts-fold-indicators--create)))))) (defun ts-fold-indicators--remove-overlays () diff --git a/ts-fold-parsers.el b/ts-fold-parsers.el index d7087b9..fe9b796 100644 --- a/ts-fold-parsers.el +++ b/ts-fold-parsers.el @@ -52,6 +52,7 @@ (declare-function ts-fold-range-ruby-if "ts-fold.el") (declare-function ts-fold-range-rust-macro "ts-fold.el") (declare-function ts-fold-range-elixir "ts-fold.el") +(declare-function ts-fold-range-yaml-object "ts-fold.el") ;; ;; (@* "Parsers" ) @@ -192,7 +193,7 @@ (compound_statement . ts-fold-range-seq) (comment . (lambda (node offset) - (if (string-prefix-p "#" (tsc-node-text node)) + (if (string-prefix-p "#" (treesit-node-text node)) (ts-fold-range-line-comment node offset "#") (ts-fold-range-c-like-comment node offset)))))) @@ -258,5 +259,10 @@ "Rule set for TypeScript." (append (ts-fold-parsers-javascript))) +(defun ts-fold-parsers-yaml () + "Rule set for YAML." + '((block_mapping_pair . ts-fold-range-yaml-object))) + + (provide 'ts-fold-parsers) ;;; ts-fold-parsers.el ends here diff --git a/ts-fold-util.el b/ts-fold-util.el index 07880a5..3508ddc 100644 --- a/ts-fold-util.el +++ b/ts-fold-util.el @@ -46,6 +46,25 @@ (push ov lst))) lst)) +;; +;; (@* "Treesitter") +;; + +(defun ts-fold--get-nth-child (node index) + (nth index (treesit-node-children node))) + +(defun ts-fold--get-nth-named-child (node index) + (nth index (treesit-node-children node t))) + +;; +;; (@* "Folds" ) +;; + +(defun ts-fold--get-fold-func (node) + (let ((fold-alist (alist-get major-mode ts-fold-range-alist)) + (node-symbol (intern (treesit-node-type node)))) + (alist-get node-symbol fold-alist))) + ;; ;; (@* "Face" ) ;; diff --git a/ts-fold.el b/ts-fold.el index 47852ab..6db8c30 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -41,7 +41,6 @@ (require 'subr-x) (require 's) -(require 'tree-sitter) (require 'ts-fold-util) (require 'ts-fold-parsers) @@ -61,7 +60,9 @@ (defcustom ts-fold-range-alist `((agda-mode . ,(ts-fold-parsers-agda)) (c-mode . ,(ts-fold-parsers-c)) + (c-ts-mode . ,(ts-fold-parsers-c)) (c++-mode . ,(ts-fold-parsers-c++)) + (c++-ts-mode . ,(ts-fold-parsers-c++)) (caml-mode . ,(ts-fold-parsers-ocaml)) (csharp-mode . ,(ts-fold-parsers-csharp)) (css-mode . ,(ts-fold-parsers-css)) @@ -72,24 +73,32 @@ (java-mode . ,(ts-fold-parsers-java)) (javascript-mode . ,(ts-fold-parsers-javascript)) (js-mode . ,(ts-fold-parsers-javascript)) + (js-ts-mode . ,(ts-fold-parsers-javascript)) (js2-mode . ,(ts-fold-parsers-javascript)) (js3-mode . ,(ts-fold-parsers-javascript)) (json-mode . ,(ts-fold-parsers-json)) + (json-ts-mode . ,(ts-fold-parsers-json)) (jsonc-mode . ,(ts-fold-parsers-json)) (julia-mode . ,(ts-fold-parsers-julia)) (nix-mode . ,(ts-fold-parsers-nix)) (ocaml-mode . ,(ts-fold-parsers-ocaml)) (php-mode . ,(ts-fold-parsers-php)) (python-mode . ,(ts-fold-parsers-python)) + (python-ts-mode . ,(ts-fold-parsers-python)) (rjsx-mode . ,(ts-fold-parsers-javascript)) - (ruby-mode . ,(ts-fold-parsers-ruby)) + (ruby-ts-mode . ,(ts-fold-parsers-ruby)) (rust-mode . ,(ts-fold-parsers-rust)) (rustic-mode . ,(ts-fold-parsers-rust)) (sh-mode . ,(ts-fold-parsers-bash)) (scala-mode . ,(ts-fold-parsers-scala)) (swift-mode . ,(ts-fold-parsers-swift)) (tuareg-mode . ,(ts-fold-parsers-ocaml)) - (typescript-mode . ,(ts-fold-parsers-typescript))) + (tsx-ts-mode . ,(ts-fold-parsers-typescript)) + (typescript-mode . ,(ts-fold-parsers-typescript)) + (typescript-ts-mode . ,(ts-fold-parsers-typescript)) + (yaml-mode . ,(ts-fold-parsers-yaml)) + (yaml-ts-mode . ,(ts-fold-parsers-yaml)) + ) "An alist of (major-mode . (foldable-node-type . function)). FUNCTION is used to determine where the beginning and end for FOLDABLE-NODE-TYPE @@ -153,11 +162,14 @@ the fold in a cons cell. See `ts-fold-range-python' for an example." (ts-fold-open-all))) (defun ts-fold--tree-sitter-trigger () - "Turn `ts-fold-mode' on and off alongside `tree-sitter-mode' + "Turn `ts-fold-mode' on and off alongside `treesit' when in a mode ts-fold can act on." - (if (and tree-sitter-mode (ts-fold-usable-mode-p)) - (ts-fold-mode 1) - (ts-fold-mode -1))) + (condition-case nil + (if (and (treesit-buffer-root-node) + (ts-fold-usable-mode-p)) + (ts-fold-mode 1) + (ts-fold-mode -1)) + (error nil))) ;;;###autoload (define-minor-mode ts-fold-mode @@ -176,12 +188,12 @@ when in a mode ts-fold can act on." :global t (if global-ts-fold-mode (progn - (add-hook 'tree-sitter-mode-hook #'ts-fold--tree-sitter-trigger) + (add-hook 'prog-mode-hook #'ts-fold--tree-sitter-trigger) ;; try to turn on in all buffers. (dolist (buf (buffer-list)) (with-current-buffer buf (ts-fold--tree-sitter-trigger)))) - (remove-hook 'tree-sitter-mode-hook #'ts-fold--tree-sitter-trigger))) + (remove-hook 'prog-mode-hook #'ts-fold--tree-sitter-trigger))) (defun ts-fold-usable-mode-p (&optional mode) "Return non-nil if `ts-fold' has defined folds for MODE." @@ -200,19 +212,19 @@ Return nil if no valid node is found. This function is borrowed from `tree-sitter-node-at-point'." (let* ((pos (or pos (point))) (mode-ranges (alist-get major-mode ts-fold-range-alist)) - (root (tsc-root-node tree-sitter-tree)) - (node (tsc-get-descendant-for-position-range root pos pos)) + (root (treesit-buffer-root-node)) + (node (treesit-node-descendant-for-range root pos pos)) ;; Used for looping (current node)) - (while (and current (not (alist-get (tsc-node-type current) mode-ranges))) - (setq current (tsc-get-parent current))) + (while (and current (not (ts-fold--get-fold-func current))) + (setq current (treesit-node-parent current))) current)) (defun ts-fold--get-fold-range (node) "Return the beginning (as buffer position) of fold for NODE. Return nil if there is no fold to be made." (when-let* ((fold-alist (alist-get major-mode ts-fold-range-alist)) - (fold-func (alist-get (tsc-node-type node) fold-alist))) + (fold-func (ts-fold--get-fold-func node))) (cond ((functionp fold-func) (funcall fold-func node (cons 0 0))) ((listp fold-func) (funcall (nth 0 fold-func) node (cons (nth 1 fold-func) (nth 2 fold-func)))) (t (user-error "Bad folding function for node"))))) @@ -256,10 +268,11 @@ Return nil otherwise." (defmacro ts-fold--ensure-ts (&rest body) "Run BODY only if `tree-sitter-mode` is enabled." - (declare (indent 0)) - `(if (bound-and-true-p tree-sitter-mode) + (declare (indent 0) + (debug (&rest form))) + `(if (and (functionp 'treesit-buffer-root-node) (treesit-buffer-root-node)) (progn ,@body) - (user-error "Ignored, tree-sitter-mode is not enabled in the current buffer"))) + (message "ts-fold: ignoring, because cannot parse current buffer with treesit"))) ;;;###autoload (defun ts-fold-close (&optional node) @@ -294,8 +307,8 @@ If the current node is not folded or not foldable, do nothing." (interactive) (ts-fold--ensure-ts (when-let* ((node (ts-fold--foldable-node-at-pos)) - (beg (tsc-node-start-position node)) - (end (tsc-node-end-position node))) + (beg (treesit-node-start node)) + (end (treesit-node-end node))) (thread-last (overlays-in beg end) (seq-filter (lambda (ov) (eq (overlay-get ov 'invisible) 'ts-fold))) (mapc #'delete-overlay))))) @@ -305,14 +318,12 @@ If the current node is not folded or not foldable, do nothing." "Fold all foldable syntax nodes in the buffer." (interactive) (ts-fold--ensure-ts - (let* ((node (tsc-root-node tree-sitter-tree)) - (patterns (seq-mapcat (lambda (fold-range) `((,(car fold-range)) @name)) - (alist-get major-mode ts-fold-range-alist) - 'vector)) - (query (tsc-make-query tree-sitter-language patterns)) - (nodes-to-fold (tsc-query-captures query node #'ignore))) + (let* ((node (treesit-buffer-root-node)) + (patterns (mapconcat (lambda (fold-range) (concat "(" (symbol-name (car fold-range)) ") " "@name")) + (alist-get major-mode ts-fold-range-alist) " ")) + (query (treesit-query-compile (treesit-node-language node) patterns)) + (nodes-to-fold (treesit-query-capture node query nil nil t))) (thread-last nodes-to-fold - (mapcar #'cdr) (mapc #'ts-fold-close))))) ;;;###autoload @@ -356,7 +367,7 @@ If the current syntax node is not foldable, do nothing." "Return previous/next sibling node starting from NODE. If NEXT is non-nil, return next sibling. Otherwirse, return previouse sibling." - (if next (tsc-get-next-sibling node) (tsc-get-prev-sibling node))) + (if next (treesit-node-next-sibling node) (treesit-node-prev-sibling node))) (defun ts-fold--continuous-node-prefix (node prefix next) "Iterate through node starting from NODE and compare node-text to PREFIX; @@ -365,11 +376,11 @@ then return the last iterated node. Argument NEXT is a boolean type. If non-nil iterate forward; otherwise iterate in backward direction." (let ((iter-node node) (last-node node) - (last-line (car (tsc-node-start-point node))) line text break + (last-line (treesit-node-start node)) line text break (line-range 1) (last-line-range 1) max-line-range) (while (and iter-node (not break)) - (setq text (tsc-node-text iter-node) - line (car (tsc-node-start-point iter-node)) + (setq text (treesit-node-text iter-node) + line (treesit-node-start iter-node) line-range (1+ (s-count-matches "\n" text)) max-line-range (max line-range last-line-range)) (if (and (ts-fold--in-range-p line (- last-line max-line-range) (+ last-line max-line-range)) @@ -382,14 +393,14 @@ in backward direction." (defun ts-fold--one-liner-node (node) "Helper function to check if NODE is on one line only." - (= (car (aref (tsc-node-range node) 2)) (car (aref (tsc-node-range node) 3)))) + (= (car (aref (treesit-node-start node) 2)) (car (aref (treesit-node-end node) 3)))) (defun ts-fold-range-seq (node offset) "Return the fold range in sequence starting from NODE. Argument OFFSET can be used to tweak the final beginning and end position." - (let ((beg (1+ (tsc-node-start-position node))) - (end (1- (tsc-node-end-position node)))) + (let ((beg (1+ (treesit-node-start node))) + (end (1- (treesit-node-end node)))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-line-comment (node offset prefix) @@ -402,8 +413,8 @@ Argument PREFIX is the comment prefix in string." (when-let* ((first-node (ts-fold--continuous-node-prefix node prefix nil)) (last-node (ts-fold--continuous-node-prefix node prefix t)) (prefix-len (length prefix)) - (beg (+ (tsc-node-start-position first-node) prefix-len)) - (end (tsc-node-end-position last-node))) + (beg (+ (treesit-node-start first-node) prefix-len)) + (end (treesit-node-end last-node))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-block-comment (node offset) @@ -418,7 +429,7 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (let ((text (tsc-node-text node))) + (let ((text (treesit-node-text node))) (if (and (string-match-p "\n" text) (string-prefix-p "/*" text)) (ts-fold-range-block-comment node offset) (if (string-prefix-p "///" text) @@ -434,10 +445,11 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (let* ((named-node (tsc-get-child-by-field node :condition)) - (else (tsc-get-child-by-field node :alternative)) - (beg (tsc-node-end-position named-node)) - (end (1- (tsc-node-start-position else)))) + (let* ((named-node (treesit-node-child-by-field-name node "condition")) + (else (or (treesit-node-child-by-field-name node "alternative") + (treesit-node-child node -1))) + (beg (treesit-node-end named-node)) + (end (1- (treesit-node-start else)))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-c-preproc-ifdef (node offset) @@ -445,10 +457,10 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (when-let* ((named-node (tsc-get-child-by-field node :name)) - (else (tsc-get-child-by-field node :alternative)) - (beg (tsc-node-end-position named-node)) - (end (1- (tsc-node-start-position else)))) + (when-let* ((named-node (treesit-node-child-by-field-name node "name")) + (else (treesit-node-child-by-field-name node "alternative")) + (beg (treesit-node-end named-node)) + (end (1- (treesit-node-start else)))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-c-preproc-elif (node offset) @@ -456,10 +468,10 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (when-let* ((named-node (tsc-get-child-by-field node :condition)) - (else (tsc-get-child-by-field node :alternative)) - (beg (tsc-node-end-position named-node)) - (end (1- (tsc-node-start-position else)))) + (when-let* ((named-node (treesit-node-child-by-field-name node "condition")) + (else (treesit-node-child-by-field-name node "alternative")) + (beg (treesit-node-end named-node)) + (end (1- (treesit-node-start else)))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-c-preproc-else (node offset) @@ -469,8 +481,8 @@ For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." (when-let* ((target "#else") (len (length target)) - (beg (+ (tsc-node-start-position node) len)) - (end (tsc-node-end-position node))) + (beg (+ (treesit-node-start node) len)) + (end (treesit-node-end node))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-html (node offset) @@ -478,9 +490,9 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (let* ((beg (tsc-node-end-position (tsc-get-nth-child node 0))) - (end-node (tsc-get-nth-child node (1- (tsc-count-children node)))) - (end (tsc-node-start-position end-node))) + (let* ((beg (treesit-node-end (ts-fold--get-nth-child node 0))) + (end-node (ts-fold--get-nth-child node (1- (treesit-node-child-count node)))) + (end (treesit-node-start end-node))) (ts-fold--cons-add (cons beg end) offset))) ;;+ OCaml @@ -492,11 +504,11 @@ For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." (unless (ts-fold--one-liner-node node) (when-let* - ((text (tsc-node-text node)) + ((text (treesit-node-text node)) (beg (if (string-prefix-p "(* " text) - (+ 2 (tsc-node-start-position node)) - (+ 3 (tsc-node-start-position node)))) - (end (- (tsc-node-end-position node) 2))) + (+ 2 (treesit-node-start node)) + (+ 3 (treesit-node-start node)))) + (end (- (treesit-node-end node) 2))) (ts-fold--cons-add (cons beg end) offset)))) (defun ts-fold-range-ocaml-module-definition (node offset) @@ -506,11 +518,11 @@ For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." (unless (ts-fold--one-liner-node node) (when-let* - ((module-binding (tsc-get-nth-named-child node 0)) - (body (tsc-get-child-by-field module-binding :body)) + ((module-binding (ts-fold--get-nth-child node 0)) + (body (treesit-node-child-by-field-name module-binding "body")) ;; body is struct ... end - (beg (+ 6 (tsc-node-start-position body))) - (end (- (tsc-node-end-position node) 3))) + (beg (+ 6 (treesit-node-start body))) + (end (- (treesit-node-end node) 3))) (ts-fold--cons-add (cons beg end) offset)))) (defun ts-fold-range-ocaml-type-definition (node offset) @@ -520,17 +532,17 @@ For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." (unless (ts-fold--one-liner-node node) (when-let* - ((type-definition (tsc-get-nth-named-child node 0)) - (body (tsc-get-child-by-field type-definition :body)) - (text (tsc-node-text (tsc-get-nth-child body 0))) + ((type-definition (ts-fold--get-nth-named-child node 0)) + (body (treesit-node-child-by-field-name type-definition "body")) + (text (treesit-node-text (ts-fold--get-nth-child body 0))) (beg (if (string-equal "{" text) - (1+ (tsc-node-start-position body)) - (tsc-node-end-position (tsc-get-prev-sibling body)))) + (1+ (treesit-node-start body)) + (treesit-node-end (treesit-node-prev-sibling body)))) (end (if (string-equal "{" text) - (1- (tsc-node-end-position node)) - (tsc-node-end-position node)))) + (1- (treesit-node-end node)) + (treesit-node-end node)))) (ts-fold--cons-add (cons beg end) offset)))) (defun ts-fold-range-ocaml-value-definition (node offset) @@ -540,10 +552,10 @@ For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." (unless (ts-fold--one-liner-node node) (when-let* - ((let-binding (tsc-get-nth-named-child node 0)) - (body (tsc-get-child-by-field let-binding :body)) - (beg (tsc-node-end-position (tsc-get-prev-sibling body))) - (end (tsc-node-end-position node))) + ((let-binding (ts-fold--get-nth-named-child node 0)) + (body (treesit-node-child-by-field-name let-binding "body")) + (beg (treesit-node-end (treesit-node-prev-sibling body))) + (end (treesit-node-end node))) (ts-fold--cons-add (cons beg end) offset)))) ;;- OCaml @@ -553,13 +565,13 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (when-let* ((named-node (or (tsc-get-child-by-field node :superclasses) - (tsc-get-child-by-field node :return_type) - (tsc-get-child-by-field node :parameters) - (tsc-get-child-by-field node :name))) + (when-let* ((named-node (or (treesit-node-child-by-field-name node "superclasses") + (treesit-node-child-by-field-name node "return_type") + (treesit-node-child-by-field-name node "parameters") + (treesit-node-child-by-field-name node "name"))) ;; the colon is an anonymous node after return_type or parameters node - (beg (tsc-node-end-position (tsc-get-next-sibling named-node))) - (end (tsc-node-end-position node))) + (beg (treesit-node-end (treesit-node-next-sibling named-node))) + (end (treesit-node-end node))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-ruby-class-def (node offset) @@ -567,11 +579,11 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (when-let* ((named-node (or (tsc-get-child-by-field node :superclass) - (tsc-get-child-by-field node :parameters) - (tsc-get-child-by-field node :name))) - (beg (tsc-node-end-position named-node)) - (end (tsc-node-end-position node))) + (when-let* ((named-node (or (treesit-node-child-by-field-name node "superclass") + (treesit-node-child-by-field-name node "parameters") + (treesit-node-child-by-field-name node "name"))) + (beg (treesit-node-end named-node)) + (end (treesit-node-end node))) (ts-fold--cons-add (cons beg (- end 3)) offset))) (defun ts-fold-range-ruby-if (node offset) @@ -579,8 +591,8 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (when-let ((beg (tsc-node-start-position node)) - (end (tsc-node-end-position node))) + (when-let ((beg (treesit-node-start node)) + (end (treesit-node-end node))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-rust-macro (node offset) @@ -588,11 +600,11 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (when-let* ((children (tsc-count-children node)) - (last_bracket (tsc-get-nth-child node (- children 1))) - (first_bracket (tsc-get-nth-child node 2)) - (beg (tsc-node-start-position first_bracket)) - (end (1+ (tsc-node-start-position last_bracket)))) + (when-let* ((children (treesit-node-child-count node)) + (last_bracket (ts-fold--get-nth-child node (- children 1))) + (first_bracket (ts-fold--get-nth-child node 2)) + (beg (treesit-node-start first_bracket)) + (end (1+ (treesit-node-start last_bracket)))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-elixir (node offset) @@ -600,11 +612,11 @@ more information." For arguments NODE and OFFSET, see function `ts-fold-range-seq' for more information." - (when-let* ((children (tsc-count-children node)) - (end_child (tsc-get-nth-child node (- children 1))) - (do_child (tsc-get-nth-child node 1)) - (beg (tsc-node-start-position do_child)) - (end (tsc-node-start-position end_child))) + (when-let* ((children (treesit-node-child-count node)) + (end_child (ts-fold--get-nth-child node (- children 1))) + (do_child (ts-fold--get-nth-child node 1)) + (beg (treesit-node-start do_child)) + (end (treesit-node-start end_child))) (ts-fold--cons-add (cons beg end) offset))) (defun ts-fold-range-julia (node offset) @@ -613,11 +625,24 @@ more information." It excludes the NODE's first child and the `end' keyword. For argument OFFSET, see function `ts-fold-range-seq' for more information." - (let* ((identifier (tsc-get-nth-named-child node 0)) - (end-position (byte-to-position (aref (tsc-node-range identifier) 1))) - (start-position (byte-to-position (aref (tsc-node-range node) 0))) + (let* ((identifier (ts-fold--get-nth-named-child node 0)) + (end-position (treesit-node-end identifier)) + (start-position (treesit-node-start node)) (fold-begin (1- (- end-position start-position)))) (ts-fold-range-seq node (ts-fold--cons-add (cons fold-begin -2) offset)))) +(defun ts-fold-range-yaml-object (node offset) + "Define fold range for YAML object. + +For arguments NODE and OFFSET, see function `ts-fold-range-seq' for +more information. + +Excludes the NODE's first child, which is the key." + (let* ((beg (treesit-node-end (ts-fold--get-nth-child node 0))) + (end (treesit-node-end node))) + (ts-fold--cons-add (cons beg end) offset))) + + + (provide 'ts-fold) ;;; ts-fold.el ends here