Skip to content

Commit

Permalink
Read multiple forms from input
Browse files Browse the repository at this point in the history
  • Loading branch information
dan sutton committed Dec 10, 2018
1 parent 06fcf78 commit 9e9c38c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/cider/piggieback.clj
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@
(transport/send transport (response-for msg :status :done))
(alter-meta! session dissoc :thread :eval-msg)))))

(defn read-cljs-string [form-str]
(defn read-cljs-string
"Returns a sequence of forms read from form-str.
Allows for evaluating multiple forms in the same message. For
instance '(def x 1) (def x 2)` will return the sequence of both
forms rather than silently eliding the second form."
[form-str]
(when-not (string/blank? form-str)
(binding [*ns* (create-ns ana/*cljs-ns*)
reader/resolve-symbol ana/resolve-symbol
Expand All @@ -206,9 +211,15 @@
(apply merge
((juxt :requires :require-macros)
(ana/get-namespace ana/*cljs-ns*)))]
(reader/read {:read-cond :allow :features #{:cljs}}
(readers/source-logging-push-back-reader
(java.io.StringReader. form-str))))))
(let [rdr (readers/source-logging-push-back-reader
(java.io.StringReader. form-str))
eof (Object.)]
(take-while #(not= % eof)
(repeatedly
#(reader/read {:read-cond :allow
:features #{:cljs}
:eof eof}
rdr)))))))

(defn eval-cljs [repl-env env form opts]
(cljs.repl/evaluate-form repl-env
Expand Down Expand Up @@ -242,26 +253,27 @@
special-fns (merge cljs.repl/default-special-fns (:special-fns repl-options))
is-special-fn? (set (keys special-fns))]
(try
(let [form (read-cljs-string code)
env (assoc (ana/empty-env) :ns (ana/get-namespace init-ns))
result (when form
(if (and (seq? form) (is-special-fn? (first form)))
(do ((get special-fns (first form)) repl-env env form repl-options)
nil)
(eval-cljs repl-env env form repl-options)))]
(.flush ^Writer *out*)
(.flush ^Writer *err*)
(when (and
(or (not ns)
(not= init-ns ana/*cljs-ns*))
ana/*cljs-ns*)
(swap! session assoc #'ana/*cljs-ns* ana/*cljs-ns*))
(transport/send
transport
(response-for msg
{:value (or result "nil")
:printed-value 1
:ns (@session #'ana/*cljs-ns*)})))
(let [forms (read-cljs-string code)]
(doseq [form forms]
(let [env (assoc (ana/empty-env) :ns (ana/get-namespace init-ns))
result (when form
(if (and (seq? form) (is-special-fn? (first form)))
(do ((get special-fns (first form)) repl-env env form repl-options)
nil)
(eval-cljs repl-env env form repl-options)))]
(.flush ^Writer *out*)
(.flush ^Writer *err*)
(when (and
(or (not ns)
(not= init-ns ana/*cljs-ns*))
ana/*cljs-ns*)
(swap! session assoc #'ana/*cljs-ns* ana/*cljs-ns*))
(transport/send
transport
(response-for msg
{:value (or result "nil")
:printed-value 1
:ns (@session #'ana/*cljs-ns*)})))))
(catch Throwable t
(repl-caught session transport msg t repl-env repl-options)))))))

Expand Down
6 changes: 6 additions & 0 deletions test/cider/piggieback_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,9 @@
(is (-> (nrepl/message *session* {:op "eval" :code "(defprotocol Foo [do-a-thing [this]])"})
nrepl/combine-responses
:value)))

(deftest handles-multiple-forms
(is (= ["#'cljs.user/x" "#'cljs.user/y"]
(-> (nrepl/message *session* {:op "eval" :code "(def x 1) (def y 2)" :ns "cljs.user"})
nrepl/combine-responses
:value))))

0 comments on commit 9e9c38c

Please sign in to comment.