Skip to content

Commit 04f6c39

Browse files
committed
[Fix #305] No newline after :as or :refer
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.
1 parent 9fee2ed commit 04f6c39

File tree

6 files changed

+58
-38
lines changed

6 files changed

+58
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
## Unreleased
44

5+
* [#305](https://github.com/clojure-emacs/refactor-nrepl/issues/305): Don't put `:as` or `:refer` on their own lines in the ns form, when the libspec is so long it causes the line to wrap.
56
* [clojure-emacs/clj-refactor.el#459](https://github.com/clojure-emacs/clj-refactor.el/issues/459): `clean-ns` should conform to the style guide: `(:require` in the ns form should be followed by a newline.
67
* You can opt out via the new `:insert-newline-after-require` configuration option.
78
* [#294](https://github.com/clojure-emacs/refactor-nrepl/pull/294): Properly skip uneval nodes when looking for the first/last sexp
89
* From now on, if you set the `clojure.tools.namespace.repl/refresh-dirs`, files outside said `refresh-dirs` won't be analyzed, resulting in safer, more efficient analysis.
910

10-
1111
## 2.5.1 (2021-02-16)
1212

1313
### Bugs fixed

src/refactor_nrepl/ns/pprint.clj

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
(vec (concat (remove sequential? libspecs)
1515
(filter sequential? libspecs))))
1616

17-
(defn- pprint-prefix-form [[name & libspecs]]
17+
(defn- pprint-libspec-with-prefix-form [[name & libspecs]]
1818
(printf "[%s" name)
1919
(let [ordered-libspecs (libspec-vectors-last libspecs)]
2020
(dorun
@@ -35,11 +35,22 @@
3535
(printf "%s " libspec))))))
3636
ordered-libspecs))))
3737

38-
(defn insert-clause-delimiter []
38+
(defn- insert-clause-delimiter []
3939
(if (:insert-newline-after-require *config*)
4040
(println)
4141
(print " ")))
4242

43+
(def ^:const ^:private as-or-refer-re-pattern
44+
(re-pattern (str "(:as|:refer)" (System/lineSeparator))))
45+
46+
(defn- pprint-libspec [libspec]
47+
;; If a vector gets too long `pprint` will print one element per line.
48+
;; This puts `:as` and `:refer` on their own line, which causes the ns form
49+
;; to take up too much vertical space.
50+
(printf (str/replace (with-out-str (pprint libspec))
51+
as-or-refer-re-pattern
52+
"$1")))
53+
4354
(defn pprint-require-form
4455
[[_ & libspecs]]
4556
(print "(:require")
@@ -48,13 +59,15 @@
4859
(map-indexed
4960
(fn [idx libspec]
5061
(if (= idx (dec (count libspecs)))
51-
(printf "%s)\n" (str/trim-newline
52-
(with-out-str (if (prefix-form? libspec)
53-
(pprint-prefix-form libspec)
54-
(pprint libspec)))))
62+
(printf "%s)\n"
63+
(str/trim-newline
64+
(with-out-str
65+
(if (prefix-form? libspec)
66+
(pprint-libspec-with-prefix-form libspec)
67+
(pprint libspec)))))
5568
(if (prefix-form? libspec)
56-
(pprint-prefix-form libspec)
57-
(pprint libspec))))
69+
(pprint-libspec-with-prefix-form libspec)
70+
(pprint-libspec libspec))))
5871
libspecs)))
5972

6073
(defn- form-is? [form type]

test/refactor_nrepl/ns/clean_ns_test.clj

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -144,25 +144,29 @@
144144
(is (= clean-requires requires))
145145
(is (= clean-imports imports))))
146146

