From b88dd5bd9c9aaa3e1b662b3ae781c13188484ea6 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Sun, 15 Oct 2017 10:51:16 -0700 Subject: [PATCH] [Fix #391] Add option for disabling warnings * 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). --- CHANGELOG.md | 1 + clj-refactor.el | 45 +++++++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37c74421..41a0b723 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/clj-refactor.el b/clj-refactor.el index 2a2f2be8..da672fbc 100644 --- a/clj-refactor.el +++ b/clj-refactor.el @@ -8,7 +8,7 @@ ;; Benedek Fazekas ;; 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 @@ -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. @@ -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))) @@ -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 () @@ -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 @@ -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