Refactor REPL - eval hotkey now pipes to active REPL session
This commit is contained in:
parent
35d9fc10e7
commit
0914090ed4
|
@ -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}
|
||||
|
|
|
@ -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
|
||||
|
|
35
editor/replsession.fnl
Normal file
35
editor/replsession.fnl
Normal file
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
2
wrap.fnl
2
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"
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue