Skip to content

Commit

Permalink
Alias namespaces
Browse files Browse the repository at this point in the history
Added:
Pods render namespaced keywords with an alias if it is declared in the
settings.

#393
  • Loading branch information
kimo-k committed Jul 26, 2023
1 parent 6a68f6e commit 2eda4a0
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. This change

#### Added

- Pods render namespaced keywords with an alias if it is declared in the settings. See #393
- Added rewrite-clj 1.1.47 dependency

#### Changed
Expand Down
2 changes: 2 additions & 0 deletions src/day8/re_frame_10x/components/cljs_devtools.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@
[data indexed-path {:keys [object update-path-fn sort?] :as opts} & [class]]
(let [render-paths? (rf/subscribe [::app-db.subs/data-path-annotations?])
open-new-inspectors? @(rf/subscribe [::settings.subs/open-new-inspectors?])
ns->alias @(rf/subscribe [::settings.subs/ns->alias])
data (cond->> data sort? tools.datafy/deep-sorted-map)
data (tools.datafy/alias-namespaces data ns->alias)
input-field-path (second indexed-path) ;;path typed in input-box
shadow-root (-> (.getElementById js/document "--re-frame-10x--") ;;main shadow-root html component
.-shadowRoot
Expand Down
9 changes: 7 additions & 2 deletions src/day8/re_frame_10x/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@
:shiftKey true}}})
(rf/inject-cofx ::local-storage/load {:key "log-outputs" :or [:day8.re-frame-10x.fx.log/console]})
(rf/inject-cofx ::local-storage/load {:key "log-pretty?" :or true})
(rf/inject-cofx ::local-storage/load {:key "ns-aliases" :or
(let [id (random-uuid)]
{id {:id id :ns-full "long-namespace" :ns-alias "ln"}})})
rf/unwrap]
(fn [{:keys [panel-width-ratio show-panel selected-tab filter-items app-db-json-ml-expansions
external-window? external-window-dimensions show-epoch-traces? using-trace?
ignored-events low-level-trace filtered-view-trace retained-epochs app-db-paths
app-db-follows-events? ambiance syntax-color-scheme categories data-path-annotations?
show-event-history open-new-inspectors? handle-keys? key-bindings log-outputs log-pretty?]}
show-event-history open-new-inspectors? handle-keys? key-bindings log-outputs log-pretty?
ns-aliases]}
{:keys [debug?]}]
{:fx [(when using-trace?
[:dispatch [::settings.events/enable-tracing]])
Expand Down Expand Up @@ -84,7 +88,8 @@
[:dispatch [::settings.events/handle-keys? handle-keys?]]
[:dispatch [::settings.events/key-bindings key-bindings]]
[:dispatch [::settings.events/log-outputs log-outputs]]
[:dispatch [::settings.events/log-pretty? log-pretty?]]]}))
[:dispatch [::settings.events/log-pretty? log-pretty?]]
[:dispatch [::settings.events/ns-aliases ns-aliases]]]}))

;; Global

Expand Down
34 changes: 34 additions & 0 deletions src/day8/re_frame_10x/panels/settings/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,37 @@
[(rf/path [:settings :log-pretty?]) rf/trim-v (local-storage/save "log-pretty?")]
(fn [_ [pretty?]]
pretty?))

(def ns-aliases-interceptors
[(rf/path [:settings :ns-aliases])
rf/trim-v
(local-storage/save "ns-aliases")])

(rf/reg-event-db
::ns-aliases
ns-aliases-interceptors
(fn [_ [ns-aliases]]
ns-aliases))

(rf/reg-event-db
::add-ns-alias
ns-aliases-interceptors
(fn [ns-aliases [ns-full ns-alias]]
(let [id (random-uuid)]
(assoc ns-aliases id {:id id
:ns-full ns-full
:ns-alias ns-alias
:sort (js/Date.now)}))))

(rf/reg-event-db
::remove-ns-alias
ns-aliases-interceptors
(fn [ns-aliases [id]]
(dissoc ns-aliases id)))

(rf/reg-event-db
::update-ns-alias
ns-aliases-interceptors
(fn [ns-aliases [id ns-full ns-alias]]
(update ns-aliases id merge {:ns-full (str ns-full)
:ns-alias (str ns-alias)})))
13 changes: 13 additions & 0 deletions src/day8/re_frame_10x/panels/settings/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,16 @@
(fn [{:keys [log-pretty?]} _]
log-pretty?))

(rf/reg-sub
::ns-aliases
:<- [::root]
(fn [{:keys [ns-aliases]} _]
ns-aliases))

(rf/reg-sub
::ns->alias
:<- [::ns-aliases]
(fn [ns-aliases]
(into {}
(map (comp (juxt :ns-full :ns-alias) val)
ns-aliases))))
66 changes: 57 additions & 9 deletions src/day8/re_frame_10x/panels/settings/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,28 @@
:label "Done"
:on-click #(rf/dispatch [::settings.events/toggle])}])

