Skip to content

Commit c4313db

Browse files
arichiardibbatsov
authored andcommitted
Add shadow-select CLJS REPL type (#2397)
This kind of CLJS REPL is very helpful for cases when you are already watching one or more shadow builds in shadow either after having launched the server in a terminal or from within Cider. This patch also refactors option normalization out of figwheel-main so that it can be reused by every other function.
1 parent 623a669 commit c4313db

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [#2373](https://github.com/clojure-emacs/cider/issues/2373): Make it possible to configure the welcome message displayed in scratch buffers via `cider-scratch-initial-message`.
1919
* Add the ability to jump to the profiler buffer using `cider-selector`.
2020
* [#1980](https://github.com/clojure-emacs/cider/issues/1980): Echo back missing namespace name on interactive eval (requires nREPL 0.4.3+).
21+
* [#2397](https://github.com/clojure-emacs/cider/pull/2397): Add shadow-select CLJS REPL type.
2122
* [#2314](https://github.com/clojure-emacs/cider/pull/2314): Add `cider-ns-reload` and `cider-ns-reload-all` interactive commands.
2223

2324
### Bugs fixed

cider.el

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,38 @@ Generally you should not disable this unless you run into some faulty check."
682682
(unless (cider-library-present-p "thheller/shadow-cljs")
683683
(user-error "The shadow-cljs ClojureScript REPL is not available")))
684684

685+
(defun cider-normalize-cljs-init-options (options)
686+
"Normalize the OPTIONS string used for initializing a CLJS REPL."
687+
(if (or (string-prefix-p "{" options)
688+
(string-prefix-p "(" options)
689+
(string-prefix-p "[" options)
690+
(string-prefix-p ":" options))
691+
options
692+
(concat ":" options)))
693+
694+
(defcustom cider-shadow-default-options nil
695+
"Defines default `shadow-cljs' options."
696+
:type 'string
697+
:safe (lambda (s) (or (null s) (stringp s)))
698+
:package-version '(cider . "0.18.0"))
699+
700+
(defun cider-shadow-select-cljs-init-form ()
701+
"Generate the init form for a shadow-cljs select-only REPL.
702+
We have to prompt the user to select a build, that's why this is a command,
703+
not just a string."
704+
(let ((form "(do (require '[shadow.cljs.devtools.api :as shadow]) (shadow/nrepl-select %s))")
705+
(options (or cider-shadow-default-options
706+
(read-from-minibuffer "Select shadow-cljs build (e.g. dev): "))))
707+
(format form (cider-normalize-cljs-init-options options))))
708+
685709
(defun cider-shadow-cljs-init-form ()
686710
"Generate the init form for a shadow-cljs REPL.
687711
We have to prompt the user to select a build, that's why
688712
this is a command, not just a string."
689-
(let ((form "(do (require '[shadow.cljs.devtools.api :as shadow]) (shadow/watch :%s) (shadow/nrepl-select :%s))")
690-
(build (string-remove-prefix ":" (read-from-minibuffer "Select shadow-cljs build (e.g. dev): "))))
713+
(let* ((form "(do (require '[shadow.cljs.devtools.api :as shadow]) (shadow/watch %s) (shadow/nrepl-select %s))")
714+
(options (or cider-shadow-default-options
715+
(read-from-minibuffer "Select shadow-cljs build (e.g. dev): ")))
716+
(build (cider-normalize-cljs-init-options options)))
691717
(format form build build)))
692718

693719
(defcustom cider-figwheel-main-default-options nil
@@ -701,16 +727,11 @@ Figwheel for details."
701727

702728
(defun cider-figwheel-main-init-form ()
703729
"Produce the figwheel-main ClojureScript init form."
704-
(let* ((form "(do (require 'figwheel.main) (figwheel.main/start %s))")
705-
(options (string-trim
706-
(or cider-figwheel-main-default-options
707-
(read-from-minibuffer "Select figwheel-main build (e.g. :dev): "))))
708-
(normalized-options (if (or (string-prefix-p "{" options)
709-
(string-prefix-p "(" options)
710-
(string-prefix-p ":" options))
711-
options
712-
(concat ":" options))))
713-
(format form normalized-options)))
730+
(let ((form "(do (require 'figwheel.main) (figwheel.main/start %s))")
731+
(options (string-trim
732+
(or cider-figwheel-main-default-options
733+
(read-from-minibuffer "Select figwheel-main build (e.g. :dev): ")))))
734+
(format form (cider-normalize-cljs-init-options options))))
714735

715736
(defun cider-custom-cljs-repl-init-form ()
716737
"Prompt for a form that would start a ClojureScript REPL.
@@ -735,6 +756,7 @@ The supplied string will be wrapped in a do form if needed."
735756
(boot "(do (require 'adzerk.boot-cljs-repl) (adzerk.boot-cljs-repl/start-repl))"
736757
cider-check-boot-requirements)
737758
(shadow cider-shadow-cljs-init-form cider-check-shadow-cljs-requirements)
759+
(shadow-select cider-shadow-select-cljs-init-form cider-check-shadow-cljs-requirements)
738760
(custom cider-custom-cljs-repl-init-form nil))
739761
"A list of supported ClojureScript REPLs.
740762

doc/clojurescript.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ npx shadow-cljs server
254254
255255
And connect to it with `cider-connect`.
256256
257+
Lastly, if you already have a running server watching a build, for instance you
258+
have already run `npx shadow-cljs watch :dev`, you can use the `shadow-select`
259+
CLJS REPL and specify `:dev` when prompted.
260+
257261
[leiningen]: http://leiningen.org/
258262
[boot]: http://boot-clj.com/
259263
[piggieback]: https://github.com/nrepl/piggieback

test/cider-tests.el

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@
207207
:and-return-value '())
208208
(expect (cider-project-type) :to-equal cider-default-repl-command))))
209209

210+
(describe "cider-normalize-cljs-init-options"
211+
(describe "from options"
212+
(it "leaves keywords alone"
213+
(expect (cider-normalize-cljs-init-options ":dev") :to-equal ":dev"))
214+
(it "leaves maps alone"
215+
(expect (cider-normalize-cljs-init-options "{:a 1 :b 2}") :to-equal "{:a 1 :b 2}"))
216+
(it "leaves s-exprs alone"
217+
(expect (cider-normalize-cljs-init-options "(hashmap :a 1 :b 2)") :to-equal "(hashmap :a 1 :b 2)"))
218+
(it "leaves vectors alone"
219+
(expect (cider-normalize-cljs-init-options "[1 2 3]") :to-equal "[1 2 3]"))
220+
(it "prepends colon to plain names"
221+
(expect (cider-normalize-cljs-init-options "dev") :to-equal ":dev"))))
222+
210223
(describe "cider-figwheel-main-init-form"
211224
;; whitespace checks sprinkled amongst other tests
212225
(describe "from options"

0 commit comments

Comments
 (0)