diff --git a/CHANGELOG.md b/CHANGELOG.md index 30f4ec43..3b43d728 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Makefile b/Makefile index 09a6f9de..ee584764 100644 --- a/Makefile +++ b/Makefile @@ -3,19 +3,19 @@ VERSION ?= 1.10 .inline-deps: - lein inline-deps + lein with-profile -user inline-deps touch .inline-deps inline-deps: .inline-deps test: .inline-deps - lein with-profile +$(VERSION),+plugin.mranderson/config test + lein with-profile -user,+$(VERSION),+plugin.mranderson/config test cljfmt: - lein with-profile +$(VERSION),+cljfmt,+lein-plugin cljfmt check + lein with-profile -user,+$(VERSION),+cljfmt,+lein-plugin cljfmt check eastwood: - lein with-profile +$(VERSION),+eastwood eastwood + lein with-profile -user,+$(VERSION),+eastwood eastwood kondo: lein with-profile -dev,+$(VERSION),+clj-kondo run -m clj-kondo.main --lint src @@ -27,14 +27,14 @@ kondo: BUMP ?= patch release: - lein with-profile +$(VERSION),+lein-plugin release $(BUMP) + lein with-profile -user,+$(VERSION),+lein-plugin release $(BUMP) # Deploying requires the caller to set environment variables as # specified in project.clj to provide a login and password to the # artifact repository. deploy: .inline-deps - lein with-profile +$(VERSION),+plugin.mranderson/config,+lein-plugin deploy clojars + lein with-profile -user,+$(VERSION),+plugin.mranderson/config,+lein-plugin deploy clojars clean: lein clean diff --git a/README.md b/README.md index 3dd6b634..53bc327f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/project.clj b/project.clj index fd277c75..2f4f87a0 100644 --- a/project.clj +++ b/project.clj @@ -63,12 +63,12 @@ with-debug-bindings [[:inner 0]] merge-meta [[:inner 0]] try-if-let [[:block 1]]}}}] - :eastwood {:plugins [[jonase/eastwood "0.4.0"]] + :eastwood {:plugins [[jonase/eastwood "0.6.0"]] ;; TODO: add :test-paths :eastwood {:namespaces [:source-paths] ;; vendored - shouldn't be tweaked for satisfying linters: :exclude-namespaces [refactor-nrepl.ns.slam.hound.regrow] :exclude-linters [:unused-ret-vals]}} :clj-kondo [:test - {:dependencies [[clj-kondo "2021.03.31"]]}]} + {:dependencies [[clj-kondo "2021.06.18"]]}]} :jvm-opts ["-Djava.net.preferIPv4Stack=true"]) diff --git a/src/refactor_nrepl/config.clj b/src/refactor_nrepl/config.clj index e54a06c0..49e93da9 100644 --- a/src/refactor_nrepl/config.clj +++ b/src/refactor_nrepl/config.clj @@ -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 diff --git a/src/refactor_nrepl/ns/pprint.clj b/src/refactor_nrepl/ns/pprint.clj index 74a8052e..a35be0a3 100644 --- a/src/refactor_nrepl/ns/pprint.clj +++ b/src/refactor_nrepl/ns/pprint.clj @@ -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]]]) @@ -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] @@ -107,7 +114,8 @@ (defn- pprint-import-form [[_ & imports]] - (printf "(:import ") + (print "(:import") + (insert-clause-delimiter) (dorun (map-indexed (fn [idx import] diff --git a/test/refactor_nrepl/ns/clean_ns_test.clj b/test/refactor_nrepl/ns/clean_ns_test.clj index 548c866c..b6743993 100644 --- a/test/refactor_nrepl/ns/clean_ns_test.clj +++ b/test/refactor_nrepl/ns/clean_ns_test.clj @@ -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) @@ -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)) diff --git a/test/resources/artifacts_pprinted b/test/resources/artifacts_pprinted index 2ca303d8..73cf6f45 100644 --- a/test/resources/artifacts_pprinted +++ b/test/resources/artifacts_pprinted @@ -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)) diff --git a/test/resources/artifacts_pprinted_traditional_newline b/test/resources/artifacts_pprinted_traditional_newline new file mode 100644 index 00000000..e37be69c --- /dev/null +++ b/test/resources/artifacts_pprinted_traditional_newline @@ -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))