<commit_before>(ns devtools.core)

(def MAX_COLLECTION_ELEMENTS 10)
(def MAX_MAP_ELEMENTS 5)
(def MAX_SET_ELEMENTS 7)
(def ABBREVIATION "…")
(def INDEX_SEPARATOR ":")

(declare inlined-value-template)

; dirty
(defn cljs-value? [value]
  (or (exists? (aget value "meta"))
      (exists? (aget value "_meta"))
      (exists? (aget value "_hash"))))

(defn js-value? [value]
  (not (cljs-value? value)))

(defn template [tag style & more]
  (let [arr #js [tag (if (empty? style) #js {} #js {"style" style})]]
    (doseq [o more]
      (.push arr o))
    arr))

(defn reference [object & more]
  (let [arr #js ["object" #js {"object" object}]]
    (doseq [o more]
      (.push arr o))
    arr))

(defn spacer [& _] " ")

(defn nil-template [_]
  (template "span" "color:#808080" "nil"))

(defn keyword-template [value]
  (template "span" "color:#881391" (str ":" (name value))))

(defn symbol-template [value]
  (template "span" "color:#881391" (str value)))

(defn number-template [value]
  (if (integer? value)
    (template "span" "color:#1C00CF" value)
    (template "span" "color:#1C88CF" value)))

(defn index-template [value]
  (template "span" "color:#881391" value INDEX_SEPARATOR))

(defn deref-template [value]
  (cond
    (satisfies? IAtom value) (template "span" "color:#f0f" "#<Atom " (reference @value) ">")
    (satisfies? IVolatile value) (template "span" "color:#f0f" "#<Volatile " (reference @value) ">")
    :else (pr-str value))) ; TODO: should we handle IDelay and others? I believe it is not safe to dereference them here

; TODO: abbreviate long strings
(defn string-template [value]
  (template "span" "color:#C41A16" (str "\"" value "\"")))

(defn fn-template [value]
  (template "span" "color:#f00" (reference value) "fn"))

; TODO: convert to idiomatic clojure code
(defn header-collection-template [value]
  (let [arr (template "span" "" "[")]
    (doseq [x (take MAX_COLLECTION_ELEMENTS value)]
      (.push arr (inlined-value-template x) (spacer x)))
    (.pop arr)
    (if (> (count value) MAX_COLLECTION_ELEMENTS)
      (.push arr ABBREVIATION))
    (.push arr "]")
    arr))

(defn header-map-template [value]
  (let [arr (template "span" "" "{")
        v (seq value)]
    (doseq [[k v] (take MAX_MAP_ELEMENTS v)]
      (.push arr
             (inlined-value-template k) (spacer k)
             (inlined-value-template v) (spacer v)))
    (.pop arr)
    (if (> (count v) MAX_MAP_ELEMENTS)
      (.push arr ABBREVIATION))
    (.push arr "}")
    arr))

(defn header-set-template [value]
  (let [arr (template "span" "" "#{")]
    (doseq [x (take MAX_SET_ELEMENTS value)]
      (.push arr (inlined-value-template x) (spacer x)))
    (.pop arr)
    (if (> (count value) MAX_SET_ELEMENTS)
      (.push arr ABBREVIATION))
    (.push arr "}")
    arr))

(defn generic-template [value]
  (template "span" "" ">" (reference value) "<"))

(defn atomic-template [value]
  (cond
    (nil? value) (nil-template value)
    (string? value) (string-template value)
    (number? value) (number-template value)
    (keyword? value) (keyword-template value)
    (symbol? value) (symbol-template value)
    (fn? value) (fn-template value)
    (satisfies? IDeref value) (deref-template value)
    ))

(defn header-container-template [value]
  (cond
    (map? value) (header-map-template value)
    (set? value) (header-set-template value)
    (coll? value) (header-collection-template value)
    ))

(defn inlined-value-template [value]<commit_msg>Set color on each level<commit_after>(ns devtools.core)

(def MAX_COLLECTION_ELEMENTS 10)
(def MAX_MAP_ELEMENTS 5)
(def MAX_SET_ELEMENTS 7)
(def ABBREVIATION "…")
(def INDEX_SEPARATOR ":")

(declare inlined-value-template)

; dirty
(defn cljs-value? [value]
  (or (exists? (aget value "meta"))
      (exists? (aget value "_meta"))
      (exists? (aget value "_hash"))))

(defn js-value? [value]
  (not (cljs-value? value)))

(defn template [tag style & more]
  (let [arr #js [tag (if (empty? style) #js {} #js {"style" style})]]
    (doseq [o more]
      (.push arr o))
    arr))

(defn reference [object & more]
  (let [arr #js ["object" #js {"object" object}]]
    (doseq [o more]
      (.push arr o))
    arr))

(defn spacer [& _] " ")

(defn nil-template [_]
  (template "span" "color:#808080" "nil"))

(defn keyword-template [value]
  (template "span" "color:#881391" (str ":" (name value))))

(defn symbol-template [value]
  (template "span" "color:#881391" (str value)))

(defn number-template [value]
  (if (integer? value)
    (template "span" "color:#1C00CF" value)
    (template "span" "color:#1C88CF" value)))

(defn index-template [value]
  (template "span" "color:#881391" value INDEX_SEPARATOR))

(defn deref-template [value]
  (cond
    (satisfies? IAtom value) (template "span" "color:#f0f" "#<Atom " (reference @value) ">")
    (satisfies? IVolatile value) (template "span" "color:#f0f" "#<Volatile " (reference @value) ">")
    :else (pr-str value))) ; TODO: should we handle IDelay and others? I believe it is not safe to dereference them here

; TODO: abbreviate long strings
(defn string-template [value]
  (template "span" "color:#C41A16" (str "\"" value "\"")))

(defn fn-template [value]
  (template "span" "color:#f00" (reference value) "fn"))

; TODO: convert to idiomatic clojure code
(defn header-collection-template [value]
  (let [arr (template "span" "color:#000" "[")]
    (doseq [x (take MAX_COLLECTION_ELEMENTS value)]
      (.push arr (inlined-value-template x) (spacer x)))
    (.pop arr)
    (if (> (count value) MAX_COLLECTION_ELEMENTS)
      (.push arr ABBREVIATION))
    (.push arr "]")
    arr))

(defn header-map-template [value]
  (let [arr (template "span" "color:#000" "{")
        v (seq value)]
    (doseq [[k v] (take MAX_MAP_ELEMENTS v)]
      (.push arr
             (inlined-value-template k) (spacer k)
             (inlined-value-template v) (spacer v)))
    (.pop arr)
    (if (> (count v) MAX_MAP_ELEMENTS)
      (.push arr ABBREVIATION))
    (.push arr "}")
    arr))

(defn header-set-template [value]
  (let [arr (template "span" "color:#000" "#{")]
    (doseq [x (take MAX_SET_ELEMENTS value)]
      (.push arr (inlined-value-template x) (spacer x)))
    (.pop arr)
    (if (> (count value) MAX_SET_ELEMENTS)
      (.push arr ABBREVIATION))
    (.push arr "}")
    arr))

(defn generic-template [value]
  (template "span" "color:#000" (reference value)))

(defn atomic-template [value]
  (cond
    (nil? value) (nil-template value)
    (string? value) (string-template value)
    (number? value) (number-template value)
    (keyword? value) (keyword-template value)
    (symbol? value) (symbol-template value)
    (fn? value) (fn-template value)
    (satisfies? IDeref value) (deref-template value)
    ))

(defn header-container-template [value]
  (cond
    (map? value) (header-map-template value)
    (set? value) (header-set-template value)
    (coll? value) (header-collection-template value)
    ))

(defn inlined-value-template [value]
{'fraction_non_alphanumeric': 0.17092789428325195, 'fraction_numerical': 0.013789141051422005, 'mean_word_length': 3.626578073089701, 'pattern_counts': {'":': 4, '<': 8, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3588690', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2499, 'n_words': 717}