Skip to content

Commit

Permalink
[Fix #305] No newline after :as or :refer
Browse files Browse the repository at this point in the history
This makes the ns form take up less vertical space and makes it easier
on the eyes.

This was never done intentionally but a side-effect of simply delegating
the printing of regular libspecs to `clojure.core.pprint` which puts
each element of a vector on its own line when the line gets long enough.
  • Loading branch information
expez committed Jun 27, 2021
1 parent 9fee2ed commit eec24e9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 37 deletions.
26 changes: 18 additions & 8 deletions src/refactor_nrepl/ns/pprint.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(vec (concat (remove sequential? libspecs)
(filter sequential? libspecs))))

(defn- pprint-prefix-form [[name & libspecs]]
(defn- pprint-libspec-with-prefix-form [[name & libspecs]]
(printf "[%s" name)
(let [ordered-libspecs (libspec-vectors-last libspecs)]
(dorun
Expand All @@ -35,11 +35,19 @@
(printf "%s " libspec))))))
ordered-libspecs))))

(defn insert-clause-delimiter []
(defn- insert-clause-delimiter []
(if (:insert-newline-after-require *config*)
(println)
(print " ")))

(defn- pprint-libspec [libspec]
;; If a vector gets too long `pprint` will print one element per line.
;; This puts `:as` and `:refer` on their own line, which causes the ns form
;; to take up too much vertical space.
(printf (str/replace (with-out-str (pprint libspec))
(re-pattern (str "(:as|refer)" (System/lineSeparator)))
"$1")))

(defn pprint-require-form
[[_ & libspecs]]
(print "(:require")
Expand All @@ -48,13 +56,15 @@
(map-indexed
(fn [idx libspec]
(if (= idx (dec (count libspecs)))
(printf "%s)\n" (str/trim-newline
(with-out-str (if (prefix-form? libspec)
(pprint-prefix-form libspec)
(pprint libspec)))))
(printf "%s)\n"
(str/trim-newline
(with-out-str
(if (prefix-form? libspec)
(pprint-libspec-with-prefix-form libspec)
(pprint libspec)))))
(if (prefix-form? libspec)
(pprint-prefix-form libspec)
(pprint libspec))))
(pprint-libspec-with-prefix-form libspec)
(pprint-libspec libspec))))
libspecs)))

(defn- form-is? [form type]
Expand Down
49 changes: 25 additions & 24 deletions test/refactor_nrepl/ns/clean_ns_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,29 @@
(is (= clean-requires requires))
(is (= clean-imports imports))))

(def artifact-ns '(ns refactor-nrepl.artifacts
(:require [clojure
[edn :as edn]
[string :as str]]
[clojure.data.json :as json]
[clojure.java.io :as io]
[nrepl
[middleware :refer [set-descriptor!]]
[misc :refer [response-for]]
[transport :as transport]]
[org.httpkit.client :as http]
[refactor-nrepl.externs :refer [add-dependencies]])
(:import java.util.Date)))
(def artifact-ns
'(ns refactor-nrepl.artifacts
(:require
[clojure
[edn :as edn]
[string :as str]]
[clojure.data.json :as json]
[clojure.java.io :as io]
[nrepl
[middleware :refer [set-descriptor!]]
[misc :refer [response-for]]
[transport :as transport]]
[org.httpkit.client
:as very-very-very-very-long-alias-causing-line-wrap]
[refactor-nrepl.externs :refer [add-dependencies]])
(:import java.util.Date)))

(deftest test-pprint-artifact-ns
(are [setting filename] (let [actual (config/with-config {:insert-newline-after-require setting}
(pprint-ns (with-meta artifact-ns nil)))
expected (-> filename File. .getAbsolutePath slurp)]
(= expected actual))
(are [setting filename]
(let [actual (config/with-config {:insert-newline-after-require setting}
(pprint-ns (with-meta artifact-ns nil)))
expected (-> filename File. .getAbsolutePath slurp)]
(= expected actual))
true "test/resources/artifacts_pprinted"
false "test/resources/artifacts_pprinted_traditional_newline"))

Expand Down Expand Up @@ -200,13 +204,10 @@
(deftest does-not-remove-ns-with-rename
(is (= (nthrest ns-with-rename-cleaned 2) (nthrest (clean-ns ns-with-rename) 2))))

;; Order of stuff in maps aren't stable across versions which messes
;; with pretty-printing
(when (= (clojure-version) "1.7.0")
(deftest test-pprint
(let [ns-str (pprint-ns (clean-ns ns1))
ns1-str (slurp (.getAbsolutePath (File. "test/resources/ns1_cleaned_and_pprinted")))]
(is (= ns1-str ns-str)))))
(deftest test-pprint
(let [ns-str (pprint-ns (clean-ns ns1))
ns1-str (slurp (.getAbsolutePath (File. "test/resources/ns1_cleaned_and_pprinted")))]
(is (= ns1-str ns-str))))

(deftest preserves-shorthand-meta
(let [cleaned (pprint-ns (clean-ns ns-with-shorthand-meta))]
Expand Down
3 changes: 2 additions & 1 deletion test/resources/artifacts_pprinted
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
[middleware :refer [set-descriptor!]]
[misc :refer [response-for]]
[transport :as transport]]
[org.httpkit.client :as http]
[org.httpkit.client
:as very-very-very-very-long-alias-causing-line-wrap]
[refactor-nrepl.externs :refer [add-dependencies]])
(:import
java.util.Date))
3 changes: 2 additions & 1 deletion test/resources/artifacts_pprinted_traditional_newline
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[middleware :refer [set-descriptor!]]
[misc :refer [response-for]]
[transport :as transport]]
[org.httpkit.client :as http]
[org.httpkit.client
:as very-very-very-very-long-alias-causing-line-wrap]
[refactor-nrepl.externs :refer [add-dependencies]])
(:import java.util.Date))
10 changes: 7 additions & 3 deletions test/resources/ns1_cleaned_and_pprinted
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
:methods [[binomial [int int] double]])
(:require
[clojure data edn xml
[instant :as inst :reload true]
[pprint :refer [cl-format formatter get-pretty-writer]]
[string :refer :all :reload-all true]
[test :refer :all]
[walk :refer [postwalk prewalk]]]
clojure.test.junit)
(:import [java.io Closeable FilenameFilter PushbackReader]
[java.util Calendar Date Random]
[refactor.nrepl SomeClass$InnerClass$InnerInnerClassOne SomeClass$InnerClass$InnerInnerClassTwo]))
(:import
[java.io Closeable FilenameFilter PushbackReader]
[java.util Calendar Date Random]
[refactor.nrepl SomeClass$InnerClass$InnerInnerClassOne SomeClass$InnerClass$InnerInnerClassTwo]))

0 comments on commit eec24e9

Please sign in to comment.