Skip to content
This repository has been archived by the owner on Jan 28, 2019. It is now read-only.

Commit

Permalink
Merge pull request #71 from arrdem/bugfix/clj-1579
Browse files Browse the repository at this point in the history
Allow clojure.repl/source to read ::ns/sym abbrevs
  • Loading branch information
arrdem committed Feb 24, 2016
2 parents 326b16c + cdb86e8 commit cbe2e2c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
30 changes: 17 additions & 13 deletions src/clj/clojure/repl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions test/clojure/test_clojure/repl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))

Expand All @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion test/clojure/test_clojure/repl/example.clj
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit cbe2e2c

Please sign in to comment.