147-
(def artifact-ns '(ns refactor-nrepl.artifacts
148-
(:require [clojure
149-
[edn :as edn]
150-
[string :as str]]
151-
[clojure.data.json :as json]
152-
[clojure.java.io :as io]
153-
[nrepl
154-
[middleware :refer [set-descriptor!]]
155-
[misc :refer [response-for]]
156-
[transport :as transport]]
157-
[org.httpkit.client :as http]
158-
[refactor-nrepl.externs :refer [add-dependencies]])
159-
(:import java.util.Date)))
147+
(def artifact-ns
148+
'(ns refactor-nrepl.artifacts
149+
(:require
150+
[clojure
151+
[edn :as edn]
152+
[string :as str]]
153+
[clojure.data.json :as json]
154+
[clojure.java.io :as io]
155+
[nrepl
156+
[middleware :refer [set-descriptor!]]
157+
[misc :refer [response-for]]
158+
[transport :as transport]]
159+
[org.httpkit.client
160+
:as very-very-very-very-long-alias-causing-line-wrap]
161+
[refactor-nrepl.externs :refer [add-dependencies]])
162+
(:import java.util.Date)))
160163

161164
(deftest test-pprint-artifact-ns
162-
(are [setting filename] (let [actual (config/with-config {:insert-newline-after-require setting}
163-
(pprint-ns (with-meta artifact-ns nil)))
164-
expected (-> filename File. .getAbsolutePath slurp)]
165-
(= expected actual))
165+
(are [setting filename]
166+
(let [actual (config/with-config {:insert-newline-after-require setting}
167+
(pprint-ns (with-meta artifact-ns nil)))
168+
expected (-> filename File. .getAbsolutePath slurp)]
169+
(= expected actual))
166170
true "test/resources/artifacts_pprinted"
167171
false "test/resources/artifacts_pprinted_traditional_newline"))
168172

@@ -200,13 +204,10 @@
200204
(deftest does-not-remove-ns-with-rename
201205
(is (= (nthrest ns-with-rename-cleaned 2) (nthrest (clean-ns ns-with-rename) 2))))
202206

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

211212
(deftest preserves-shorthand-meta
212213
(let [cleaned (pprint-ns (clean-ns ns-with-shorthand-meta))]

test/resources/artifacts_pprinted

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
[middleware :refer [set-descriptor!]]
1010
[misc :refer [response-for]]
1111
[transport :as transport]]
12-
[org.httpkit.client :as http]
12+
[org.httpkit.client
13+
:as very-very-very-very-long-alias-causing-line-wrap]
1314
[refactor-nrepl.externs :refer [add-dependencies]])
1415
(:import
1516
java.util.Date))

test/resources/artifacts_pprinted_traditional_newline

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[middleware :refer [set-descriptor!]]
99
[misc :refer [response-for]]
1010
[transport :as transport]]
11-
[org.httpkit.client :as http]
11+
[org.httpkit.client
12+
:as very-very-very-very-long-alias-causing-line-wrap]
1213
[refactor-nrepl.externs :refer [add-dependencies]])
1314
(:import java.util.Date))

test/resources/ns1_cleaned_and_pprinted

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
:methods [[binomial [int int] double]])
1111
(:require
1212
[clojure data edn xml
13+
[instant :as inst :reload true]
1314
[pprint :refer [cl-format formatter get-pretty-writer]]
15+
[string :refer :all :reload-all true]
16+
[test :refer :all]
1417
[walk :refer [postwalk prewalk]]]
1518
clojure.test.junit)
16-
(:import [java.io Closeable FilenameFilter PushbackReader]
17-
[java.util Calendar Date Random]
18-
[refactor.nrepl SomeClass$InnerClass$InnerInnerClassOne SomeClass$InnerClass$InnerInnerClassTwo]))
19+
(:import
20+
[java.io Closeable FilenameFilter PushbackReader]
21+
[java.util Calendar Date Random]
22+
[refactor.nrepl SomeClass$InnerClass$InnerInnerClassOne SomeClass$InnerClass$InnerInnerClassTwo]))

0 commit comments

Comments
 (0)