Fold javadoc also

This commit is contained in:
Greg Sexton 2015-04-08 21:31:14 +01:00
parent e9577c8a8d
commit 27b28c8e16
2 changed files with 55 additions and 9 deletions

View File

@ -31,9 +31,10 @@
;;; Code: ;;; Code:
(require 'cl) (require 'cl)
(require 'dash)
(defcustom origami-parser-alist (defcustom origami-parser-alist
'((java-mode . origami-c-style-parser) '((java-mode . origami-java-parser)
(c-mode . origami-c-style-parser) (c-mode . origami-c-style-parser)
(c++-mode . origami-c-style-parser) (c++-mode . origami-c-style-parser)
(perl-mode . origami-c-style-parser) (perl-mode . origami-c-style-parser)
@ -49,6 +50,9 @@
:group 'origami) :group 'origami)
(defun origami-get-positions (content regex) (defun origami-get-positions (content regex)
"Returns a list of positions where REGEX matches in CONTENT. A
position is a cons cell of the character and the numerical
position in the CONTENT."
(with-temp-buffer (with-temp-buffer
(insert content) (insert content)
(goto-char (point-min)) (goto-char (point-min))
@ -87,11 +91,36 @@
(cons positions (reverse acc))))) (cons positions (reverse acc)))))
(cdr (build positions)))) (cdr (build positions))))
;;; TODO: tag these nodes? have ability to manipulate nodes that are
;;; tagged? in a scoped fashion?
(defun origami-javadoc-parser (create)
(lambda (content)
(let ((positions (->> (origami-get-positions content "/\\*\\*\\|\\*/")
(-filter (lambda (position)
(eq (get-text-property 0 'face (car position))
'font-lock-doc-face))))))
(origami-build-pair-tree create "/**" "*/" positions))))
(defun origami-c-style-parser (create) (defun origami-c-style-parser (create)
(lambda (content) (lambda (content)
(let ((positions (origami-get-positions content "[{}]"))) (let ((positions (->> (origami-get-positions content "[{}]")
(remove-if (lambda (position)
(let ((face (get-text-property 0 'face (car position))))
(-any? (lambda (f)
(memq f '(font-lock-doc-face
font-lock-comment-face
font-lock-string-face)))
(if (listp face) face (list face)))))))))
(origami-build-pair-tree create "{" "}" positions)))) (origami-build-pair-tree create "{" "}" positions))))
(defun origami-java-parser (create)
(let ((c-style (origami-c-style-parser create))
(javadoc (origami-javadoc-parser create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall c-style content))
(origami-fold-root-node (funcall javadoc content)))))))
(defun origami-lisp-parser (create regex) (defun origami-lisp-parser (create regex)
(lambda (content) (lambda (content)
(with-temp-buffer (with-temp-buffer

View File

@ -170,6 +170,10 @@
(defun origami-fold-state-equal (a b) (defun origami-fold-state-equal (a b)
(equal (origami-fold-open? a) (origami-fold-open? b))) (equal (origami-fold-open? a) (origami-fold-open? b)))
(defun origami-fold-add-child (node new)
(origami-fold-children-set node
(cons new (origami-fold-children node))))
(defun origami-fold-replace-child (node old new) (defun origami-fold-replace-child (node old new)
(origami-fold-children-set node (origami-fold-children-set node
(cons new (remove old (origami-fold-children node))))) (cons new (remove old (origami-fold-children node)))))
@ -236,13 +240,16 @@ children cannot be manipulated."
(cons tree (origami-fold-find-deepest child pred)) (cons tree (origami-fold-find-deepest child pred))
(list tree))))) (list tree)))))
(defun origami-fold-find-path-containing-range (tree beg end)
(origami-fold-find-deepest tree
(lambda (node)
(and (>= beg (origami-fold-beg node))
(<= end (origami-fold-end node))))))
(defun origami-fold-find-path-with-range (tree beg end) (defun origami-fold-find-path-with-range (tree beg end)
"Return the path to the most specific (deepest) node that has "Return the path to the most specific (deepest) node that has
exactly the range BEG-END, or null." exactly the range BEG-END, or null."
(-when-let (path (origami-fold-find-deepest tree (-when-let (path (origami-fold-find-path-containing-range tree beg end))
(lambda (node)
(and (>= beg (origami-fold-beg node))
(<= end (origami-fold-end node))))))
(let ((last (-last-item path))) (let ((last (-last-item path)))
(when (and (= beg (origami-fold-beg last)) (when (and (= beg (origami-fold-beg last))
(= end (origami-fold-end last))) (= end (origami-fold-end last)))
@ -279,6 +286,16 @@ with the current state and the current node at each iteration."
(origami-fold-postorder-reduce node (lambda (acc node) (origami-fold-postorder-reduce node (lambda (acc node)
(and acc (origami-fold-open? node))) t)) (and acc (origami-fold-open? node))) t))
(defun origami-fold-shallow-merge (tree1 tree2)
"Shallow merge the children of TREE2 in to TREE1."
(-reduce-from (lambda (tree node)
(origami-fold-assoc (origami-fold-find-path-containing-range tree
(origami-fold-beg node)
(origami-fold-end node))
(lambda (leaf)
(origami-fold-add-child leaf node))))
tree1 (origami-fold-children tree2)))
;;; linear history structure ;;; linear history structure
(defun origami-h-new (present) (defun origami-h-new (present)
@ -346,10 +363,10 @@ was last built."
(defun origami-build-tree (buffer parser) (defun origami-build-tree (buffer parser)
(when parser (when parser
(with-current-buffer buffer (with-current-buffer buffer
(let ((contents (buffer-substring-no-properties (point-min) (point-max)))) (let ((contents (buffer-string)))
(-> parser (-> parser
(funcall contents) (funcall contents)
origami-fold-root-node))))) origami-fold-root-node)))))
(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))