cljfmt icon indicating copy to clipboard operation
cljfmt copied to clipboard

Support for defn

Open arrdem opened this issue 9 years ago • 1 comments

By convention, defns with a single arity are indented approximately as such:

(defn foo
  docstring?
  attr-map?
  [args]
  forms)
  • If there is no docstring or meta and the args vector is "short" then the name and the args should be on the same line as the defn.
  • If there is only one body form and it is "short", then it should be on the same line as the args vector.
  • If there is only one arity, then (defn foo ([args] & forms)) should be rewritten as above.

arrdem avatar Mar 04 '16 15:03 arrdem

This might be better split into two issues.

The first, removing parentheses around functions with one arity, seems pretty specific to functions alone. We could add that in as its own rule, and it would likely only be a few lines of code.

The second issue, fixing newlines, seems a more general issue, like indentation. I've been thinking about specifying rules to denote valid points for newlines. For instance:

;; valid
(if x y z)

;; valid
(if x
  y
  z)

;; invalid
(if x y
  z)

I'm not decided on the syntax for specifying the rules yet. Perhaps something like:

[[if _ _ _]
 [if _ \n _ \n _]]

For when and similar, we also need to have some repetition:

[[when _ _ *]
 [when _ \n (_ \n) *]]

And for defn, some sort of predicate/type match:

[[defn _ :vector \n (_ \n) *]
 [defn _ \n :string \n (_ \n) *]]

weavejester avatar Mar 04 '16 17:03 weavejester