Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

184 lines (167 sloc) 7.196 kB
(ns reply.eval-modes.nrepl
(:import [java.util.concurrent ConcurrentLinkedQueue])
(:require [clojure.main]
[ :as nrepl.cmdline]
[ :as nrepl]
[ :as nrepl.misc]
[ :as nrepl.server]
[ :as nrepl.transport]
[reply.eval-state :as eval-state]
[reply.reader.jline :as reader.jline]
[reply.signals :as signals]
[net.cgrand.sjacket :as sjacket]
[net.cgrand.sjacket.parser :as sjacket.parser]))
(def current-command-id (atom nil))
(def current-session (atom nil))
(def current-ns (atom (str *ns*)))
(defn handle-client-interruption! [client]
(fn [sig]
(when-let [command-id @current-command-id]
(client {:op "interrupt"
:session @current-session
:interrupt-id command-id})))))
(def response-queues (atom {}))
(defn session-responses [session]
(cons (.poll (@response-queues session))
(session-responses session))))
(defn execute-with-client [client options form]
(let [command-id (nrepl.misc/uuid)
session (or (:session options) @current-session)
session-sender (nrepl/client-session client :session session)]
(session-sender {:op "eval" :code form :id command-id})
(reset! current-command-id command-id)
(doseq [{:keys [ns value out err] :as res}
#(not (and (= command-id (:id %))
(some #{"done" "interrupted" "error"} (:status %))))
(filter identity (session-responses session)))]
(when (some #{"need-input"} (:status res))
(let [input-result (.readLine *in*)]
{:op "stdin" :stdin (str input-result "\n")
:id (nrepl.misc/uuid)})))
(when value ((:value options print) value))
(when (and ns (not (:session options)))
(reset! current-ns ns)))
(when (:interactive options) (println))
(reset! current-command-id nil)
(defn parsed-forms
([request-exit] (parsed-forms request-exit nil))
([request-exit text-so-far]
(if-let [next-text (.readLine *in*)]
(let [concatted-text (if text-so-far
(str text-so-far \newline next-text)
parse-tree (sjacket.parser/parser concatted-text)]
(if (empty? (:content parse-tree))
(list "")
(let [completed? #(not= :net.cgrand.parsley/unfinished (:tag %))
complete-forms (take-while completed? (:content parse-tree))
remainder (drop-while completed? (:content parse-tree))
form-strings (map sjacket/str-pt
(remove #(contains? #{:whitespace :comment :discard}
(:tag %))
(cond (seq remainder)
(concat form-strings
(parsed-forms request-exit
(apply str (map sjacket/str-pt remainder)))))
(seq form-strings)
(list "")))))
(list request-exit))))
(defn run-repl
([connection] (run-repl connection nil))
([connection {:keys [prompt] :as options}]
(let [{:keys [major minor incremental qualifier]} *clojure-version*]
(loop [ns (execute-with-client connection options "")]
(prompt ns)
(let [eof (Object.)
execute (partial execute-with-client connection
(assoc options :interactive true))
forms (parsed-forms eof)]
(if (reply.exit/done? eof (first forms))
(recur (last (doall (map execute forms))))))))))
;; TODO: this could be less convoluted if we could break backwards-compat
(defn- url-for [attach host port]
(if (and attach (re-find #"^\w+://" attach))
(let [[port host] (if attach
(reverse (.split attach ":"))
[port host])]
(format "nrepl://%s:%s" (or host "localhost") port))))
(defn- load-drawbridge
"Load Drawbridge (and therefore get its HTTP/HTTPS multimethods
registered) if it's available."
(require '[cemerick.drawbridge.client])
(catch Exception e)))
(defn get-connection [{:keys [attach host port]}]
(let [port (if-not attach
(-> (nrepl.server/start-server :port (Integer. (or port 0)))
deref :ss .getLocalPort))
url (url-for attach host port)]
(when (-> url .getScheme .toLowerCase #{"http" "https"})
(nrepl/url-connect url)))
(defn completion-eval [client session form]
(let [results (atom "nil")]
{:value (partial reset! results)
:session session}
(pr-str `(binding [*ns* (symbol ~(deref current-ns))] ~form)))
(read-string @results)))
(defn poll-for-responses [connection]
(try (when-let [{:keys [out err] :as resp} (nrepl.transport/recv connection 100)]
(when err (print err))
(when out (print out))
(when-not (or err out)
(.offer (@response-queues (:session resp)) resp))
(catch Throwable t
(clojure.repl/pst t)
(recur connection))
(defn main
"Mostly ripped from nREPL's cmdline namespace."
(let [connection (get-connection options)
client (nrepl/client connection Long/MAX_VALUE)
session (nrepl/new-session client)
completion-session (nrepl/new-session client)]
(reset! current-session session)
(swap! response-queues assoc session (ConcurrentLinkedQueue.))
(swap! response-queues assoc completion-session (ConcurrentLinkedQueue.))
(let [options (assoc options :prompt
(fn [ns]
(partial completion-eval client completion-session)
options (if (:color options)
(merge options nrepl.cmdline/colored-output)
(.start (Thread. (partial poll-for-responses connection)))
(assoc options :value (constantly nil))
(pr-str (list 'do
'(set-signal-handler! "INT" (fn [s]))
(reply.initialization/construct-init-code options))))
(handle-client-interruption! client)
(run-repl client options))))
Jump to Line
Something went wrong with that request. Please try again.