Skip to content

Commit 1e88a01

Browse files
committed
extract current namespace to load code into
1 parent 2f8affe commit 1e88a01

File tree

4 files changed

+381
-230
lines changed

4 files changed

+381
-230
lines changed

deps.edn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{:deps {thheller/shadow-cljs {:mvn/version "2.23.8"}
22
org.babashka/sci {:mvn/version "0.8.40"}
3-
applied-science/js-interop {:mvn/version "0.4.2"}}
3+
applied-science/js-interop {:mvn/version "0.4.2"}
4+
rewrite-clj/rewrite-clj {:mvn/version "1.1.47"}}
45
:paths ["src/"]}

src/eval-region.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@ export function tryEval(s) {
7272
}
7373
}
7474

75+
export const clearEval = (view) => {
76+
if (evalResult.length != 0) {
77+
evalResult = ""
78+
updateEditor(view, codeBeforeEval, posBeforeEval)
79+
}
80+
}
81+
7582
export const evalAtCursor = (view) => {
83+
clearEval(view)
7684
const doc = view.state.doc.toString()
7785
codeBeforeEval = doc
7886
posBeforeEval = view.state.selection.main.head
@@ -85,14 +93,8 @@ export const evalAtCursor = (view) => {
8593
return true
8694
}
8795

88-
export const clearEval = (view) => {
89-
if (evalResult.length != 0) {
90-
evalResult = ""
91-
updateEditor(view, codeBeforeEval, posBeforeEval)
92-
}
93-
}
94-
9596
export const evalTopLevel = (view) => {
97+
clearEval(view)
9698
posAtFormEnd = topLevelNode(view.state).to
9799
const doc = view.state.doc.toString()
98100
posBeforeEval = view.state.selection.main.head
@@ -106,6 +108,7 @@ export const evalTopLevel = (view) => {
106108
}
107109

108110
export const evalCell = (view) => {
111+
clearEval(view)
109112
const doc = view.state.doc.toString()
110113
posBeforeEval = view.state.selection.main.head
111114
evalResult = tryEval(view.state.doc.text.join(" "))

src/lang_clojure_eval/main.cljs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[goog.string]
77
[goog.string.format]
88
[clojure.pprint :as pprint]
9+
[rewrite-clj.zip :as z]
910
[sci.impl.evaluator]))
1011

1112
(defonce context
@@ -25,14 +26,19 @@
2526
'lang-clojure-eval.integer
2627
{'parseInt int/parse-int}}}))
2728

29+
(defn current-ns [source]
30+
(z/sexpr (z/next (z/find-next-value (z/of-string source) z/next 'ns))))
31+
2832
(defn eval-string [source]
29-
(let [reqs "(require '[lang-clojure-eval.character :as Character]
30-
'[lang-clojure-eval.integer :as Integer])
33+
(let [reqs (str "(ns " (or (current-ns source) "lang-clojure-eval")
34+
"(:require [lang-clojure-eval.character :as Character]
35+
[lang-clojure-eval.integer :as Integer]))
3136
(defn int [x]
3237
(if (.isInteger js/Number (js/parseInt x))
3338
(js/parseInt x)
34-
(.charCodeAt x 0)))"]
39+
(.charCodeAt x 0)))")]
3540
(try (binding [*print-length* 100]
3641
(with-out-str (pprint/pprint (sci/eval-string* context (str reqs source)))))
3742
(catch :default e
3843
(with-out-str (error-handler source e))))))
44+

0 commit comments

Comments
 (0)