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)
(let ((beg wrap-beg) (prev -1) (end wrap-end) (offset 0) children acc)
(while (and (< prev beg) (< beg end))
(python-nav-forward-block)
(setq prev beg) ;; if prev == beg, region has not next block
(setq beg (point)) (setq beg (point))
(search-forward-regexp ":" nil t)
(setq offset (- (point) beg)) (save-excursion (python-nav-end-of-block) (setq end (point)))
(end-of-defun) (save-excursion (python-nav-end-of-statement) (setq offset (- (point) beg)))
(backward-char) (save-excursion
(setq end (point)) (python-nav-forward-block)
(when (> offset 0) (when (and (< (point) end) (< prev beg)) (setq children t)))
(setq acc (cons (funcall create beg end offset nil) acc))) (when (and (< prev beg) (< beg end) (< end wrap-end))
(beginning-of-defun -1)) (setq acc (cons (funcall create beg end offset (and children (origami-python-parser-one-level-blocks beg end))) acc)))
(reverse 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)