Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
read-ns-decl + read-ns-sym
Browse files Browse the repository at this point in the history
  • Loading branch information
rksm committed Apr 13, 2015
1 parent 8fa50d7 commit 370e091
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:url "http://github.com/cloxp/cloxp-source-reader"
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/tools.reader "0.8.16"]
[org.clojure/tools.namespace "0.2.8"]
[org.clojure/tools.analyzer.jvm "0.6.5"]
[org.rksm/system-files "0.1.4-SNAPSHOT"]
[com.keminglabs/cljx "0.6.0"]])
30 changes: 18 additions & 12 deletions src/rksm/cloxp_source_reader/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [clojure.tools.reader.reader-types :as trt]
[clojure.tools.reader.impl.utils]
[clojure.tools.reader :as tr]
[clojure.tools.namespace.parse :as tnp]
[clojure.string :as s]
[rksm.system-files :refer (source-reader-for-ns)]
(cljx core rules))
Expand Down Expand Up @@ -173,18 +174,6 @@

; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

; (defn add-source-to-interns-from-repl
; "This method uses the RT/baseloader to lookup the files belonging to symbols.
; When files get reloaded / defs redefined this can mean that the code being
; retrieved is outdated"
; [ns intern-meta-data]
; (let [ns-string (str (ns-name ns))
; sym-fn (partial symbol ns-string)
; source-fn #(or (source-for-symbol (sym-fn (-> % :name str))) "")]
; (map #(assoc % :source (source-fn %)) intern-meta-data)))

; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

(defn updated-source
"Takes the new source for a def and produces a new version of the ns source,
with the new def code embedded. meta-info is a meta-data like structure."
Expand All @@ -197,3 +186,20 @@
s/split-lines count
(drop (drop line lines)))]
(str (s/join "\n" (concat before-lines [(s/trim-newline new-src-for-def)] after-lines)))))

(defn read-ns-decl
[source-or-rdr]
(let [rdr (if (instance? java.io.PushbackReader source-or-rdr)
source-or-rdr
(-> source-or-rdr
java.io.StringReader.
java.io.PushbackReader.))]
(tnp/read-ns-decl rdr)))

(defn read-ns-sym
[source-or-rdr]
(some-> source-or-rdr read-ns-decl second))

(comment
(read-ns-sym "foo\n(ns ^{:doc \"baz\"} bar)")
)
5 changes: 4 additions & 1 deletion test/rksm/cloxp_source_reader/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@
source (java.io.StringReader. "(def x 23)\n\n(def y 24)")]
(src-rdr/add-source-to-interns-with-reader source entities)))))))

(deftest ns-decl-read-test
(is (= 'bar (src-rdr/read-ns-sym "foo\n(ns ^{:doc \"baz\"} bar)"))))

; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

(comment
(test-ns 'rksm.cloxp-source-reader.test)
)
)

0 comments on commit 370e091

Please sign in to comment.