Skip to content

Commit 664dfe2

Browse files
committed
refactor repl
1 parent 85d4e82 commit 664dfe2

11 files changed

+308
-339
lines changed

src/renderer/core.cljs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
[renderer.history.subs]
2828
[renderer.notification.events]
2929
[renderer.notification.subs]
30-
[renderer.reepl.core]
3130
[renderer.reepl.replumb :as replumb]
3231
[renderer.ruler.subs]
3332
[renderer.snap.events]

src/renderer/reepl/codemirror.cljs

+60-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
["codemirror/mode/clojure/clojure.js"]
1010
["codemirror/mode/javascript/javascript.js"]
1111
["react" :as react]
12+
[cljs.tools.reader :as reader]
1213
[clojure.string :as str]
1314
[reagent.core :as r]))
1415

@@ -30,6 +31,50 @@
3031
:end #js {:line lno
3132
:ch (+ cno (count forward))}}))
3233

34+
(defn is-valid-cljs?
35+
[source]
36+
(try
37+
(fn []
38+
(reader/read-string source)
39+
true)
40+
(catch js/Error _
41+
false)))
42+
43+
(def default-opts
44+
{:style {:height "auto"
45+
:flex 1}
46+
47+
:should-go-up
48+
(fn [_source inst]
49+
(let [pos (.getCursor inst)]
50+
(= 0 (.-line pos))))
51+
52+
:should-go-down
53+
(fn [_source inst]
54+
(let [pos (.getCursor inst)
55+
last-line (.lastLine inst)]
56+
(= last-line (.-line pos))))
57+
58+
;; TODO: if the cursor is inside a list, and the function doesn't have enought
59+
;; arguments yet, then return false
60+
;; e.g. (map |) <- map needs at least one argument.
61+
:should-eval
62+
(fn [source inst evt]
63+
(if (.-shiftKey evt)
64+
false
65+
(if (.-metaKey evt)
66+
true
67+
(let [lines (.lineCount inst)
68+
in-place (or (= 1 lines)
69+
(let [pos (.getCursor inst)
70+
last-line (dec lines)]
71+
(and
72+
(= last-line (.-line pos))
73+
(= (.-ch pos)
74+
(count (.getLine inst last-line))))))]
75+
(and in-place
76+
(is-valid-cljs? source))))))})
77+
3378
(defn cm-current-word
3479
"Find the current 'word' according to CodeMirror's `wordChars' list"
3580
[cm]
@@ -148,21 +193,22 @@
148193
149194
:on-cm-init (fn [cm] -> nil)
150195
called with the CodeMirror instance, for whatever extra fiddling you want to do."
151-
[value-atom {:keys [style
152-
on-change
153-
on-eval
154-
on-up
155-
on-down
156-
complete-atom
157-
complete-word
158-
should-go-up
159-
should-go-down
160-
should-eval
161-
js-cm-opts
162-
on-cm-init]}]
163-
196+
[value-atom options]
164197
(let [cm (atom nil)
165-
ref (react/createRef)]
198+
ref (react/createRef)
199+
options (merge default-opts options)
200+
{:keys [style
201+
on-change
202+
on-eval
203+
on-up
204+
on-down
205+
complete-atom
206+
complete-word
207+
should-go-up
208+
should-go-down
209+
should-eval
210+
js-cm-opts
211+
on-cm-init]} options]
166212
(r/create-class
167213
{:component-did-mount
168214
(fn [_this]

src/renderer/reepl/completions.cljs

-40
This file was deleted.

src/renderer/reepl/core.cljs

-203
This file was deleted.

src/renderer/reepl/db.cljs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(ns renderer.reepl.db)
2+
3+
(def initial-state
4+
{:items []
5+
:hist-pos 0
6+
:history [""]})

src/renderer/reepl/handlers.cljs

+11
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,14 @@
6262
0
6363
(dec pos))]
6464
(assoc db :hist-pos new-pos)))
65+
66+
;; TODO: is there a macro or something that could do this cleaner?
67+
(defn make-handlers
68+
[state]
69+
{:add-input (partial swap! state add-input)
70+
:add-result (partial swap! state add-result)
71+
:go-up (partial swap! state go-up)
72+
:go-down (partial swap! state go-down)
73+
:clear-items (partial swap! state clear-items)
74+
:set-text (partial swap! state set-text)
75+
:add-log (partial swap! state add-log)})

0 commit comments

Comments
 (0)