Skip to content

Commit

Permalink
Introduce :insert-newline-after-require option
Browse files Browse the repository at this point in the history
  • Loading branch information
vemv committed Jun 27, 2021
1 parent 295448a commit 231aee5
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

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

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Configuration settings are passed along with each msg, currently the recognized
;; Should `clean-ns` favor prefix forms in the ns macro?
:prefix-rewriting true

;; Should `pprint-ns` place a newline after the `:require` and `:import` tokens?
:insert-newline-after-require true

;; Some libspecs are side-effecting and shouldn't be pruned by `clean-ns`
;; even if they're otherwise unused.
;; This seq of strings will be used as regexp patterns to match
Expand Down
3 changes: 3 additions & 0 deletions src/refactor_nrepl/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
;; Should `clean-ns` favor prefix forms in the ns macro?
:prefix-rewriting true

;; Should `pprint-ns` place a newline after the `:require` and `:import` tokens?
:insert-newline-after-require true

;; Some libspecs are side-effecting and shouldn't be pruned by `clean-ns`
;; even if they're otherwise unused.
;; This seq of strings will be used as regexp patterns to match
Expand Down
12 changes: 10 additions & 2 deletions src/refactor_nrepl/ns/pprint.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[pprint :refer [pprint]]
[string :as str]]
[refactor-nrepl
[config :refer [*config*]]
[core :as core :refer [prefix-form?]]
[util :as util :refer [replace-last]]])

Expand Down Expand Up @@ -34,9 +35,15 @@
(printf "%s " libspec))))))
ordered-libspecs))))

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

(defn pprint-require-form
[[_ & libspecs]]
(print "(:require\n")
(print "(:require")
(insert-clause-delimiter)
(dorun
(map-indexed
(fn [idx libspec]
Expand Down Expand Up @@ -107,7 +114,8 @@

(defn- pprint-import-form
[[_ & imports]]
(printf "(:import ")
(print "(:import")
(insert-clause-delimiter)
(dorun
(map-indexed
(fn [idx import]
Expand Down
12 changes: 7 additions & 5 deletions test/refactor_nrepl/ns/clean_ns_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,12 @@
(:import java.util.Date)))

(deftest test-pprint-artifact-ns
(let [path (.getAbsolutePath (File. "test/resources/artifacts_pprinted"))
actual (pprint-ns (with-meta artifact-ns nil))
expected (slurp path)]
(is (= 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"))

(deftest handles-imports-when-only-enum-is-used
(let [new-ns (clean-ns ns2)
Expand Down Expand Up @@ -231,7 +233,7 @@

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

(deftest fallback-to-relative-path
(is (= (pprint-ns (clean-ns ns1))
Expand Down
3 changes: 2 additions & 1 deletion test/resources/artifacts_pprinted
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
[transport :as transport]]
[org.httpkit.client :as http]
[refactor-nrepl.externs :refer [add-dependencies]])
(:import java.util.Date))
(:import
java.util.Date))
13 changes: 13 additions & 0 deletions test/resources/artifacts_pprinted_traditional_newline
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(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))

0 comments on commit 231aee5

Please sign in to comment.