Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #391] Add option for disabling warnings #392

Merged
merged 1 commit into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
50 changes: 34 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 printed 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 ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this should be called cljr--inject-middleware-p? to better reflect its usage.

Also, shouldn't it have an argument list like (&rest _) since it's actually expecting 3 arguments, that are ignored?

Edit: I see it's used in two places, so I'd suggest leaving this and creating another function, cljr--inject-middleware-p that just has the right argument list and calls out to this one.

"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 @@ -4002,15 +4013,22 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-change-function-
(kill-buffer cljr--manual-intervention-buffer))
(pop-to-buffer cljr--change-signature-buffer))))

;;;###autoload
(defun cljr--inject-middleware-p (&rest _)
"Return non-nil if nREPL middleware should be injected."
(cljr--inside-project-p))

;;;###autoload
(defun cljr--inject-jack-in-dependencies ()
"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")))
(add-to-list 'cider-jack-in-lein-plugins `("refactor-nrepl" ,(cljr--version t)
:predicate cljr--inject-middleware-p))
(add-to-list 'cider-jack-in-nrepl-middlewares '("refactor-nrepl.middleware/wrap-refactor"
:predicate cljr--inject-middleware-p))))

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