(defn closeable-text-box
[& {:keys [model width on-close on-change]}]
(defn closeable
[& {:keys [on-close children]}]
[rc/h-box
:children (conj children
[rc/close-button
:div-size 25
:font-size 25
:top-offset -4
:left-offset 10
:on-click on-close])])

(defn closeable-text-box [& {:keys [width model on-change on-close]}]
[closeable
:on-close on-close
:children [[rc/input-text
:width width
:style {:width width ;; TODO: Not needed in standard re-com but caused by :all unset
:height "25px"
:padding (css-join "0px" styles/gs-5s)}
:model model
:change-on-blur? false
:on-change on-change]
[rc/close-button
:div-size 25
:font-size 25
:top-offset -4
:left-offset 10
:on-click on-close]]])
:on-change on-change]]])

(defn explanation-text [children]
[rc/v-box
Expand Down Expand Up @@ -274,6 +279,49 @@
[[:div "How should the log (" [material/print] ") buttons behave?"]]
settings-box-131])
[rc/line]
(let [ambiance @(rf/subscribe [::settings.subs/ambiance])]
[settings-box
[[rc/h-box
:align :center
:gap horizontal-gap
:children [[rc/label :label "Alias namespaces:"]
[rc/button
:class (styles/button ambiance)
:style {:width styles/gs-81s}
:label [rc/v-box
:align :center
:children ["+ ns, alias"]]
:on-click #(rf/dispatch [::settings.events/add-ns-alias])]]]
[rc/v-box
:width comp-section-width
:gap vertical-gap
:children (for [[id {:keys [ns-full ns-alias] :as m} :as s] @(rf/subscribe [::settings.subs/ns-aliases])]
^{:key id}
[closeable
:on-close #(rf/dispatch [::settings.events/remove-ns-alias id])
:children
[[rc/input-text
:width "150px"
:style {:width "150px"
:height "25px"
:padding (css-join "0px" styles/gs-5s)}
:model ns-full
:width "150px"
:change-on-blur? false
:on-change #(rf/dispatch [::settings.events/update-ns-alias id % ns-alias])]
[rc/input-text
:style {:width "150px"
:height "25px"
:padding (css-join "0px" styles/gs-5s)}
:model ns-alias
:width "150px"
:change-on-blur? false
:on-change #(rf/dispatch [::settings.events/update-ns-alias id ns-full %])]]])]]
[(if-let [[_ {:keys [ns-alias ns-full]}] (first @(rf/subscribe [::settings.subs/ns-aliases]))]
[:p "Display " [:code "::" (str ns-alias) "/x"] " when printing " [:code ":" (str ns-full) "/x"]]
"")]
settings-box-131])
[rc/line]
(let [ambiance @(rf/subscribe [::settings.subs/ambiance])]
[settings-box
[[rc/button
Expand Down
11 changes: 11 additions & 0 deletions src/day8/re_frame_10x/tools/datafy.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@

(defn deep-sorted-map [m]
(walk/postwalk #(cond->> % (map? %) (into (sorted-map))) m))

(defn alias [k ns->alias]
(if-let [a (get ns->alias (namespace k))]
(keyword (str ":" a) (name k))
k))

(defn alias-namespaces [m ns->alias]
(->> m
(walk/postwalk
#(cond-> %
(keyword? %) (alias ns->alias)))))
11 changes: 7 additions & 4 deletions src/day8/re_frame_10x/tools/highlight_hiccup.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
[rewrite-clj.node.stringz :refer [StringNode]]
[rewrite-clj.node.seq :refer [SeqNode]]
[day8.re-frame-10x.styles :as styles]
[day8.re-frame-10x.tools.datafy :as tools.datafy]
[day8.re-frame-10x.inlined-deps.re-frame.v1v1v2.re-frame.core :as rf]
[day8.re-frame-10x.panels.event.subs :as event.subs]))
[day8.re-frame-10x.panels.event.subs :as event.subs]
[day8.re-frame-10x.panels.settings.subs :as settings.subs]))

(def clj-core-macros #{'and 'binding 'case 'catch 'comment 'cond 'cond-> 'cond->> 'condp 'def
'defmacro 'defn 'defn- 'defmulti 'defmethod 'defonce 'defprotocol 'deftype
Expand Down Expand Up @@ -92,9 +94,10 @@
[:br])

(defmethod form KeywordNode [{:keys [k] :as node}]
[:code.clj__keyword {:class [(styles/clj-keyword)
(selected-style node)]}
(str k)])
(let [ns->alias (rf/subscribe [::settings.subs/ns->alias])]
[:code.clj__keyword {:class [(styles/clj-keyword)
(selected-style node)]}
(str (tools.datafy/alias k @ns->alias))]))

(defmethod form StringNode [{:keys [lines]}]
[:code.clj__string {:class (styles/clj-string)}
Expand Down
2 changes: 1 addition & 1 deletion src/day8/re_frame_10x/tools/reader/edn.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
(defn read-string-maybe [s]
(try (cljs.tools.reader.edn/read-string {:readers default-readers} s)
(catch :default _
nil)))
nil)))

0 comments on commit 2eda4a0

Please sign in to comment.