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. Set
`cider-lein-*' variables using advice-driven let-binding instead of
global modification, and only inject nrepl-middleware when inside
project context. After REPL has started, if outside project context,
display more specific warning (unless the new defcustom is set to
non-nil).
  • Loading branch information
raxod502 committed Oct 15, 2017
1 parent f5295df commit 506add8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Remove *most* of the code implementing `let` related refactorings because these featured got migrated and reworked in Clojure mode. The usual mnemonics for these features still work and also a version of these features using multiple cursors are still maintained here as multiple cursors mode is not supported in Clojure mode.
- [#294](https://github.com/clojure-emacs/clj-refactor.el/issues/294) Remove dash library dependency.
- [#372](https://github.com/clojure-emacs/clj-refactor.el/issues/372) Remove s.el dependency
- [#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.

### Bugs fixed

Expand Down
53 changes: 33 additions & 20 deletions clj-refactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ as can be."
:group 'cljr
:type 'boolean)

(defcustom cljr-suppress-outside-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 and therefore the
refactor-nrepl middleware will not be injected."
: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 @@ -3131,15 +3139,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 (not (string-empty-p (cljr--project-dir)))
(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-outside-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-outside-project-warning'.)"))))

;;;###autoload
(defun cljr-version ()
Expand Down Expand Up @@ -3167,16 +3181,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 @@ -3992,18 +4000,23 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-change-function-
(pop-to-buffer cljr--change-signature-buffer))))

;;;###autoload
(defun cljr--inject-jack-in-dependencies ()
(defun cljr--inject-jack-in-dependencies (cider-jack-in &rest args)
"Inject the REPL dependencies of clj-refactor at `cider-jack-in'.
If injecting the dependencies is not preferred set `cljr-inject-dependencies-at-jack-in' to nil."
(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")))
(if (and (not (string-empty-p (cljr--project-dir)))
cljr-inject-dependencies-at-jack-in)
(let ((cider-jack-in-lein-plugins
(cons `("refactor-nrepl" ,(cljr--version t))
cider-jack-in-lein-plugins))
(cider-jack-in-nrepl-middlewares
(cons "refactor-nrepl.middleware/wrap-refactor"
cider-jack-in-nrepl-middlewares)))
(apply cider-jack-in args))
(apply cider-jack-in args)))

;;;###autoload
(eval-after-load 'cider
'(cljr--inject-jack-in-dependencies))
'(advice-add #'cider-jack-in :around #'cljr--inject-jack-in-dependencies))

(add-hook 'cider-connected-hook #'cljr--init-middleware)

Expand Down

0 comments on commit 506add8

Please sign in to comment.