Skip to content

Commit

Permalink
Add shadow-select CLJS REPL type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arichiardi committed Aug 2, 2018
1 parent a1f86b3 commit e2f0d95
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [#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`.
* Add the ability to jump to the profiler buffer using `cider-selector`.
* [#1980](https://github.com/clojure-emacs/cider/issues/1980): Echo back missing namespace name on interactive eval (requires nREPL 0.4.3+).
* [#2397](https://github.com/clojure-emacs/cider/pull/2397): Add shadow-select CLJS REPL type.

### Bugs fixed

Expand Down
46 changes: 34 additions & 12 deletions cider.el
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,38 @@ Generally you should not disable this unless you run into some faulty check."
(unless (cider-library-present-p "thheller/shadow-cljs")
(user-error "The shadow-cljs ClojureScript REPL is not available")))

(defun cider-normalize-cljs-init-options (options)
"Normalize the OPTIONS string used for initializing a CLJS REPL."
(if (or (string-prefix-p "{" options)
(string-prefix-p "(" options)
(string-prefix-p "[" options)
(string-prefix-p ":" options))
options
(concat ":" options)))

(defcustom cider-shadow-default-options nil
"Defines default `shadow-cljs' options."
:type 'string
:safe (lambda (s) (or (null s) (stringp s)))
:package-version '(cider . "0.18.0"))

(defun cider-shadow-select-cljs-init-form ()
"Generate the init form for a shadow-cljs select-only REPL.
We have to prompt the user to select a build, that's why this is a command,
not just a string."
(let ((form "(do (require '[shadow.cljs.devtools.api :as shadow]) (shadow/nrepl-select :%s))")
(options (or cider-shadow-default-options
(read-from-minibuffer "Select shadow-cljs build (e.g. dev): "))))
(format form (cider-normalize-cljs-init-options options))))

(defun cider-shadow-cljs-init-form ()
"Generate the init form for a shadow-cljs REPL.
We have to prompt the user to select a build, that's why
this is a command, not just a string."
(let ((form "(do (require '[shadow.cljs.devtools.api :as shadow]) (shadow/watch :%s) (shadow/nrepl-select :%s))")
(build (string-remove-prefix ":" (read-from-minibuffer "Select shadow-cljs build (e.g. dev): "))))
(let* ((form "(do (require '[shadow.cljs.devtools.api :as shadow]) (shadow/watch :%s) (shadow/nrepl-select :%s))")
(options (or cider-shadow-default-options
(read-from-minibuffer "Select shadow-cljs build (e.g. dev): ")))
(build (cider-normalize-cljs-init-options options)))
(format form build build)))

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

(defun cider-figwheel-main-init-form ()
"Produce the figwheel-main ClojureScript init form."
(let* ((form "(do (require 'figwheel.main) (figwheel.main/start %s))")
(options (string-trim
(or cider-figwheel-main-default-options
(read-from-minibuffer "Select figwheel-main build (e.g. :dev): "))))
(normalized-options (if (or (string-prefix-p "{" options)
(string-prefix-p "(" options)
(string-prefix-p ":" options))
options
(concat ":" options))))
(format form normalized-options)))
(let ((form "(do (require 'figwheel.main) (figwheel.main/start %s))")
(options (string-trim
(or cider-figwheel-main-default-options
(read-from-minibuffer "Select figwheel-main build (e.g. :dev): ")))))
(format form (cider-normalize-cljs-init-options options))))

(defun cider-custom-cljs-repl-init-form ()
"Prompt for a form that would start a ClojureScript REPL.
Expand All @@ -735,6 +756,7 @@ The supplied string will be wrapped in a do form if needed."
(boot "(do (require 'adzerk.boot-cljs-repl) (adzerk.boot-cljs-repl/start-repl))"
cider-check-boot-requirements)
(shadow cider-shadow-cljs-init-form cider-check-shadow-cljs-requirements)
(shadow-select cider-shadow-select-cljs-init-form cider-check-shadow-cljs-requirements)
(custom cider-custom-cljs-repl-init-form nil))
"A list of supported ClojureScript REPLs.
Expand Down
4 changes: 4 additions & 0 deletions doc/clojurescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ npx shadow-cljs server
And connect to it with `cider-connect`.
Lastly, if you already have a running server watching a build, for instance you
have already run `npx shadow-cljs watch :dev`, you can use the `shadow-select`
CLJS REPL and specify `:dev` when prompted.
[leiningen]: http://leiningen.org/
[boot]: http://boot-clj.com/
[piggieback]: https://github.com/nrepl/piggieback
Expand Down
13 changes: 13 additions & 0 deletions test/cider-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@
:and-return-value '())
(expect (cider-project-type) :to-equal cider-default-repl-command))))

(describe "cider-normalize-cljs-init-options"
(describe "from options"
(it "leaves keywords alone"
(expect (cider-normalize-cljs-init-options ":dev") :to-equal ":dev"))
(it "leaves maps alone"
(expect (cider-normalize-cljs-init-options "{:a 1 :b 2}") :to-equal "{:a 1 :b 2}"))
(it "leaves s-exprs alone"
(expect (cider-normalize-cljs-init-options "(hashmap :a 1 :b 2)") :to-equal "(hashmap :a 1 :b 2)"))
(it "leaves vectors alone"
(expect (cider-normalize-cljs-init-options "[1 2 3]") :to-equal "[1 2 3]"))
(it "prepends colon to plain names"
(expect (cider-normalize-cljs-init-options "dev") :to-equal ":dev"))))

(describe "cider-figwheel-main-init-form"
;; whitespace checks sprinkled amongst other tests
(describe "from options"
Expand Down

0 comments on commit e2f0d95

Please # to comment.