Introduce offset

This commit is contained in:
Greg Sexton 2014-08-10 16:14:14 +01:00
parent 8a2d0388f6
commit 4d66682979
3 changed files with 26 additions and 19 deletions

View File

@ -49,10 +49,11 @@
parser to allow for recursive nesting of a parser. CREATE is a parser to allow for recursive nesting of a parser. CREATE is a
function that should build state taking the beginning, end and function that should build state taking the beginning, end and
children of the pair." children of the pair."
(parser-do (begin <- start) (parser-do (initial-pos <- (parser-position))
(begin <- start)
(children <- (funcall children)) (children <- (funcall children))
(end <- end) (end <- end)
(parser-return (funcall create begin end children)))) (parser-return (funcall create initial-pos end (- begin initial-pos) children))))
(defun origami-c-style-parser (create) (defun origami-c-style-parser (create)
(let ((pair (origami-pair (parser-char "{") (let ((pair (origami-pair (parser-char "{")

View File

@ -40,11 +40,12 @@
;;; overlay manipulation ;;; overlay manipulation
(defun origami-create-overlay (beg end buffer) (defun origami-create-overlay (beg end offset buffer)
(when (> (- end beg) 0) (when (> (- end beg) 0)
;; TODO: adding 1 won't work for anything other than parsing at ;; TODO: need to show the end so offset by 1. Maybe add an
;; the char level -- see TODO further down too ;; end-offset to fold node? The opposite of this is used in
(make-overlay (+ beg 1) end buffer))) ;; origami-fold-end.
(make-overlay (+ beg offset) (- end 1) buffer)))
(defun origami-hide-node-overlay (node) (defun origami-hide-node-overlay (node)
(-when-let (ov (origami-fold-data node)) (-when-let (ov (origami-fold-data node))
@ -76,7 +77,7 @@
;;; fold structure ;;; fold structure
(defun origami-fold-node (beg end open &optional children data) (defun origami-fold-node (beg end offset open &optional children data)
(let ((sorted-children (-sort (lambda (a b) (let ((sorted-children (-sort (lambda (a b)
(or (< (origami-fold-beg a) (origami-fold-beg b)) (or (< (origami-fold-beg a) (origami-fold-beg b))
(and (= (origami-fold-beg a) (origami-fold-beg b)) (and (= (origami-fold-beg a) (origami-fold-beg b))
@ -100,11 +101,13 @@
(if (and beg-children (or (> beg beg-children) (< end end-children))) (if (and beg-children (or (> beg beg-children) (< end end-children)))
(error "Node does not overlap children in range. beg=%s end=%s beg-children=%s end-children=%s" (error "Node does not overlap children in range. beg=%s end=%s beg-children=%s end-children=%s"
beg end beg-children end-children) beg end beg-children end-children)
(vector beg end open sorted-children data))))) (if (> (+ beg offset) end)
(error "Offset is not within the range of the node: beg=%s end=%s offset=%s" beg end offset)
(vector beg end offset open sorted-children data))))))
(defun origami-fold-root-node (&optional children) (defun origami-fold-root-node (&optional children)
;; TODO: fix min and max ;; TODO: fix min and max
(origami-fold-node 1 100000 t children 'root)) (origami-fold-node 1 100000 0 t children 'root))
(defun origami-fold-is-root-node? (node) (eq (origami-fold-data node) 'root)) (defun origami-fold-is-root-node? (node) (eq (origami-fold-data node) 'root))
@ -112,16 +115,17 @@
(when node (when node
(if (origami-fold-is-root-node? node) (if (origami-fold-is-root-node? node)
(aref node 0) (aref node 0)
;; TODO: decrementing to counter offset (- (overlay-start (origami-fold-data node)) (origami-fold-offset node)))))
(- (overlay-start (origami-fold-data node)) 1))))
(defun origami-fold-end (node) (defun origami-fold-end (node)
(when node (when node
(if (origami-fold-is-root-node? node) (if (origami-fold-is-root-node? node)
(aref node 1) (aref node 1)
(overlay-end (origami-fold-data node))))) (+ (overlay-end (origami-fold-data node)) 1))))
(defun origami-fold-open? (node) (when node (aref node 2))) (defun origami-fold-offset (node) (when node (aref node 2)))
(defun origami-fold-open? (node) (when node (aref node 3)))
(defun origami-fold-open-set (node value) (defun origami-fold-open-set (node value)
(when node (when node
@ -129,21 +133,23 @@
node node
(origami-fold-node (origami-fold-beg node) (origami-fold-node (origami-fold-beg node)
(origami-fold-end node) (origami-fold-end node)
(origami-fold-offset node)
value value
(origami-fold-children node) (origami-fold-children node)
(origami-fold-data node))))) (origami-fold-data node)))))
(defun origami-fold-children (node) (when node (aref node 3))) (defun origami-fold-children (node) (when node (aref node 4)))
(defun origami-fold-children-set (node children) (defun origami-fold-children-set (node children)
(when node (when node
(origami-fold-node (origami-fold-beg node) (origami-fold-node (origami-fold-beg node)
(origami-fold-end node) (origami-fold-end node)
(origami-fold-offset node)
(origami-fold-open? node) (origami-fold-open? node)
children children
(origami-fold-data node)))) (origami-fold-data node))))
(defun origami-fold-data (node) (when node (aref node 4))) (defun origami-fold-data (node) (when node (aref node 5)))
(defun origami-fold-range-equal (a b) (defun origami-fold-range-equal (a b)
(and (equal (origami-fold-beg a) (origami-fold-beg b)) (and (equal (origami-fold-beg a) (origami-fold-beg b))
@ -290,16 +296,16 @@ was last built."
(defun origami-get-parser (buffer) (defun origami-get-parser (buffer)
(let* ((cached-tree (origami-get-cached-tree buffer)) (let* ((cached-tree (origami-get-cached-tree buffer))
(create (lambda (beg end children) (create (lambda (beg end offset children)
(let ((previous-fold (-last-item (origami-fold-find-path-with-range cached-tree beg end)))) (let ((previous-fold (-last-item (origami-fold-find-path-with-range cached-tree beg end))))
(origami-fold-node beg end (origami-fold-node beg end offset
(if previous-fold (origami-fold-open? previous-fold) t) (if previous-fold (origami-fold-open? previous-fold) t)
children children
(or (-> (origami-fold-find-path-with-range (or (-> (origami-fold-find-path-with-range
(origami-get-cached-tree buffer) beg end) (origami-get-cached-tree buffer) beg end)
-last-item -last-item
origami-fold-data) origami-fold-data)
(origami-create-overlay beg end buffer))))))) (origami-create-overlay beg end offset buffer)))))))
(-when-let (parser-gen (cdr (assoc (buffer-local-value 'major-mode buffer) (-when-let (parser-gen (cdr (assoc (buffer-local-value 'major-mode buffer)
origami-parser-alist))) origami-parser-alist)))
(funcall parser-gen create)))) (funcall parser-gen create))))

View File

@ -119,7 +119,7 @@ consumed count."
(parser-do (pos <- (parser-position)) (parser-do (pos <- (parser-position))
(a <- (parser-item)) (a <- (parser-item))
(if (funcall pred a) (if (funcall pred a)
(parser-return pos) (parser-return (+ pos 1))
(parser-zero)))) (parser-zero))))
(defun parser-char (x) (defun parser-char (x)