parinfer / parinfer.js

Let's simplify the way we write Lisp
https://shaunlebron.github.io/parinfer
MIT License
1.76k stars 40 forks source link

Multi Arity functions in Indent mode #118

Closed ashercoren closed 8 years ago

ashercoren commented 8 years ago

When writing a multi arity functions, the regular way of writing it is:

(def f
  ([a]
    (+ a 3))
  ([a b]
    (+ a b)))

Where each arity option has a list of parameters, followed by the function implementation. The function implementation is indented one level further than the list of parameters.

With indent mode, this automatically gets changed, since the function implementation is nested inside the variable list, so it gets changed to:

(def f
  ([a
    (+ a 3)])
  ([a b
    (+ a b)]))
shaunlebron commented 8 years ago

Thanks for reporting! This is a known issue, and is the only irreconcilable inconsistency with the clojure indentation standards. When designing parinfer, I ran it against all clojure core repos to be sure that this was the only thing that had to go.

If it's any consolation, Parinfer is actually still compatible with the Clojure Style Guide, since it has a strict stance against indenting multi-arities that way. I brought up how this contradicts real-world usage to the style guide author here. You can read his response there.

So, from the perspectives of everyone involved:

facundoolano commented 8 years ago

I use the same indentation suggested in the style guide you mentioned. I always run into problems, though, when converting a function of single arity into a multiple arity one. I can't seem to find a way to wrap the old body around a new set of parenthesis without breaking the code:

(defn foo
  "What I have."
  [x]
  (bar x))

(defn foo
  "What I'd like next (before adding the other arity definition)."
  ([x]
   (bar x)))

(defn foo
  "What I actually get."
  ([x
    (bar x)]))

I can't tell if it's parinfer or the editor -sublime- causing it. If it's parinfer, I wonder if that's a behaviour that could be changed.

facundoolano commented 8 years ago

Actually, nevermind, I just noticed I can avoid that by changing to paren mode before adding the new parentheses.

shaunlebron commented 8 years ago

@facundoolano yeah, it's a common enough hiccup. still thinking on alternate resolutions