diff --git a/treesit-fold-parsers.el b/treesit-fold-parsers.el index b8c2f2f..4d80b93 100644 --- a/treesit-fold-parsers.el +++ b/treesit-fold-parsers.el @@ -72,6 +72,8 @@ (declare-function treesit-fold-range-lua-do-loop "treesit-fold.el") (declare-function treesit-fold-range-lua-repeat "treesit-fold.el") (declare-function treesit-fold-range-make-recipe "treesit-fold.el") +(declare-function treesit-fold-markdown-heading "treesit-fold.el") +(declare-function treesit-fold-markdown-code-block "treesit-fold.el") (declare-function treesit-fold-range-matlab-function "treesit-fold.el") (declare-function treesit-fold-range-matlab-statements "treesit-fold.el") (declare-function treesit-fold-range-matlab-blocks "treesit-fold.el") @@ -450,7 +452,8 @@ (defun treesit-fold-parsers-markdown () "Rule set for Markdown." - '((fenced_code_block . (treesit-fold-range-seq 2 -2)) + '((fenced_code_block . treesit-fold-markdown-code-block) + (section . treesit-fold-markdown-heading) (html_block . treesit-fold-range-html))) (defun treesit-fold-parsers-matlab () diff --git a/treesit-fold.el b/treesit-fold.el index e063f3e..c4823f6 100644 --- a/treesit-fold.el +++ b/treesit-fold.el @@ -1152,6 +1152,47 @@ more information." (end (treesit-node-end last-child))) (treesit-fold--cons-add (cons beg end) offset))) +(defun treesit-fold-markdown-next-heading (node siblings) + "Return first heading from SIBLINGS with start point after NODE. +If there is no sibling, then return nil." + (or + (seq-find + (lambda (n) + (when-let ((child (treesit-node-child n 0 t))) + (and (> (treesit-node-start child) (treesit-node-start node)) + (treesit-fold--compare-type child "atx_heading")))) + (remove node siblings)) + (treesit-node-next-sibling (treesit-node-parent node) t))) + +(defun treesit-fold-markdown-heading (node offset) + "Define fold range for Markdown headings. + +For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for +more information." + (when-let* + ((parent (treesit-node-parent node)) + (head (treesit-node-child node 0 t)) + (beg (treesit-node-start node)) + (siblings (treesit-fold-find-children parent "section")) + (end (1- + (or (treesit-node-start + (treesit-fold-markdown-next-heading node siblings)) + (point-max)))) + (name (length (string-trim (or (treesit-node-text head) ""))))) + (treesit-fold--cons-add (cons beg end) (cons name 0)))) + +(defun treesit-fold-markdown-code-block (node offset) + "Define fold range for Markdown code blocks. + +For arguments NODE and OFFSET, see function `treesit-fold-range-seq' for +more information." + (let* ((beg (1+ (treesit-node-start node))) + (end (1- (treesit-node-end node))) + (name (+ 2 (length + (or (treesit-node-text (treesit-node-child node 1)) + ""))))) + (treesit-fold--cons-add (cons beg end) (cons name -2)))) + (defun treesit-fold-range-matlab-blocks (node offset) "Define fold range for MATLAB blocks.