repl: table inspector

This commit is contained in:
Jeremy Penner 2021-04-03 22:35:50 -04:00
parent fec30be7dd
commit d84a515a55
4 changed files with 43 additions and 18 deletions

View file

@ -2,41 +2,65 @@
(local fennel (require :lib.fennel)) (local fennel (require :lib.fennel))
(local style (require :core.style)) (local style (require :core.style))
(local lume (require :lib.lume)) (local lume (require :lib.lume))
(local {: textbutton} (util.require :editor.imstate))
(local repl (util.hot-table ...)) (local repl (util.hot-table ...))
(fn repl.text-height [text ?font] (fn repl.text-height [text ?font]
(let [font (or ?font style.font) (let [font (or ?font style.code_font)
(_ newlines) (text:gsub "\n" "\n")] (_ newlines) (text:gsub "\n" "\n")]
(* (font:get_height) (+ newlines 1)))) (* (font:get_height) (+ newlines 1))))
(fn repl.inspect-table [tbl view x y-start w] (fn get-state [tbl k depth state]
(var y y-start) (when (= nil (. state k))
(tset state k {:collapsed (= (type (. tbl k)) :table) :children {}}))
(. state k))
(fn repl.inspect-table [tbl view x y w depth state]
(local font style.code_font)
(var h 0) (var h 0)
(each [k v (pairs tbl)] (each [k v (pairs tbl)]
(let [kstr (fv k) (let [kstate (get-state tbl k depth state)
wk 1] kstr (fv k)
nil)) wk (font:get_width kstr)
) xoffset (+ wk style.padding.x)
(fn repl.inspect [v view x y w] toggle-collapse (textbutton view kstr x (+ y h))
(renderer.draw_text style.font (fv v) x y style.text) hv (if kstate.collapsed
(do (renderer.draw_text font "..." (+ x xoffset) (+ y h) style.syntax.comment) (font:get_height))
(repl.inspect v view (+ x xoffset) (+ y h) (- w xoffset) (+ depth 1) kstate.children))]
(when toggle-collapse (tset (. state k) :collapsed (not kstate.collapsed)))
(set h (+ h hv style.padding.y))))
h)
(fn repl.inspect-default [v view x y w depth state]
(renderer.draw_text style.code_font (fv v) x y style.text)
(repl.text-height (fv v))) (repl.text-height (fv v)))
(fn repl.inspector [{: vals} view x y] (fn repl.inspect [v view x y w depth state]
(if (> w 0)
(match (type v)
:table (repl.inspect-table v view x y w depth state)
_ (repl.inspect-default v view x y w depth state))
0))
(fn repl.inspector [{: vals : state} view x y]
(var h 0) (var h 0)
(each [i v (ipairs vals)] (each [i v (ipairs vals)]
(set h (+ h (repl.inspect v view x (+ y h) view.size.x)))) (set h (+ h (repl.inspect v view x (+ y h) view.size.x 1 state))))
(+ h style.padding.y)) (+ h style.padding.y))
(fn repl.notify [listeners line] (fn repl.notify [listeners line]
(each [_ listener (ipairs listeners)] (each [_ listener (ipairs listeners)]
(listener:append line))) (listener:append line)))
(fn repl.run [{: listeners}] (fn repl.mk-result [vals]
(local inspector #(repl.inspector $...)) (local inspector #(repl.inspector $...))
{:draw inspector : vals :state {}})
(fn repl.run [{: listeners}]
(fennel.repl {:readChunk coroutine.yield (fennel.repl {:readChunk coroutine.yield
:onValues #(repl.notify listeners {:draw inspector :vals $1}) :onValues #(repl.notify listeners (repl.mk-result $1))
:onError (fn [errType err luaSource] (repl.notify listeners {:draw inspector :vals [err]})) :onError (fn [errType err luaSource] (repl.notify listeners (repl.mk-result [err])))
:pp #$1 :pp #$1
:env (lume.clone _G)})) :env (lume.clone _G)}))

View file

@ -13,7 +13,6 @@
(set self.cmd "") (set self.cmd "")
(set self.scrollheight math.huge) (set self.scrollheight math.huge)
(set self.scrollable true) (set self.scrollable true)
(set self.pin-to-bottom-y self.scroll.to.y)
(self.conn:listen self)) (self.conn:listen self))
(fn ReplView.try_close [self do_close] (fn ReplView.try_close [self do_close]

View file

@ -57,7 +57,7 @@
(local tbl {}) (local tbl {})
(fn find-table [] (fn find-table []
(let [loaded-pkg (. package.loaded modname)] (let [loaded-pkg (. package.loaded modname)]
(if (= (type loaded-pkg) :table) loaded-pkg) tbl)) (if (= (type loaded-pkg) :table) loaded-pkg tbl)))
(setmetatable {:hot tbl} { (setmetatable {:hot tbl} {
:__index (fn [_ key] (. (find-table) key)) :__index (fn [_ key] (. (find-table) key))
:__newindex (fn [_ key val] (tset (find-table) key val)) :__newindex (fn [_ key val] (tset (find-table) key val))

View file

@ -1,6 +1,8 @@
TODO: TODO:
* tile editor (geometric shapes for now?) * tile editor (geometric shapes for now?)
Repl-y thing: REIL:
appends objects to a list (inspectable?) * history navigation
* paren matching (minibuffer?)
* edit table values inline