Skip to content

Commit

Permalink
Rename edn.cljc to edn.cljs and reorganize accordingly to fix lint er…
Browse files Browse the repository at this point in the history
…rors

Basically fipp.ednize is cljs only but were using it like it was a cljc file so
this addresses that so edn.cljs is properly infected as cljs.
  • Loading branch information
dgtized committed Nov 5, 2024
1 parent f153df1 commit 59e5d60
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 87 deletions.
89 changes: 59 additions & 30 deletions src/shimmers/common/edn.cljc → src/shimmers/common/edn.cljs
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
(ns shimmers.common.edn
"Adjusted formatting from `fipp.edn` for Clojure/EDN forms"
(:require [fipp.ednize :refer [edn record->tagged]]
[fipp.edn :refer [pretty-coll]]
[fipp.visit :refer [visit visit*]]
[fipp.engine :refer (pprint-document)]))

;; See https://github.com/brandonbloom/fipp/issues/83 for symbol hack, basically
;; it's forcing EdnPrinter to believe it's a symbol and not a float so it
;; doesn't wrap it in a string.
(defn fixed-width
"Format float `v` to `width` decimal places as long as it's not infinite."
[v width]
#?(:cljs
(if (or (integer? v) (infinite? v))
v
(symbol (.toFixed v width)))
:clj
(if (or (integer? v)
(= v Double/POSITIVE_INFINITY)
(= v Double/NEGATIVE_INFINITY))
v
(symbol (format (str "%." width "f") v)))))
(:require
[fipp.edn :refer [pretty-coll]]
[fipp.ednize :refer [edn IEdn record->tagged]]
[fipp.engine :refer [pprint-document]]
[fipp.visit :refer [visit visit*]]
[shimmers.common.format :refer [fixed-width]]
[thi.ng.geom.types
:refer [Circle2 Line2 LineStrip2 Polygon2 Rect2 Triangle2]]
[thi.ng.geom.vector :refer [Vec2 Vec3]]))

;; identity operation for a map record that removes type info so tagged-literal
;; will not be called recursively with the original type. However this should
;; pickup extra keys if something else has been assoced in beyond the record keys.
;; TODO: worth including meta info?
(defn untyped [s]
(zipmap (keys s) (vals s)))

;; Simplify IEdn output for pretty printing
(extend-protocol IEdn
Circle2
(-edn [s]
(tagged-literal 'Circle2 (untyped s)))

Line2
(-edn [s]
(tagged-literal 'Line2 (untyped s)))

LineStrip2
(-edn [s]
(tagged-literal 'LineStrip2 (untyped s)))

Polygon2
(-edn [s]
(tagged-literal 'Polygon2 (untyped s)))

Rect2
(-edn [s]
(tagged-literal 'Rect2 (untyped s)))

Triangle2
(-edn [s]
(tagged-literal 'Triangle2 (untyped s)))

Vec2
(-edn [s]
(tagged-literal 'v2 (mapv (fn [v] (fixed-width v 2)) s)))

Vec3
(-edn [s]
(tagged-literal 'v3 (mapv (fn [v] (fixed-width v 2)) s))))

(defrecord EdnPrinter [symbols print-meta print-length print-level print-fixed-width]

Expand All @@ -31,25 +60,25 @@
(visit this (edn x)))


(visit-nil [this]
(visit-nil [_this]
[:text "nil"])

(visit-boolean [this x]
(visit-boolean [_this x]
[:text (str x)])

(visit-string [this x]
(visit-string [_this x]
[:text (pr-str x)])

(visit-character [this x]
(visit-character [_this x]
[:text (pr-str x)])

(visit-symbol [this x]
(visit-symbol [_this x]
[:text (str x)])

(visit-keyword [this x]
(visit-keyword [_this x]
[:text (str x)])

(visit-number [this x]
(visit-number [_this x]
[:text (pr-str (fixed-width x print-fixed-width))])

(visit-seq [this x]
Expand Down Expand Up @@ -81,10 +110,10 @@
[:align [:span "^" (visit this m)] :line (visit* this x)]
(visit* this x)))

(visit-var [this x]
(visit-var [_this x]
[:text (str x)])

(visit-pattern [this x]
(visit-pattern [_this x]
[:text (pr-str x)])

(visit-record [this x]
Expand Down
18 changes: 18 additions & 0 deletions src/shimmers/common/format.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns shimmers.common.format)

;; See https://github.com/brandonbloom/fipp/issues/83 for symbol hack, basically
;; it's forcing EdnPrinter to believe it's a symbol and not a float so it
;; doesn't wrap it in a string.
(defn fixed-width
"Format float `v` to `width` decimal places as long as it's not infinite."
[v width]
#?(:cljs
(if (or (integer? v) (infinite? v))
v
(symbol (.toFixed v width)))
:clj
(if (or (integer? v)
(= v Double/POSITIVE_INFINITY)
(= v Double/NEGATIVE_INFINITY))
v
(symbol (format (str "%." width "f") v)))))
73 changes: 16 additions & 57 deletions src/shimmers/common/ui/debug.cljc
Original file line number Diff line number Diff line change
@@ -1,64 +1,20 @@
(ns shimmers.common.ui.debug
(:require
[shimmers.common.edn :as sc-edn]
[fipp.ednize :refer [IEdn]]
[shimmers.common.format :as sc-format]
#?@(:cljs
[[shimmers.common.ui.controls :as ctrl]
[thi.ng.geom.types :refer [Circle2 Line2 LineStrip2 Polygon2 Rect2 Triangle2]]
[thi.ng.geom.vector :refer [Vec2 Vec3]]]
:clj [[thi.ng.geom.types]
[thi.ng.geom.vector]]))
#?(:clj (:import [thi.ng.geom.types Circle2 Line2 LineStrip2 Polygon2 Rect2 Triangle2]
[thi.ng.geom.vector Vec2 Vec3])))
[[shimmers.common.edn :as sc-edn]
[shimmers.common.ui.controls :as ctrl]])))

;; identity operation for a map record that removes type info so tagged-literal
;; will not be called recursively with the original type. However this should
;; pickup extra keys if something else has been assoced in beyond the record keys.
;; TODO: worth including meta info?
(defn untyped [s]
(zipmap (keys s) (vals s)))
#?(:cljs (def untyped sc-edn/untyped))

(defn fixed-width
"Format float `v` to 2 decimal places as long as it's not infinite."
([v]
(sc-edn/fixed-width v 2))
(sc-format/fixed-width v 2))
([v width]
(sc-edn/fixed-width v width)))

;; Simplify IEdn output for pretty printing
(extend-protocol IEdn
Circle2
(-edn [s]
(tagged-literal 'Circle2 (untyped s)))

Line2
(-edn [s]
(tagged-literal 'Line2 (untyped s)))

LineStrip2
(-edn [s]
(tagged-literal 'LineStrip2 (untyped s)))

Polygon2
(-edn [s]
(tagged-literal 'Polygon2 (untyped s)))

Rect2
(-edn [s]
(tagged-literal 'Rect2 (untyped s)))

Triangle2
(-edn [s]
(tagged-literal 'Triangle2 (untyped s)))

Vec2
(-edn [s]
(tagged-literal 'v2 (mapv fixed-width s)))

Vec3
(-edn [s]
(tagged-literal 'v3 (mapv fixed-width s))))
(sc-format/fixed-width v width)))

;; cljs only
(defmacro time-it
"Evaluates expr and stores it in debug `atom` at `key`. Returns the value of expr."
[atom key expr]
Expand All @@ -69,6 +25,7 @@
" msecs"))
ret#))

;; cljs only
(defmacro span-prof
[desc expr]
`(let [start# (cljs.core/system-time)
Expand All @@ -82,13 +39,15 @@
(when (= :profile (first tap-value))
(swap! sink conj (second tap-value)))))

(defn pre-edn
([edn] (pre-edn edn {}))
([edn options]
[:pre.debug [:code (with-out-str (sc-edn/pprint edn options))]]))
#?(:cljs
(defn pre-edn
([edn] (pre-edn edn {}))
([edn options]
[:pre.debug [:code (with-out-str (sc-edn/pprint edn options))]])))

(defn display [atom]
(pre-edn (deref atom)))
#?(:cljs
(defn display [atom]
(pre-edn (deref atom))))

#?(:cljs
(do
Expand Down

0 comments on commit 59e5d60

Please sign in to comment.