Skip to content

Commit

Permalink
Handle aligning commented out forms.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Stampoultzis committed Jan 10, 2016
1 parent 21b4981 commit dc55b77
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 18 deletions.
108 changes: 98 additions & 10 deletions align-cljlet-test.el
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
(require 'align-cljlet)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Unit Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun acl-forward-sexp-should-move-to (initial-buffer expected-buffer)
(with-temp-buffer
(clojure-mode)
(insert initial-buffer)
(goto-char (point-min))
(search-forward "|" nil nil 1)
(delete-backward-char 1)
(acl-forward-sexp)
(insert "|")
(should (equal (buffer-substring-no-properties 1 (point-max))
expected-buffer))))

(ert-deftest acl-foward-sexp-test ()
(acl-forward-sexp-should-move-to "|a b"
"a| b")
(acl-forward-sexp-should-move-to "a| b"
"a b|")
(acl-forward-sexp-should-move-to "a b|"
"a b|")
(acl-forward-sexp-should-move-to "|#foo/bar [1 2 3] [1 2 3]"
"#foo/bar [1 2 3]| [1 2 3]")
(acl-forward-sexp-should-move-to "|^int 123 999"
"^int 123| 999")
(acl-forward-sexp-should-move-to "|#_(ignore me) 123 999"
"#_(ignore me) 123| 999")
(acl-forward-sexp-should-move-to "|123 #_(ignore me) 999"
"123| #_(ignore me) 999")
(acl-forward-sexp-should-move-to "123| #_(ignore me) 999"
"123 #_(ignore me) 999|")
)

(defun acl-goto-next-pair-should-move-to (initial-buffer expected-buffer)
(with-temp-buffer
(clojure-mode)
(insert initial-buffer)
(goto-char (point-min))
(search-forward "|" nil nil 1)
(delete-backward-char 1)
(acl-goto-next-pair)
(let ((expected-position (1+ (string-match "|" expected-buffer))))
(should (equal (point)
expected-position)))))

(ert-deftest acl-goto-next-pair-test ()
(acl-goto-next-pair-should-move-to "|a b c d" "a b |c d")
(acl-goto-next-pair-should-move-to "a b |c d e f" "a b c d |e f")
(acl-goto-next-pair-should-move-to "a b c d |e f" "a b c d e |f") ;; slightly odd behaviour here
(acl-goto-next-pair-should-move-to "|(1 2 3) b c d" "(1 2 3) b |c d")
(acl-goto-next-pair-should-move-to "|a #_(1 2 3) b c d" "a #_(1 2 3) b |c d")
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Alignment tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun acl-should-error (buffer)
(with-temp-buffer
(clojure-mode)
Expand All @@ -16,8 +76,8 @@
(goto-char (point-min))
(down-list)
(align-cljlet)
(should (equal expected
(buffer-substring-no-properties (point-min) (point-max))))))
(should (equal (buffer-substring-no-properties (point-min) (point-max))
expected))))

(ert-deftest align-let ()
(acl-should-align "
Expand Down Expand Up @@ -59,28 +119,56 @@
"
{#foo/bar [1 2 3] 234
:foobar (list 1 2 3)
}")
}"))

(acl-should-error
" {:a 1 :b 2}"))
(ert-deftest align-hash-with-commented-form ()
(acl-should-align
"
(let [variable-a #_(slow-database-call) 1
some-other-variable 2] body)
"
"
(let [variable-a #_(slow-database-call) 1
some-other-variable 2] body)
"))

(ert-deftest align-cond ()
(ert-deftest align-hash-with-commented-form-at-front ()
(acl-should-align
"(cond
"
(let [#_(slow-database-call) variable-a 1
some-other-variable 2] body)
"
"
(let [#_(slow-database-call) variable-a 1
some-other-variable 2] body)
"))

(ert-deftest align-hash-with-all-sorts-of-commented-forms ()
(acl-should-align
"
(let [#_(slow-database-call) variable-a #_(1 2 3) 1
some-other-variable 2] body)"
"
(let [#_(slow-database-call) variable-a #_(1 2 3) 1
some-other-variable 2] body)"))

(ert-deftest align-cond ()
(acl-should-align
"(cond
(> grade 90) \"A\"
(> grade 80) \"B\"
(> grade 70) \"C\"
(> grade 60) \"D\"
:else \"F\")"
"(cond
"(cond
(> grade 90) \"A\"
(> grade 80) \"B\"
(> grade 70) \"C\"
(> grade 60) \"D\"
:else \"F\")")

;; TODO: Currently does not handle :>> forms
)
;; TODO: Currently does not handle :>> forms
)

(ert-deftest align-for ()
(acl-should-align
Expand Down
36 changes: 28 additions & 8 deletions align-cljlet.el
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,38 @@
))
t)

(defun acl-forward-sexp ()
(call-interactively 'clojure-forward-logical-sexp))
(defun acl-skip-commented ()
(while (acl-is-commented?)
(call-interactively 'clojure-forward-logical-sexp)))

(defun acl-is-commented? ()
(or (looking-at "#_")
(looking-back "#_")
(and (looking-at "\\s-")
(save-excursion
(call-interactively 'clojure-backward-logical-sexp)
(or (looking-back "#_")
(looking-at "#_"))))))

(defun acl-forward-sexp (&optional dont-skip-comments)
"Jumps the cursor forward to the end of the current sexp or to
the end of the next sexp if already positioned at the
end. Commented forms are skipped by default unless
dont-skip-comments is true."
(if (and (not dont-skip-comments) (looking-at "#_("))
(acl-skip-commented)
(call-interactively 'clojure-forward-logical-sexp))
(unless dont-skip-comments (acl-skip-commented))
)

(defun acl-goto-next-pair ()
"Skip ahead to the next definition"
(condition-case nil
(progn
(acl-forward-sexp)
(acl-forward-sexp)
(forward-sexp)
(backward-sexp)
(acl-forward-sexp)
(call-interactively 'clojure-backward-logical-sexp)
t)
(error nil)))

Expand All @@ -158,12 +179,11 @@

(defun acl-next-sexp ()
"Goes to the next sexp, returning true or false if there is no next"

(condition-case nil
(progn
(acl-forward-sexp)
(acl-forward-sexp)
(backward-sexp)
(acl-forward-sexp t)
(call-interactively 'clojure-backward-logical-sexp)
't)
('error nil)))

Expand Down Expand Up @@ -225,7 +245,7 @@
(acl-respace-subform (list max-width)))

(defun acl-respace-subform (widths)
"Respace a defroute subform using the widths given. Point must
"Respace a subform using the widths given. Point must
be positioned on the first s-exp in the subform."
(save-excursion
(while widths
Expand Down

0 comments on commit dc55b77

Please sign in to comment.