From 0914090ed4a0b6b2e0192c5dd2593bec211389e9 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Sun, 10 Apr 2022 00:20:18 -0400 Subject: [PATCH] Refactor REPL - eval hotkey now pipes to active REPL session --- editor/init.fnl | 26 +++++++++++++++----------- editor/repl.fnl | 28 ++++++++++------------------ editor/replsession.fnl | 35 +++++++++++++++++++++++++++++++++++ inspector/debug.fnl | 15 ++++++++++----- wrap.fnl | 2 -- 5 files changed, 70 insertions(+), 36 deletions(-) create mode 100644 editor/replsession.fnl diff --git a/editor/init.fnl b/editor/init.fnl index 6ffa7e7..baaaf16 100644 --- a/editor/init.fnl +++ b/editor/init.fnl @@ -4,13 +4,17 @@ (local keymap (require :core.keymap)) (local common (require :core.common)) +(fn selected-form [] + (let [ldoc core.active_view.doc + (aline acol bline bcol) (ldoc:get_selection)] + (if (and (= aline bline) (= acol bcol)) + (ldoc:get_text aline 1 aline 10000000) + (ldoc:get_text aline acol bline bcol)))) + (fn inline-eval [eval] (let [ldoc core.active_view.doc - (aline acol bline bcol) (ldoc:get_selection) - inject #(ldoc:insert bline bcol (eval $1))] - (if (and (= aline bline) (= acol bcol)) - (inject (ldoc:get_text aline 1 aline 10000000)) - (inject (ldoc:get_text aline acol bline bcol))))) + (aline acol bline bcol) (ldoc:get_selection)] + (ldoc:insert bline bcol (eval (selected-form))))) (require :editor.editmode) @@ -18,16 +22,16 @@ "repl:submit" #(core.active_view:submit) }) -(local ReplView (require :editor.replview)) -(local repl (require :editor.repl)) +(local {: show : submit} (util.require :inspector.debug)) (command.add nil { - "repl:create" (fn [] - (local node (core.root_view:get_active_node)) - (node:add_view (ReplView (repl.new))) - ) + "repl:create" #(show) +}) +(command.add :core.docview { + "repl:eval" #(submit nil (selected-form)) }) (keymap.add { :return "repl:submit" + "alt+e" "repl:eval" }) {: inline-eval} diff --git a/editor/repl.fnl b/editor/repl.fnl index b84bca6..b895fc5 100644 --- a/editor/repl.fnl +++ b/editor/repl.fnl @@ -4,6 +4,8 @@ (local lume (require :lib.lume)) (local {: textbutton : under : group-wrapper} (util.require :editor.imgui)) (local {: inspect} (util.require :inspector)) +(local replsession (require :editor.replsession)) + (local repl (util.hot-table ...)) (fn repl.inspector [{: w &as form} {: vals : states}] @@ -19,30 +21,20 @@ (fn repl.mk-result [vals] {:draw repl.inspector : vals :states (icollect [_ (ipairs vals)] {})}) -(fn repl.run [{: listeners}] - (fennel.repl {:readChunk coroutine.yield - :onValues #(repl.notify listeners (repl.mk-result $1)) - :onError (fn [errType err luaSource] (repl.notify listeners (repl.mk-result [err]))) - :pp #$1 - :env (lume.clone _G)})) - (fn repl.listen [{: listeners} listener] (table.insert listeners listener)) (fn repl.unlisten [{: listeners} listener] (lume.remove listeners listener)) -(fn repl.submit [{: coro} chunk] - (coroutine.resume coro (.. chunk "\n"))) +(fn repl.submit [{: session : listeners} chunk] + (session:submit chunk (fn [{: vals : err : traceback}] (repl.notify listeners (repl.mk-result (or vals [err traceback])))))) -(fn repl.new [] - (local result - {:listeners [] - :listen #(repl.listen $...) - :unlisten #(repl.unlisten $...) - :submit #(repl.submit $...) - :coro (coroutine.create repl.run)}) - (coroutine.resume result.coro result) - result) +(fn repl.new [?id] + {:listeners [] + :listen #(repl.listen $...) + :unlisten #(repl.unlisten $...) + :submit #(repl.submit $...) + :session (replsession.session ?id)}) repl.hot diff --git a/editor/replsession.fnl b/editor/replsession.fnl new file mode 100644 index 0000000..36784bc --- /dev/null +++ b/editor/replsession.fnl @@ -0,0 +1,35 @@ +(local util (require :lib.util)) +(local fennel (require :lib.fennel)) + +(local replsession (util.hot-table ...)) + +(set replsession.sessions {}) + +(fn replsession.new-session [] + (let [run (fn [session] + (fennel.repl {:readChunk coroutine.yield + :onValues #(session.callback {:vals $1}) + :onError #(session.callback {:errType $1 :err $2 :luaSource $3 :traceback (fennel.traceback)}) + :pp #$1 + :env (lume.clone _G)})) + session {:coro (coroutine.create run) :submit replsession.submit}] + (coroutine.resume session.coro session) + session)) + +(fn replsession.submit [session chunk callback] + (assert (= session.callback nil)) + (set session.callback callback) + (coroutine.resume session.coro (.. chunk "\n")) + (assert (= session.callback callback)) + (set session.callback nil)) + +(fn replsession.activate [id] (set replsession.active-session-id id)) +(fn replsession.session-id [?id] (or ?id replsession.active-session-id :REPL)) +(fn replsession.session [?id] + (let [id (replsession.session-id ?id) + session (or (. replsession.sessions id) (replsession.new-session))] + (tset replsession.sessions id session) + session)) + +replsession.hot + diff --git a/inspector/debug.fnl b/inspector/debug.fnl index 1ad75a2..a8a0a66 100644 --- a/inspector/debug.fnl +++ b/inspector/debug.fnl @@ -3,6 +3,7 @@ (local util (require :lib.util)) (local repl (require :editor.repl)) (local ReplView (require :editor.replview)) +(local replsession (require :editor.replsession)) (local module (util.hot-table ...)) @@ -15,16 +16,20 @@ (fn create-inspector-window [name ?value] (let [node (core.root_view:get_active_node) - conn (repl.new) + conn (repl.new name) view (ReplView conn)] (set view.inspector-name name) (set view.title name) (view:append {:draw (fn [_ _ x y] (renderer.draw_text style.font name x y style.text) (+ (style.font:get_height) style.padding.y))}) (view:append (repl.mk-result [?value])) - (node:add_view view))) + (node:add_view view) + view)) -(lambda module.show [name ?value] - (when (= (find-existing-inspector-window name) nil) - (create-inspector-window name ?value))) +(lambda module.show [?name ?value] + (let [name (replsession.session-id ?name)] + (or (find-existing-inspector-window name) (create-inspector-window name ?value)))) + +(lambda module.submit [?name chunk] + (: (module.show ?name) :submit chunk)) module.hot diff --git a/wrap.fnl b/wrap.fnl index 3fcbe07..51ffbe4 100644 --- a/wrap.fnl +++ b/wrap.fnl @@ -15,7 +15,6 @@ (: :gsub "%.init$" ""))) (command.add "core.docview" { - "fennel:eval" #(editor.inline-eval #(fv (fennel.eval $1 {:env _G :compiler-env _G}) {})) "lume:hotswap" (fn [] (local modname (get-modname)) (core.log (.. "Hotswapping " modname)) @@ -29,7 +28,6 @@ (core.log (.. modname " was not a loaded macro module"))))) }) (keymap.add { - "alt+e" "fennel:eval" "alt+r" "lume:hotswap" "alt+u" "fennel:unload-macro" })