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

Commit

Permalink
Add var and find-var to lumo
Browse files Browse the repository at this point in the history
Now that we have eval, we can easily provide var and find-var at the REPL.
  • Loading branch information
arichiardi committed May 18, 2018
1 parent a475bd6 commit 6ead0f0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/cljs/snapshot/lumo/repl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/cljs/snapshot/lumo/repl.cljs
Original file line number Diff line number Diff line change
@@ -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]])
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/js/cljs.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ async function startClojureScriptEngine(opts: CLIOptsType): Promise<mixed> {
}

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,
Expand Down
5 changes: 5 additions & 0 deletions src/test/lumo/lumo/repl_tests.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))

0 comments on commit 6ead0f0

Please sign in to comment.