/src/chlorine/configs.cljs

https://github.com/mauricioszabo/atom-chlorine · ClojureScript · 49 lines · 43 code · 5 blank · 1 comment · 0 complexity · 5265a3df65e78d226da0ada17c55705b MD5 · raw file

  1. (ns chlorine.configs
  2. (:require [chlorine.state :refer [configs state]]
  3. [clojure.walk :as walk]
  4. [chlorine.utils :as aux]
  5. ; [check.core :refer-macros [check]]
  6. [clojure.test]))
  7. (defn- propagate-to-config [new-value]
  8. (let [config (. js/atom -config)]
  9. (doseq [[key value] (:config new-value)
  10. :let [atom-key (str "chlorine." (name key))
  11. atom-value (clj->js value)]
  12. :when (not= atom-value (.get config atom-key))]
  13. (.set config atom-key atom-value))))
  14. (defn- propagate-to-state [new-value]
  15. (let [normalized (-> new-value js->clj walk/keywordize-keys
  16. (update :eval-mode keyword)
  17. (update :refresh-mode keyword))]
  18. (when-not (-> @state :config (= normalized))
  19. (swap! state assoc :config normalized))))
  20. (defn- transform-config [configs]
  21. (let [type-for (fn [{:keys [type]}] (if (vector? type)
  22. :string
  23. type))]
  24. (->> configs
  25. (map (fn [[k v]] [k (cond-> {:type (type-for v)
  26. :title (:description v)
  27. :default (:default v)}
  28. (vector? (:type v)) (assoc :enum (:type v)))]))
  29. (into {}))))
  30. (defn get-configs []
  31. (-> configs
  32. transform-config
  33. (assoc :console-pos {:type "string"
  34. :title "Position of console when connecting REPL"
  35. :enum ["right" "down"]
  36. :default "right"}
  37. :autodetect-nrepl {:type "boolean"
  38. :title "Auto-detect nREPL port when connecting to socket"
  39. :default false})
  40. clj->js))
  41. (defn observe-configs! []
  42. (.add @aux/subscriptions
  43. (.. js/atom -config (observe "chlorine" propagate-to-state)))
  44. (add-watch state :configs (fn [_ _ _ value] (propagate-to-config value))))