diff --git a/src/cljs/snapshot/lumo/repl.clj b/src/cljs/snapshot/lumo/repl.clj index a9065d3f..761ae9e7 100644 --- a/src/cljs/snapshot/lumo/repl.clj +++ b/src/cljs/snapshot/lumo/repl.clj @@ -19,6 +19,15 @@ [n] `(lumo.repl/source* '~n)) +(defmacro var + "Prints the source code for the given symbol, if it can find it. + This requires that the symbol resolve to a Var defined in a + namespace for which the source is available. + + Example: (source filter)" + [n] + `(lumo.repl/eval '~n)) + (defmacro ^:private with-err-str "Evaluates exprs in a context in which *print-err-fn* is bound to .append on a fresh StringBuffer. Returns the string created by any nested diff --git a/src/cljs/snapshot/lumo/repl.cljs b/src/cljs/snapshot/lumo/repl.cljs index 5ba98ced..88eb584f 100644 --- a/src/cljs/snapshot/lumo/repl.cljs +++ b/src/cljs/snapshot/lumo/repl.cljs @@ -1,5 +1,5 @@ (ns lumo.repl - (:refer-clojure :exclude [load-file* eval]) + (:refer-clojure :exclude [load-file* eval var]) (:require-macros [cljs.env.macros :as env] [cljs.analyzer.macros :refer [no-warn]] [lumo.repl :refer [with-err-str]]) @@ -858,6 +858,12 @@ (vreset! result value)))) @result))) +(defn find-var + "Returns the global var named by the namespace-qualified symbol, or + nil if no var with that name." + [sym] + (eval `(~'var ~sym))) + (defn- intern ([ns name] (intern ns name nil)) diff --git a/src/js/cljs.js b/src/js/cljs.js index 40aa6c4a..5cd68638 100644 --- a/src/js/cljs.js +++ b/src/js/cljs.js @@ -544,7 +544,7 @@ async function startClojureScriptEngine(opts: CLIOptsType): Promise { } execute( - "(require '[lumo.repl :refer [apropos find-doc] :refer-macros [dir doc source]])", + "(require '[lumo.repl :refer [apropos find-doc find-var] :refer-macros [dir doc source var]])", 'text', true, false, diff --git a/src/test/lumo/lumo/repl_tests.cljs b/src/test/lumo/lumo/repl_tests.cljs index 3006744a..ffdb546e 100644 --- a/src/test/lumo/lumo/repl_tests.cljs +++ b/src/test/lumo/lumo/repl_tests.cljs @@ -208,3 +208,8 @@ Special Form (is (= 3 (core/eval (list + 1 2)))) (is (= 17 (core/eval '(let [a 10] (+ 3 4 a))))) (is (= 5 ((eval (eval '+)) 2 3))))) + +(when test-util/lumo-env? + (deftest test-find-var + (is (= #'cljs.core/map (lumo/find-var 'map)) "it should find function vars") + (is (= #'lumo.repl/*pprint-results* (lumo/find-var 'lumo.repl/*pprint-results*)) "it should find a var in another namespace")))