Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read multiple forms from input #98

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## `Master`

* [#98](https://github.com/nrepl/piggieback/pull/98): Read multiple forms from input.

## `0.3.10`

* [#95](https://github.com/nrepl/piggieback/issues/95): Bind `*cljs-warnings*`.
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should also document this in the docstring of the function just so it's clear to everyone how this is supposed to behave.

(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))))