From 548be3de3322c6a5658409d133cb4cdf0fefaf7c Mon Sep 17 00:00:00 2001 From: dan sutton Date: Thu, 7 Nov 2019 22:52:22 -0600 Subject: [PATCH] Show startup commands in repl buffer --- CHANGELOG.md | 1 + cider-connection.el | 4 +++- cider-repl.el | 27 +++++++++++++++++++++++++++ cider.el | 31 +++++++++++++++++-------------- test/cider-repl-tests.el | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d4916a0a..fd54cda7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### New features +* [#2744](https://github.com/clojure-emacs/cider/pull/2744): Add startup commands to repl banner. * [#2499](https://github.com/clojure-emacs/cider/issues/2499): Add `cider-jump-to-pop-to-buffer-actions`. * [#2738](https://github.com/clojure-emacs/cider/pull/2738): Add ability to lookup a function symbol when cursor is at the opening paren. * [#2735](https://github.com/clojure-emacs/cider/pull/2735): New debugger command `P` to inspect an arbitrary expression, it was previously bound to `p` which now inspects the current value. diff --git a/cider-connection.el b/cider-connection.el index c72d62108..a47fe7a47 100644 --- a/cider-connection.el +++ b/cider-connection.el @@ -699,6 +699,7 @@ PARAMS is a plist as received by `cider-repl-create'." (declare-function cider-repl-reset-markers "cider-repl") (defvar-local cider-session-name nil) (defvar-local cider-repl-init-function nil) +(defvar-local cider-launch-params nil) (defun cider-repl-create (params) "Create new repl buffer. PARAMS is a plist which contains :repl-type, :host, :port, :project-dir, @@ -727,7 +728,8 @@ function with the repl buffer set as current." ;; REPLs start with clj and then "upgrade" to a different type cider-repl-type (plist-get params :repl-type) ;; ran at the end of cider--connected-handler - cider-repl-init-function (plist-get params :repl-init-function)) + cider-repl-init-function (plist-get params :repl-init-function) + cider-launch-params params) (cider-repl-reset-markers) (add-hook 'nrepl-response-handler-functions #'cider-repl--state-handler nil 'local) (add-hook 'nrepl-connected-hook #'cider--connected-handler nil 'local) diff --git a/cider-repl.el b/cider-repl.el index c6d2edf16..e2efb68b4 100644 --- a/cider-repl.el +++ b/cider-repl.el @@ -319,6 +319,7 @@ fully initialized." ((pred identity) (pop-to-buffer buffer))) (with-current-buffer buffer (cider-repl--insert-banner) + (cider-repl--insert-startup-commands) (when-let* ((window (get-buffer-window buffer t))) (with-selected-window window (recenter (- -1 scroll-margin)))) @@ -333,6 +334,32 @@ fully initialized." (insert-before-markers (propertize (cider-repl--help-banner) 'font-lock-face 'font-lock-comment-face)))) +(defun cider-repl--insert-startup-commands () + "Insert the values from params specified in PARAM-TUPLES. +PARAM-TUPLES are tuples of (param-key description) or (param-key +description transform) where transform is called with the param-value if +present." + (cl-labels + ((emit-comment + (contents) + (insert-before-markers + (propertize + (if (string-blank-p contents) ";;\n" (concat ";; " contents "\n")) + 'font-lock-face 'font-lock-comment-face)))) + (let ((jack-in-command (plist-get cider-launch-params :jack-in-cmd)) + (cljs-repl-type (plist-get cider-launch-params :cljs-repl-type)) + (cljs-init-form (plist-get cider-launch-params :repl-init-form))) + (when jack-in-command + ;; spaces to align with the banner + (emit-comment (concat " Startup: " jack-in-command))) + (when (or cljs-repl-type cljs-init-form) + (emit-comment "") + (when cljs-repl-type + (emit-comment (concat "ClojureScript REPL type: " (symbol-name cljs-repl-type)))) + (when cljs-init-form + (emit-comment (concat "ClojureScript REPL init form: " cljs-init-form))) + (emit-comment ""))))) + (defun cider-repl--banner () "Generate the welcome REPL buffer banner." (format ";; Connected to nREPL server - nrepl://%s:%s diff --git a/cider.el b/cider.el index 44eeb2731..149788497 100644 --- a/cider.el +++ b/cider.el @@ -1283,20 +1283,23 @@ non-nil, don't start if ClojureScript requirements are not met." "Update PARAMS :repl-init-function for cljs connections." (with-current-buffer (or (plist-get params :--context-buffer) (current-buffer)) - (let ((cljs-type (plist-get params :cljs-repl-type))) - (plist-put params :repl-init-function - (lambda () - (cider--check-cljs cljs-type) - ;; FIXME: ideally this should be done in the state handler - (setq-local cider-cljs-repl-type cljs-type) - (cider-nrepl-send-request - (list "op" "eval" - "ns" (cider-current-ns) - "code" (cider-cljs-repl-form cljs-type)) - (cider-repl-handler (current-buffer))) - (when (and (buffer-live-p nrepl-server-buffer) - cider-offer-to-open-cljs-app-in-browser) - (cider--offer-to-open-app-in-browser nrepl-server-buffer))))))) + (let* ((cljs-type (plist-get params :cljs-repl-type)) + (repl-init-form (cider-cljs-repl-form cljs-type))) + (thread-first params + (plist-put :repl-init-function + (lambda () + (cider--check-cljs cljs-type) + ;; FIXME: ideally this should be done in the state handler + (setq-local cider-cljs-repl-type cljs-type) + (cider-nrepl-send-request + (list "op" "eval" + "ns" (cider-current-ns) + "code" repl-init-form) + (cider-repl-handler (current-buffer))) + (when (and (buffer-live-p nrepl-server-buffer) + cider-offer-to-open-cljs-app-in-browser) + (cider--offer-to-open-app-in-browser nrepl-server-buffer)))) + (plist-put :repl-init-form repl-init-form))))) (defun cider--check-existing-session (params) "Ask for confirmation if a session with similar PARAMS already exists. diff --git a/test/cider-repl-tests.el b/test/cider-repl-tests.el index 330d4e28f..2d97cfa8a 100644 --- a/test/cider-repl-tests.el +++ b/test/cider-repl-tests.el @@ -30,6 +30,38 @@ (require 'buttercup) (require 'cider-repl) +(describe "cider-repl--insert-param-values" + (it "doesn't output anything when the params aren't present" + (let ((output "") + (cider-launch-params '())) + (spy-on 'insert-before-markers + :and-call-fake (lambda (arg) + (setq output (format "%s%s" output (substring-no-properties arg))))) + (cider-repl--insert-startup-commands) + (expect output :to-be ""))) + (it "puts jack-in-command in same style as banner" + (let ((output "") + (cider-launch-params '(:jack-in-cmd "lein command"))) + (spy-on 'insert-before-markers + :and-call-fake (lambda (arg) + (setq output (format "%s%s" output (substring-no-properties arg))))) + (cider-repl--insert-startup-commands) + (expect output :to-equal + ";; Startup: lein command\n"))) + (it "formats output if present" + (let ((output "") + (cider-launch-params '(:cljs-repl-type shadow :repl-init-form "(do)"))) + (spy-on 'insert-before-markers + :and-call-fake (lambda (arg) + (setq output (format "%s%s" output (substring-no-properties arg))))) + (cider-repl--insert-startup-commands) + (expect output :to-equal + ";; +;; ClojureScript REPL type: shadow +;; ClojureScript REPL init form: (do) +;; +")))) + (describe "cider-repl--banner" :var (cider-version cider-codename) (before-all