Skip to content

Commit 9fee2ed

Browse files
vemvbbatsov
authored andcommitted
Introduce :insert-newline-after-require option
Context: #300
1 parent 50ef3de commit 9fee2ed

File tree

7 files changed

+39
-8
lines changed

7 files changed

+39
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* [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.
6+
* You can opt out via the new `:insert-newline-after-require` configuration option.
67
* [#294](https://github.com/clojure-emacs/refactor-nrepl/pull/294): Properly skip uneval nodes when looking for the first/last sexp
78
* 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.
89

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ Configuration settings are passed along with each msg, currently the recognized
8888
;; Should `clean-ns` favor prefix forms in the ns macro?
8989
:prefix-rewriting true
9090

91+
;; Should `pprint-ns` place a newline after the `:require` and `:import` tokens?
92+
:insert-newline-after-require true
93+
9194
;; Some libspecs are side-effecting and shouldn't be pruned by `clean-ns`
9295
;; even if they're otherwise unused.
9396
;; This seq of strings will be used as regexp patterns to match

src/refactor_nrepl/config.clj

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
;; Should `clean-ns` favor prefix forms in the ns macro?
1616
:prefix-rewriting true
1717

18+
;; Should `pprint-ns` place a newline after the `:require` and `:import` tokens?
19+
:insert-newline-after-require true
20+
1821
;; Some libspecs are side-effecting and shouldn't be pruned by `clean-ns`
1922
;; even if they're otherwise unused.
2023
;; This seq of strings will be used as regexp patterns to match

src/refactor_nrepl/ns/pprint.clj

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[pprint :refer [pprint]]
55
[string :as str]]
66
[refactor-nrepl
7+
[config :refer [*config*]]
78
[core :as core :refer [prefix-form?]]
89
[util :as util :refer [replace-last]]])
910

@@ -34,9 +35,15 @@
3435
(printf "%s " libspec))))))
3536
ordered-libspecs))))
3637

38+
(defn insert-clause-delimiter []
39+
(if (:insert-newline-after-require *config*)
40+
(println)
41+
(print " ")))
42+
3743
(defn pprint-require-form
3844
[[_ & libspecs]]
39-
(print "(:require\n")
45+
(print "(:require")
46+
(insert-clause-delimiter)
4047
(dorun
4148
(map-indexed
4249
(fn [idx libspec]
@@ -107,7 +114,8 @@
107114

108115
(defn- pprint-import-form
109116
[[_ & imports]]
110-
(printf "(:import ")
117+
(print "(:import")
118+
(insert-clause-delimiter)
111119
(dorun
112120
(map-indexed
113121
(fn [idx import]

test/refactor_nrepl/ns/clean_ns_test.clj

+7-5
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,12 @@
159159
(:import java.util.Date)))
160160

161161
(deftest test-pprint-artifact-ns
162-
(let [path (.getAbsolutePath (File. "test/resources/artifacts_pprinted"))
163-
actual (pprint-ns (with-meta artifact-ns nil))
164-
expected (slurp path)]
165-
(is (= expected actual))))
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))
166+
true "test/resources/artifacts_pprinted"
167+
false "test/resources/artifacts_pprinted_traditional_newline"))
166168

167169
(deftest handles-imports-when-only-enum-is-used
168170
(let [new-ns (clean-ns ns2)
@@ -231,7 +233,7 @@
231233

232234
(deftest does-not-break-import-for-inner-class
233235
(let [cleaned (pprint-ns (clean-ns ns-with-inner-classes))]
234-
(is (re-find #":import.*Line2D\$Double" cleaned))))
236+
(is (re-find #":import\n.*Line2D\$Double" cleaned))))
235237

236238
(deftest fallback-to-relative-path
237239
(is (= (pprint-ns (clean-ns ns1))

test/resources/artifacts_pprinted

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
[transport :as transport]]
1212
[org.httpkit.client :as http]
1313
[refactor-nrepl.externs :refer [add-dependencies]])
14-
(:import java.util.Date))
14+
(:import
15+
java.util.Date))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(ns refactor-nrepl.artifacts
2+
(:require [clojure
3+
[edn :as edn]
4+
[string :as str]]
5+
[clojure.data.json :as json]
6+
[clojure.java.io :as io]
7+
[nrepl
8+
[middleware :refer [set-descriptor!]]
9+
[misc :refer [response-for]]
10+
[transport :as transport]]
11+
[org.httpkit.client :as http]
12+
[refactor-nrepl.externs :refer [add-dependencies]])
13+
(:import java.util.Date))

0 commit comments

Comments
 (0)