Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Revert "Upgrade to nREPL 0.2.0"
This reverts commit 3f3a5c9.
  • Loading branch information
trptcolin committed Feb 12, 2012
1 parent 88e9a13 commit 0b002ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
2 changes: 1 addition & 1 deletion project.clj
Expand Up @@ -5,7 +5,7 @@
[org.thnetos/cd-client "0.3.1" :exclusions [org.clojure/clojure]]
[clj-stacktrace "0.2.4"]
[clojure-complete "0.1.4" :exclusions [org.clojure/clojure]]
[org.clojure/tools.nrepl "0.2.0-SNAPSHOT"]]
[org.clojure/tools.nrepl "0.0.6-SNAPSHOT"]]
:dev-dependencies [[midje "1.3-alpha4" :exclusions [org.clojure/clojure]]
[lein-midje "[1.0.0,)"]]
:repositories {
Expand Down
69 changes: 27 additions & 42 deletions src/clj/reply/evaluation/nrepl.clj
@@ -1,41 +1,31 @@
(ns reply.evaluation.nrepl
(:require [clojure.tools.nrepl.cmdline :as nrepl.cmdline]
[clojure.tools.nrepl :as nrepl]
[clojure.tools.nrepl.misc :as nrepl.misc]
[clojure.tools.nrepl.server :as nrepl.server]
[clojure.tools.nrepl.transport :as nrepl.transport]
[reply.concurrency]
[reply.eval-state]
[reply.initialization]
[reply.reader.jline :as reader.jline]))

(def current-command-id (atom nil))
(def current-session (atom nil))
(def current-ns (atom (str *ns*)))
(def current-command (atom (fn ([]) ([x]))))

(defn handle-client-interruption! [client]
(defn handle-client-interruption! []
(reply.concurrency/set-signal-handler!
"INT"
(fn [sig] (print "^C") (flush)
(when-let [command-id @current-command-id]
(client {:op "interrupt" :session @current-session :interrupt-id command-id})))))
(fn [sig] (println "^C")
(@current-command :interrupt))))

(defn execute-with-client [client options form]
(let [command-id (nrepl.misc/uuid)
response-seq (nrepl/message client {:op "eval" :code form :id command-id :session @current-session})]
(reset! current-command-id command-id)
(doall (for [{:keys [ns value out err] :as res}
(take-while #(not (some #{"done" "interrupted" "error" "eval-error"} (:status %)))
response-seq)]
(defn execute-with-connection [connection options form]
(let [response-fn ((:send connection) form)]
(reset! current-command response-fn)
(for [{:keys [ns value out err] :as res} (nrepl/response-seq response-fn)]
(do
(when (= "done" (:status res))
(reset! current-command {}))
(when value ((:value options print) value))
(when out ((:out options print) out))
(when err ((:err options print) err))
(flush)
(when ns (reset! current-ns ns)))))
(println)
(reset! current-command-id nil)
@current-ns))
ns))))

(defn run-repl
([connection] (run-repl connection nil))
Expand All @@ -51,56 +41,51 @@
done
(prn e))))]
(if (= done read-result) nil
(recur (execute-with-client
(recur (last (execute-with-connection
connection
options
(pr-str read-result)))))))))
(pr-str read-result))))))))))

(defn get-connection [options]
(let [port (if-let [attach-port (:attach options)]
(Integer/parseInt attach-port)
(-> (nrepl.server/start-server :port (Integer/parseInt (or (:port options) "0")))
deref
:ss
.getLocalPort))]
(nrepl/connect :host (or (:host options) "localhost") :port port)))
(let [[ssocket _] (nrepl/start-server (Integer/parseInt (or (:port options) "0")))]
(.getLocalPort ssocket)))]
(nrepl/connect "localhost" port)))

(defn adhoc-eval [client form]
(defn adhoc-eval [connection form]
(let [results (atom "nil")]
(execute-with-client
client
(doall (execute-with-connection
connection
{:value (partial reset! results)
:out print
:err print}
(pr-str form))
(pr-str form)))
(read-string @results)))

(defn main
"Ripped from nREPL. The only changes are in namespacing, running of initial
code, and options."
[options]
(let [connection (get-connection options)
client (nrepl/client connection 10000)
session (nrepl/new-session client)]
(reset! current-session session)
(let [connection (get-connection options)]
(let [options (assoc options :prompt
(fn [ns]
(binding [*ns* (create-ns (symbol ns))]
(reply.eval-state/set-bindings!))
(reader.jline/prepare-for-read
(partial adhoc-eval client))))
(partial adhoc-eval connection))))
options (if (:color options)
(merge options nrepl.cmdline/colored-output)
options)]
(execute-with-client
client
(doall (execute-with-connection
connection
options
(pr-str (list 'do
(reply.initialization/construct-init-code options)
(reply.initialization/export-definition 'reply.concurrency/set-signal-handler!)
'(set-signal-handler! "INT" (fn [s]))
nil)))
nil))))

(handle-client-interruption! client)
(run-repl client options))))
(handle-client-interruption!)
(run-repl connection options))))

0 comments on commit 0b002ee

Please sign in to comment.