Only compute the tree if changes have been made

This commit is contained in:
Greg Sexton 2014-06-02 20:52:39 +01:00
parent 053c2c7ac8
commit a244624c7a

View File

@ -428,11 +428,27 @@ parser to allow for recursive nesting of a parser."
;;; interactive utils ;;; interactive utils
;;; TODO: create functions for accessing/setting the local vars and
;;; make sure these are used -- error if the buffer local var isn't set
(defun origami-get-cached-tree (buffer) (defun origami-get-cached-tree (buffer)
origami-tree) (or (local-variable-p 'origami-tree buffer)
(error "Necessary local variables were not available"))
(buffer-local-value 'origami-tree buffer))
(defun origami-store-cached-tree (buffer tree) (defun origami-store-cached-tree (buffer tree)
(setq origami-tree tree)) (or (local-variable-p 'origami-tree buffer)
(local-variable-p 'origami-tree-tick buffer)
(error "Necessary local variables were not available"))
(with-current-buffer buffer
(setq origami-tree-tick (buffer-modified-tick))
(setq origami-tree tree)))
(defun origami-rebuild-tree? (buffer)
"Determines if the tree needs to be rebuilt for BUFFER since it
was last built."
(not (= (buffer-local-value 'origami-tree-tick buffer)
(buffer-modified-tick buffer))))
(defun origami-was-previously-open? (tree beg end) (defun origami-was-previously-open? (tree beg end)
(-if-let (node (-last-item (origami-fold-find-path-with-range tree beg end))) (-if-let (node (-last-item (origami-fold-find-path-with-range tree beg end)))
@ -472,9 +488,10 @@ parser to allow for recursive nesting of a parser."
(defun origami-get-fold-tree (buffer) (defun origami-get-fold-tree (buffer)
"Facade. Build the tree if it hasn't already been built "Facade. Build the tree if it hasn't already been built
otherwise fetch cached tree." otherwise fetch cached tree."
;; TODO: caching -- don't parse again if there have been no edits since last time
(when origami-mode (when origami-mode
(origami-build-tree buffer (origami-get-parser buffer)))) (if (origami-rebuild-tree? buffer)
(origami-build-tree buffer (origami-get-parser buffer))
(origami-get-cached-tree buffer))))
;;; commands ;;; commands
@ -624,7 +641,9 @@ Key bindings:
:keymap origami-mode-map :keymap origami-mode-map
:init-value nil :init-value nil
(if origami-mode ;enabling if t (if origami-mode ;enabling if t
(progn
(set (make-local-variable 'origami-tree) (origami-fold-root-node)) (set (make-local-variable 'origami-tree) (origami-fold-root-node))
(set (make-local-variable 'origami-tree-tick) (buffer-modified-tick)))
(origami-reset (current-buffer)))) (origami-reset (current-buffer))))
(provide 'origami) (provide 'origami)