Bump sci.nrepl and and use it (#81)
This commit is contained in:
parent
691ed3a31a
commit
a99cf25226
2
deps.edn
2
deps.edn
|
@ -15,7 +15,7 @@
|
|||
io.github.babashka/sci.nrepl
|
||||
#_{:local/root "../sci.nrepl"}
|
||||
{:git/url "https://github.com/babashka/sci.nrepl"
|
||||
:git/sha "2f8a9ed2d39a1b09d2b4d34d95494b56468f4a23"}
|
||||
:git/sha "75f379c685bbd58c3e23f531339eb144e104937d"}
|
||||
io.github.babashka/sci.configs
|
||||
#_{:local/root "/Users/borkdude/dev/sci.configs"}
|
||||
{:git/url "https://github.com/babashka/sci.configs"
|
||||
|
|
|
@ -1,75 +1,7 @@
|
|||
(ns scittle.nrepl
|
||||
(:require
|
||||
[clojure.edn :as edn]
|
||||
[sci.ctx-store :as store]
|
||||
[sci.nrepl.completions :refer [completions]]
|
||||
[sci.nrepl.info :refer [info]]
|
||||
[scittle.core :refer [!last-ns eval-string]]))
|
||||
|
||||
(defn nrepl-websocket []
|
||||
(.-ws_nrepl js/window))
|
||||
|
||||
(defn nrepl-reply [{:keys [id session]} {:keys [ns] :as payload}]
|
||||
(.send (nrepl-websocket)
|
||||
(str
|
||||
(let [ns (or ns (str @!last-ns))]
|
||||
(-> (assoc payload :id id :session session :ns ns)
|
||||
(dissoc :ctx))))))
|
||||
|
||||
(defn handle-nrepl-eval [{:keys [code] :as msg}]
|
||||
(let [[kind val] (try [::success (eval-string code)]
|
||||
(catch :default e
|
||||
[::error (str e)]))]
|
||||
(case kind
|
||||
::success
|
||||
(do (nrepl-reply msg {:value (pr-str val)})
|
||||
(nrepl-reply msg {:status ["done"]}))
|
||||
::error
|
||||
(do
|
||||
(nrepl-reply msg {:err (pr-str val)})
|
||||
(nrepl-reply msg {:ex (pr-str val)
|
||||
:status ["error" "done"]})))))
|
||||
|
||||
(defn handle-nrepl-info [msg]
|
||||
(let [info (info (assoc msg :ctx (store/get-ctx)))]
|
||||
(nrepl-reply msg info)))
|
||||
|
||||
(declare ops)
|
||||
|
||||
(defn
|
||||
handle-describe
|
||||
[msg]
|
||||
(nrepl-reply
|
||||
msg
|
||||
{:versions {"scittle-nrepl" {"major" "0"
|
||||
"minor" "0"
|
||||
"incremental" "1"}}
|
||||
:ops (zipmap
|
||||
(map
|
||||
name
|
||||
(concat
|
||||
(keys ops)
|
||||
;; sci.nrepl browser_server.clj handles:
|
||||
#{:clone :load-file}
|
||||
;; we are lying about close?
|
||||
#{"close"}))
|
||||
(repeat {}))
|
||||
:status ["done"]}))
|
||||
|
||||
(def ops
|
||||
"Operations supported by the nrepl server."
|
||||
{:eval handle-nrepl-eval
|
||||
:info handle-nrepl-info
|
||||
:eldoc handle-nrepl-info
|
||||
:lookup handle-nrepl-info
|
||||
:describe handle-describe
|
||||
:complete (fn [msg] (let [completions (completions (assoc msg :ctx (store/get-ctx)))]
|
||||
(nrepl-reply msg completions)))})
|
||||
|
||||
(defn handle-nrepl-message [msg]
|
||||
(if-let [handler (ops (:op msg))]
|
||||
(handler msg)
|
||||
(nrepl-reply msg (merge msg {:status ["error" "done"] :err "unkown-op"}))))
|
||||
[sci.nrepl.server :as nrepl-server]))
|
||||
|
||||
(defn ws-url [host port path]
|
||||
(str "ws://" host ":" port "/" path))
|
||||
|
@ -78,11 +10,11 @@
|
|||
(set! (.-ws_nrepl js/window)
|
||||
(new js/WebSocket (ws-url (.-hostname (.-location js/window)) ws-port "_nrepl"))))
|
||||
|
||||
(when-let [ws (nrepl-websocket)]
|
||||
(when-let [ws (nrepl-server/nrepl-websocket)]
|
||||
(prn :ws ws)
|
||||
(set! (.-onmessage ws)
|
||||
(fn [event]
|
||||
(handle-nrepl-message (edn/read-string (.-data event)))))
|
||||
(nrepl-server/handle-nrepl-message (edn/read-string (.-data event)))))
|
||||
(set! (.-onerror ws)
|
||||
(fn [event]
|
||||
(js/console.log event))))
|
||||
|
|
Loading…
Reference in a new issue