From 5538af82c0b54d5b5b97d32ebacc00bdbda09444 Mon Sep 17 00:00:00 2001 From: anton Date: Tue, 7 Jan 2020 04:42:54 -0500 Subject: [PATCH] python parser with all code blocks --- origami-parsers.el | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index 87c704f..a9de8d7 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -188,24 +188,32 @@ position in the CONTENT." (origami-fold-root-node (funcall javadoc content))))))) (defun origami-python-parser (create) + "Parse python block of codes and invoke callback CREATE on parsed regions." (lambda (content) (with-temp-buffer (insert content) (python-mode) (goto-char (point-min)) - (beginning-of-defun -1) - (let (beg (end (point-max)) offset acc) - (while (not (= (point) end)) - (setq beg (point)) - (search-forward-regexp ":" nil t) - (setq offset (- (point) beg)) - (end-of-defun) - (backward-char) - (setq end (point)) - (when (> offset 0) - (setq acc (cons (funcall create beg end offset nil) acc))) - (beginning-of-defun -1)) - (reverse acc))))) + (defun origami-python-parser-one-level-blocks (wrap-beg wrap-end) + "Parser one level python blocks of codes from WRAP-BEG to WRAP-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)) + + (save-excursion (python-nav-end-of-block) (setq end (point))) + (save-excursion (python-nav-end-of-statement) (setq offset (- (point) beg))) + (save-excursion + (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) (lambda (content)