diff --git a/src/clj/clojure/repl.clj b/src/clj/clojure/repl.clj index 70ea94f5..36a26e2b 100644 --- a/src/clj/clojure/repl.clj +++ b/src/clj/clojure/repl.clj @@ -142,19 +142,23 @@ itself (not its value) is returned. The reader macro #'x expands to (var x)."}}) [x] (when-let [v (resolve x)] (when-let [filepath (:file (meta v))] - (when-let [strm (.getResourceAsStream (RT/baseLoader) filepath)] - (with-open [rdr (LineNumberReader. (InputStreamReader. strm))] - (dotimes [_ (dec (:line (meta v)))] (.readLine rdr)) - (let [text (StringBuilder.) - pbr (proxy [PushbackReader] [rdr] - (read [] (let [i (proxy-super read)] - (.append text (char i)) - i))) - read-opts (if (.endsWith ^String filepath "cljc") {:read-cond :allow} {})] - (if (= :unknown *read-eval*) - (throw (IllegalStateException. "Unable to read source while *read-eval* is :unknown.")) - (read read-opts (PushbackReader. pbr))) - (str text))))))) + (binding [*ns* (. v ns)] + (when-let [strm (.getResourceAsStream (RT/baseLoader) filepath)] + (with-open [rdr (LineNumberReader. (InputStreamReader. strm))] + (dotimes [_ (dec (:line (meta v)))] + (.readLine rdr)) + (let [text (StringBuilder.) + pbr (proxy [PushbackReader] [rdr] + (read [] (let [i (proxy-super read)] + (.append text (char i)) + i))) + read-opts (if (.endsWith ^String filepath "cljc") + {:read-cond :allow} {})] + (if (= :unknown *read-eval*) + (throw (IllegalStateException. + "Unable to read source while *read-eval* is :unknown.")) + (read read-opts (PushbackReader. pbr))) + (str text)))))))) (defmacro source "Prints the source code for the given symbol, if it can find it. diff --git a/test/clojure/test_clojure/repl.clj b/test/clojure/test_clojure/repl.clj index 609056b5..e282555f 100644 --- a/test/clojure/test_clojure/repl.clj +++ b/test/clojure/test_clojure/repl.clj @@ -12,6 +12,7 @@ (deftest test-source (is (= "(defn foo [])" (source-fn 'clojure.test-clojure.repl.example/foo))) + (is (= "(defn qux [] ::s/foo)" (source-fn 'clojure.test-clojure.repl.example/qux))) (is (= (platform-newlines "(defn foo [])\n") (with-out-str (source clojure.test-clojure.repl.example/foo)))) (is (nil? (source-fn 'non-existent-fn)))) @@ -23,8 +24,8 @@ (deftest test-dir (is (thrown? Exception (dir-fn 'non-existent-ns))) - (is (= '[bar foo] (dir-fn 'clojure.test-clojure.repl.example))) - (is (= (platform-newlines "bar\nfoo\n") (with-out-str (dir clojure.test-clojure.repl.example))))) + (is (= '[bar foo qux] (dir-fn 'clojure.test-clojure.repl.example))) + (is (= (platform-newlines "bar\nfoo\nqux\n") (with-out-str (dir clojure.test-clojure.repl.example))))) (deftest test-apropos (testing "with a regular expression" diff --git a/test/clojure/test_clojure/repl/example.clj b/test/clojure/test_clojure/repl/example.clj index 3a5238e7..30624287 100644 --- a/test/clojure/test_clojure/repl/example.clj +++ b/test/clojure/test_clojure/repl/example.clj @@ -1,5 +1,7 @@ -(ns clojure.test-clojure.repl.example) +(ns clojure.test-clojure.repl.example + (:require [clojure.string :as s])) ;; sample namespace for repl tests, don't add anything here (defn foo []) (defn bar []) +(defn qux [] ::s/foo)