Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse markdown headings #13

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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.

Expand Down
Loading