LightTable / Clojure

Light Table Clojure language plugin
MIT License
99 stars 51 forks source link

consider making inline results with pprint #88

Closed carocad closed 6 years ago

carocad commented 8 years ago

hey guys, this is not an issue but rather a proposal that I would like to put out.

Currently the inline results are "stringified" according these lines. Basically, everything that is not a special case is handled by pr-str.

It works, but for big nested structures reading the values can get very tricky and confusing since everything is put into a single long line (which is the way the clojure reader would read it).

I propose changing the stringify way to use clojure.pprint which in normal cases would simply output everything as it was before but on nested structures will pretty print it, making it way more readable.

I modified my clean-serialize function to do exactly that. It looks like this:

(defn clean-serialize [res & [opts]]
  (binding [*print-length* (or (:print-length opts) *print-length* 1000)]
    (cond
      (fn? res) 'fn
      (var? res) (if-not (:allow-var? opts)
                   res
                   (str res))
      (nil? res) "nil"
      (false? res) "false"
      (and (instance? clojure.lang.Atom res) (:result opts)) (str "atom[" @res "]")
      (instance? clojure.lang.Atom res) (str "atom")
      ;(is-non-clojure? res) (str res)
      (record? res) (pr-str res)
      (and (string? res) (:verbatim opts)) res
      :else (with-out-str (clojure.pprint/pprint res)))))

There are two additions, (record?...) and pprint. records are due to pprint outputting them as maps and pprint is were the magic happens.

I would like to submit a PR with it but I think that more discussing would be prudent as I am not an expert on this.

Happy to hear your comments

carocad commented 6 years ago

closing this due to lack of follow up