Skip to content

Commit

Permalink
[Fix #391] Add option for disabling warnings
Browse files Browse the repository at this point in the history
* Add `cljr-suppress-outside-project-warning' defcustom.
* Use new `:predicate' support in `cider-jack-in-lein-plugins' and
  `cider-jack-in-nrepl-middlewares' to only inject when inside project
  context. (Bump CIDER dependency to 0.17.0.)
* After REPL has started, if outside project context, display better
  warning than before (unless the new defcustom is set to non-nil).
  • Loading branch information
raxod502 committed Mar 14, 2018
1 parent 408ab1f commit b88dd5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 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

- [#394](https://github.com/clojure-emacs/clj-refactor.el/issues/394) New config option `cljr-assume-language-context`: by default, when clj-refactor encounters an ambiguous context (clj vs cljs) it creates a popup asking user which context is meant. If this option is changed to "clj" or "cljs", clj-refactor will use that as the assumed context in such ambigous cases.
- [#391](https://github.com/clojure-emacs/clj-refactor.el/issues/391) Prevent refactor-nrepl from being injected when starting a REPL outside a project, and add an option `cljr-suppress-outside-project-warning` to suppress the resultant warning.

## 2.3.1

Expand Down
45 changes: 29 additions & 16 deletions clj-refactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
;; Benedek Fazekas <[email protected]>
;; Version: 2.4.0-SNAPSHOT
;; Keywords: convenience, clojure, cider
;; Package-Requires: ((emacs "24.4") (s "1.8.0") (seq "2.19") (yasnippet "0.6.1") (paredit "24") (multiple-cursors "1.2.2") (clojure-mode "5.6.1") (cider "0.15.0") (edn "1.1.2") (inflections "2.3") (hydra "0.13.2"))
;; Package-Requires: ((emacs "24.4") (s "1.8.0") (seq "2.19") (yasnippet "0.6.1") (paredit "24") (multiple-cursors "1.2.2") (clojure-mode "5.6.1") (cider "0.17.0") (edn "1.1.2") (inflections "2.3") (hydra "0.13.2"))

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -155,6 +155,13 @@ as can be."
:group 'cljr
:type 'boolean)

(defcustom cljr-suppress-no-project-warning nil
"If t, no warning is printed when starting a REPL outside a project.
By default, a warning is printing in this case since clj-refactor
will not work as expected in such REPLs."
:group 'cljr
:type 'boolean)

(defcustom cljr-find-usages-ignore-analyzer-errors nil
"DEPRECATED: use `cljr-ignore-analyzer-errors' instead.
If t, `cljr-find-usages' ignores namespaces that cannot be analyzed.
Expand Down Expand Up @@ -786,6 +793,10 @@ A new record is created to define this constructor."
car)
""))

(defun cljr--inside-project-p ()
"Return non-nil if `default-directory' is inside a Clojure project."
(not (string-empty-p (cljr--project-dir))))

(defun cljr--project-file ()
(let ((project-dir (cljr--project-dir)))
(or (let ((file (expand-file-name "project.clj" project-dir)))
Expand Down Expand Up @@ -3142,15 +3153,21 @@ if REMOVE-PACKAGE_VERSION is t get rid of the (package: 20150828.1048) suffix."

(defun cljr--check-middleware-version ()
"Check whether clj-refactor and nrepl-refactor versions are the same"
(let ((refactor-nrepl-version (or (cljr--middleware-version)
"n/a")))
(unless (string-equal (downcase refactor-nrepl-version)
(downcase (cljr--version :remove-package-version)))
(cider-repl-emit-interactive-stderr
(format "WARNING: clj-refactor and refactor-nrepl are out of sync.
(if (cljr--inside-project-p)
(let ((refactor-nrepl-version (or (cljr--middleware-version)
"n/a")))
(unless (string-equal (downcase refactor-nrepl-version)
(downcase (cljr--version :remove-package-version)))
(cider-repl-emit-interactive-stderr
(format "WARNING: clj-refactor and refactor-nrepl are out of sync.
Their versions are %s and %s, respectively.
You can mute this warning by changing cljr-suppress-middleware-warnings."
(cljr--version) refactor-nrepl-version)))))
(cljr--version) refactor-nrepl-version))))
(unless cljr-suppress-no-project-warning
(cider-repl-emit-interactive-stderr
"WARNING: No Clojure project was detected. The
refactor-nrepl middleware was not enabled. (You can mute this
warning by customizing `cljr-suppress-no-project-warning'.)"))))

;;;###autoload
(defun cljr-version ()
Expand Down Expand Up @@ -3178,16 +3195,10 @@ You can mute this warning by changing cljr-suppress-middleware-warnings."
(cider-repl-emit-interactive-stderr
(format "WARNING: Can't determine Clojure version. The refactor-nrepl middleware requires clojure %s (or newer)" cljr-minimum-clojure-version))))

(defun cljr--check-project-context ()
(unless (cljr--project-dir)
(cider-repl-emit-interactive-stderr
(format "WARNING: No clojure project detected. The refactor-nrepl middleware won't work and has been disabled!"))))

(defun cljr--init-middleware ()
(unless cljr-suppress-middleware-warnings
(cljr--check-clojure-version)
(cljr--check-middleware-version))
(cljr--check-project-context)
;; Best effort; don't freak people out with errors
(ignore-errors
(when (cljr--middleware-version) ; check if middleware is running
Expand Down Expand Up @@ -4009,8 +4020,10 @@ If injecting the dependencies is not preferred set `cljr-inject-dependencies-at-
(when (and cljr-inject-dependencies-at-jack-in
(boundp 'cider-jack-in-lein-plugins)
(boundp 'cider-jack-in-nrepl-middlewares))
(add-to-list 'cider-jack-in-lein-plugins `("refactor-nrepl" ,(cljr--version t)))
(add-to-list 'cider-jack-in-nrepl-middlewares "refactor-nrepl.middleware/wrap-refactor")))
(add-to-list 'cider-jack-in-lein-plugins `("refactor-nrepl" ,(cljr--version t)
:predicate #'cljr--inside-project-p))
(add-to-list 'cider-jack-in-nrepl-middlewares '("refactor-nrepl.middleware/wrap-refactor"
:predicate #'cljr--inside-project-p))))

;;;###autoload
(eval-after-load 'cider
Expand Down

0 comments on commit b88dd5b

Please sign in to comment.