weavejester / cljfmt

A tool for formatting Clojure code
Eclipse Public License 1.0
1.12k stars 119 forks source link

Support for cond #60

Open arrdem opened 8 years ago

arrdem commented 8 years ago

It's my personal style to format cond forms as such:

(cond
  (test-one)
  ,,(expr-one)

  (test-two)
  ,,(expr-two)

  ...)

Even if it isn't on by default, it'd be awesome if cljfmt had support for this convention.

danielstockton commented 6 years ago

The default cond and cond-> indentation is horrible.

In emacs clojure-mode, I use the following functions:

  (defun indent-cond (indent-point state)
    (goto-char (elt state 1))
    (let ((pos -1)
          (base-col (current-column)))
      (forward-char 1)
      ;; `forward-sexp' will error if indent-point is after
      ;; the last sexp in the current sexp.
      (condition-case nil
          (while (and (<= (point) indent-point)
                      (not (eobp)))
            (clojure-forward-logical-sexp 1)
            (cl-incf pos))
        ;; If indent-point is _after_ the last sexp in the
        ;; current sexp, we detect that by catching the
        ;; `scan-error'. In that case, we should return the
        ;; indentation as if there were an extra sexp at point.
        (scan-error (cl-incf pos)))
      (+ base-col (if (evenp pos) 4 2))))
  (defun indent-cond-> (indent-point state)
    (goto-char (elt state 1))
    (let ((pos -1)
          (base-col (current-column)))
      (forward-char 1)
      ;; `forward-sexp' will error if indent-point is after
      ;; the last sexp in the current sexp.
      (condition-case nil
          (while (and (<= (point) indent-point)
                      (not (eobp)))
            (clojure-forward-logical-sexp 1)
            (cl-incf pos))
        ;; If indent-point is _after_ the last sexp in the
        ;; current sexp, we detect that by catching the
        ;; `scan-error'. In that case, we should return the
        ;; indentation as if there were an extra sexp at point.
        (scan-error (cl-incf pos)))
      (+ base-col (if (oddp pos) 4 2))))
  (with-eval-after-load 'clojure-mode
    (put-clojure-indent 'cond #'indent-cond)
    (put-clojure-indent 'condp #'indent-cond)
    (put-clojure-indent 'cond-> #'indent-cond->)
    (put-clojure-indent 'cond->> #'indent-cond->))

However, on shared projects I would like to use cljfmt (not everyone uses emacs) and this is my biggest pain point.