python parser with all code blocks

This commit is contained in:
anton 2020-01-07 04:42:54 -05:00
parent 1f38085c8f
commit 5538af82c0

View File

@ -188,24 +188,32 @@ position in the CONTENT."
(origami-fold-root-node (funcall javadoc content))))))) (origami-fold-root-node (funcall javadoc content)))))))
(defun origami-python-parser (create) (defun origami-python-parser (create)
"Parse python block of codes and invoke callback CREATE on parsed regions."
(lambda (content) (lambda (content)
(with-temp-buffer (with-temp-buffer
(insert content) (insert content)
(python-mode) (python-mode)
(goto-char (point-min)) (goto-char (point-min))
(beginning-of-defun -1) (defun origami-python-parser-one-level-blocks (wrap-beg wrap-end)
(let (beg (end (point-max)) offset acc) "Parser one level python blocks of codes from WRAP-BEG to WRAP-END."
(while (not (= (point) end)) (goto-char wrap-beg)
(setq beg (point)) (let ((beg wrap-beg) (prev -1) (end wrap-end) (offset 0) children acc)
(search-forward-regexp ":" nil t) (while (and (< prev beg) (< beg end))
(setq offset (- (point) beg)) (python-nav-forward-block)
(end-of-defun) (setq prev beg) ;; if prev == beg, region has not next block
(backward-char) (setq beg (point))
(setq end (point))
(when (> offset 0) (save-excursion (python-nav-end-of-block) (setq end (point)))
(setq acc (cons (funcall create beg end offset nil) acc))) (save-excursion (python-nav-end-of-statement) (setq offset (- (point) beg)))
(beginning-of-defun -1)) (save-excursion
(reverse acc))))) (python-nav-forward-block)
(when (and (< (point) end) (< prev beg)) (setq children t)))
(when (and (< prev beg) (< beg end) (< end wrap-end))
(setq acc (cons (funcall create beg end offset (and children (origami-python-parser-one-level-blocks beg end))) acc)))
)
(reverse acc)))
(origami-python-parser-one-level-blocks (point-min) (point-max)))))
(defun origami-lisp-parser (create regex) (defun origami-lisp-parser (create regex)
(lambda (content) (lambda (content)