Skip to content

Commit

Permalink
parse markdown headings (#13)
Browse files Browse the repository at this point in the history
* parse markdown headings

* document next heading function

* replace treesit-node-get call with treesit-node-next-sibling
  • Loading branch information
tpeacock19 authored Jul 25, 2024
1 parent 323c579 commit 18d874f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion treesit-fold-parsers.el
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 ()
Expand Down
41 changes: 41 additions & 0 deletions treesit-fold.el
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,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.
Expand Down

0 comments on commit 18d874f

Please sign in to comment.