[#19] make ns key optional in nREPL messages
This commit is contained in:
parent
847f872df8
commit
249fc0a5a5
3 changed files with 31 additions and 19 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
;; Modified / stripped version of clojure.core.server for use with babashka on
|
;; Modified / stripped version of clojure.core.server for use with babashka on
|
||||||
;; GraalVM.
|
;; GraalVM.
|
||||||
;;
|
;;
|
||||||
;; Copyright (c) Rich Hickey. All rights reserved.
|
;; Copyright (c) Rich Hickey. All rights reserved.
|
||||||
;; The use and distribution terms for this software are covered by the
|
;; The use and distribution terms for this software are covered by the
|
||||||
|
|
|
||||||
|
|
@ -41,21 +41,20 @@
|
||||||
(defn eval-msg [ctx o msg #_threads]
|
(defn eval-msg [ctx o msg #_threads]
|
||||||
(try
|
(try
|
||||||
(let [ns-str (get msg :ns)
|
(let [ns-str (get msg :ns)
|
||||||
sci-ns (if ns-str
|
sw (StringWriter.)]
|
||||||
(sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false)
|
(sci/with-bindings (cond-> {sci/out sw}
|
||||||
(sci-utils/namespace-object (:env ctx) 'user nil false))]
|
ns-str
|
||||||
(sci/binding [vars/current-ns sci-ns
|
(assoc vars/current-ns
|
||||||
sci/print-length @sci/print-length]
|
(sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false)))
|
||||||
|
(when @dev? (println "current ns" (vars/current-ns-name)))
|
||||||
(let [session (get msg :session "none")
|
(let [session (get msg :session "none")
|
||||||
id (get msg :id "unknown")]
|
id (get msg :id "unknown")]
|
||||||
(when @dev? (println "Registering thread for" (str session "-" id)))
|
(when @dev? (println "Registering thread for" (str session "-" id)))
|
||||||
;; (swap! threads assoc [session id] (Thread/currentThread))
|
;; (swap! threads assoc [session id] (Thread/currentThread))
|
||||||
(let [code-str (get msg :code)
|
(let [code-str (get msg :code)
|
||||||
sw (StringWriter.)
|
|
||||||
value (if (str/blank? code-str)
|
value (if (str/blank? code-str)
|
||||||
::nil
|
::nil
|
||||||
(sci/binding [sci/out sw
|
(eval-string* ctx code-str))
|
||||||
vars/current-ns @vars/current-ns] (eval-string* ctx code-str)))
|
|
||||||
out-str (not-empty (str sw))
|
out-str (not-empty (str sw))
|
||||||
env (:env ctx)]
|
env (:env ctx)]
|
||||||
(swap! env update-in [:namespaces 'clojure.core]
|
(swap! env update-in [:namespaces 'clojure.core]
|
||||||
|
|
@ -96,13 +95,10 @@
|
||||||
(defn complete [ctx o msg]
|
(defn complete [ctx o msg]
|
||||||
(try
|
(try
|
||||||
(let [ns-str (get msg :ns)
|
(let [ns-str (get msg :ns)
|
||||||
sci-ns (if ns-str
|
sci-ns (when ns-str
|
||||||
(sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false)
|
(sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false))]
|
||||||
(sci-utils/namespace-object (:env ctx) 'user nil false))]
|
(sci/binding [vars/current-ns (or sci-ns @vars/current-ns)]
|
||||||
(sci/binding [vars/current-ns sci-ns]
|
(let [query (:symbol msg)
|
||||||
(let [
|
|
||||||
;;ns-sym (symbol ns)
|
|
||||||
query (:symbol msg)
|
|
||||||
from-current-ns (fully-qualified-syms ctx (eval-string* ctx "(ns-name *ns*)"))
|
from-current-ns (fully-qualified-syms ctx (eval-string* ctx "(ns-name *ns*)"))
|
||||||
from-current-ns (map (fn [sym]
|
from-current-ns (map (fn [sym]
|
||||||
[(namespace sym) (name sym) :unqualified])
|
[(namespace sym) (name sym) :unqualified])
|
||||||
|
|
@ -179,7 +175,7 @@
|
||||||
:describe
|
:describe
|
||||||
(do (send os (response-for msg {"status" #{"done"}
|
(do (send os (response-for msg {"status" #{"done"}
|
||||||
"aux" {}
|
"aux" {}
|
||||||
"ops" (zipmap #{"clone", "describe", "eval"}
|
"ops" (zipmap #{"clone" "eval" "load-file" "complete" "describe"}
|
||||||
(repeat {}))
|
(repeat {}))
|
||||||
"versions" {} #_{"nrepl" {"major" "0"
|
"versions" {} #_{"nrepl" {"major" "0"
|
||||||
"minor" "4"
|
"minor" "4"
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,23 @@
|
||||||
id (:id msg)
|
id (:id msg)
|
||||||
value (:value msg)]
|
value (:value msg)]
|
||||||
(is (= 1 id))
|
(is (= 1 id))
|
||||||
(is (= value "6"))))
|
(is (= value "6")))
|
||||||
|
(testing "creating a namespace and evaluating something in it"
|
||||||
|
(bencode/write-bencode os {"op" "eval"
|
||||||
|
"code" "(ns ns0) (defn foo [] :foo0) (ns ns1) (defn foo [] :foo1)"
|
||||||
|
"session" session
|
||||||
|
"id" (new-id!)})
|
||||||
|
(read-reply in session @id)
|
||||||
|
(testing "not providing the ns key evaluates in the last defined namespace"
|
||||||
|
(bencode/write-bencode os {"op" "eval" "code" "(foo)" "session" session "id" (new-id!)})
|
||||||
|
(is (= ":foo1" (:value (read-reply in session @id)))))
|
||||||
|
(testing "explicitly providing the ns key evaluates in that namespace"
|
||||||
|
(bencode/write-bencode os {"op" "eval"
|
||||||
|
"code" "(foo)"
|
||||||
|
"session" session
|
||||||
|
"id" (new-id!)
|
||||||
|
"ns" "ns0"})
|
||||||
|
(is (= ":foo0" (:value (read-reply in session @id)))))))
|
||||||
(testing "load-file"
|
(testing "load-file"
|
||||||
(bencode/write-bencode os {"op" "load-file" "file" "(ns foo) (defn foo [] :foo)" "session" session "id" (new-id!)})
|
(bencode/write-bencode os {"op" "load-file" "file" "(ns foo) (defn foo [] :foo)" "session" session "id" (new-id!)})
|
||||||
(read-reply in session @id)
|
(read-reply in session @id)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue