Factor in newlines

This commit is contained in:
Greg Sexton 2015-04-26 13:21:16 +01:00
parent 2b07b5c6fd
commit a8eb81e09c

View File

@ -67,35 +67,35 @@ position in the CONTENT."
;;; TODO: support tabs ;;; TODO: support tabs
(defun origami-indent-parser (create) (defun origami-indent-parser (create)
(cl-labels ((lines (string) (cl-labels ((lines (string) (origami-get-positions string ".*?\r?\n"))
(-> string
(origami-get-positions ".*?\r?\n")
(->> (-filter (lambda (line)
(not (s-blank? (s-trim (car line)))))))))
(annotate-levels (lines) (annotate-levels (lines)
(-map (lambda (line) (-map (lambda (line)
(let ((beg (cdr line)) (let ((indent (length (car (s-match "^ *" (car line)))))
(beg (cdr line))
(end (+ (cdr line) (length (car line)) -1))) (end (+ (cdr line) (length (car line)) -1)))
(vector (length (car (s-match "^ *" (car line)))) (if (s-blank? (s-trim (car line)))
beg 'newline ;sentinel representing line break
end (vector indent beg end (- end beg)))))
(- end beg))))
lines)) lines))
(indent (line) (aref line 0)) (indent (line) (if (eq line 'newline) -1 (aref line 0)))
(beg (line) (aref line 1)) (beg (line) (aref line 1))
(end (line) (aref line 2)) (end (line) (aref line 2))
(offset (line) (aref line 3)) (offset (line) (aref line 3))
(collapse-same-level (levels) (collapse-same-level (lines)
(reverse (-reduce-from (lambda (acc level) (->>
(if (= (indent level) (indent (car acc))) (cdr lines)
(cons (vector (indent (car acc)) (-reduce-from (lambda (acc line)
(beg (car acc)) (cond ((and (eq line 'newline) (eq (car acc) 'newline)) acc)
(end level) ((= (indent line) (indent (car acc)))
(offset (car acc))) (cons (vector (indent (car acc))
(cdr acc)) (beg (car acc))
(cons level acc))) (end line)
(list (car levels)) (offset (car acc)))
(cdr levels)))) (cdr acc)))
(t (cons line acc))))
(list (car lines)))
(remove 'newline)
reverse))
(create-tree (levels) (create-tree (levels)
(if (null levels) (if (null levels)
levels levels
@ -103,7 +103,11 @@ position in the CONTENT."
(->> levels (->> levels
(-partition-by (lambda (l) (= (indent l) curr-indent))) (-partition-by (lambda (l) (= (indent l) curr-indent)))
(-partition-all 2) (-partition-all 2)
(-map (lambda (x) (cons (caar x) (create-tree (cadr x))))))))) (-mapcat (lambda (x)
;takes care of multiple identical levels, introduced when there are newlines
(-concat
(-map 'list (butlast (car x)))
(list (cons (-last-item (car x)) (create-tree (cadr x)))))))))))
(build-nodes (tree) (build-nodes (tree)
(if (null tree) (cons 0 nil) (if (null tree) (cons 0 nil)
;; complexity here is due to having to find the end of the children so that the ;; complexity here is due to having to find the end of the children so that the