|
18 | 18 | [orchard.print :as print]) |
19 | 19 | (:import |
20 | 20 | (java.lang.reflect Constructor Field Method Modifier) |
21 | | - (java.util Arrays List Map))) |
| 21 | + (java.util Arrays List Map) |
| 22 | + (orchard.print Diff DiffColl))) |
22 | 23 |
|
23 | 24 | ;; |
24 | 25 | ;; Navigating Inspector State |
|
47 | 48 | :display-analytics-hint nil |
48 | 49 | :analytics-size-cutoff 100000 |
49 | 50 | :sort-maps false |
| 51 | + :only-diff false |
50 | 52 | :pretty-print false}) |
51 | 53 |
|
52 | 54 | (defn- reset-render-state [inspector] |
|
1038 | 1040 | (unindent) |
1039 | 1041 | (unindent)))) |
1040 | 1042 |
|
| 1043 | +(defmethod inspect DiffColl [{:keys [only-diff] :as inspector} ^DiffColl obj] |
| 1044 | + (let [val (cond-> (.coll obj) |
| 1045 | + only-diff print/diff-coll-hide-equal-items)] |
| 1046 | + (-> inspector |
| 1047 | + (render-class-name val) |
| 1048 | + (render-counted-length val) |
| 1049 | + (render-section-header "Diff contents") |
| 1050 | + (indent) |
| 1051 | + (render-value-maybe-expand val) |
| 1052 | + (unindent)))) |
| 1053 | + |
| 1054 | +(defmethod inspect Diff [inspector ^Diff obj] |
| 1055 | + (let [d1 (.d1 obj), d2 (.d2 obj)] |
| 1056 | + (-> inspector |
| 1057 | + (render-class-name obj) |
| 1058 | + (render-section-header "Diff") |
| 1059 | + (indent) |
| 1060 | + (render-labeled-value " Left" d1) |
| 1061 | + (render-labeled-value "Right" d2) |
| 1062 | + (unindent)))) |
| 1063 | + |
1041 | 1064 | (defn ns-refers-by-ns [^clojure.lang.Namespace ns] |
1042 | 1065 | (group-by (fn [^clojure.lang.Var v] (.ns v)) |
1043 | 1066 | (map val (ns-refers ns)))) |
|
1086 | 1109 | (unindent)) |
1087 | 1110 | inspector))) |
1088 | 1111 |
|
1089 | | -(defn render-view-mode [{:keys [value view-mode pretty-print] :as inspector}] |
| 1112 | +(defn render-view-mode [{:keys [value view-mode pretty-print only-diff] :as inspector}] |
1090 | 1113 | (if (some? value) |
1091 | 1114 | (let [supported (filter #(view-mode-supported? inspector %) view-mode-order) |
1092 | 1115 | add-circle #(if %2 (str "●" %1) %1) |
| 1116 | + diff? (print/diff-result? value) |
1093 | 1117 | view-mode-str (str (->> supported |
1094 | 1118 | (map #(add-circle (name %) (= % view-mode))) |
1095 | 1119 | (str/join " ")) |
1096 | | - " " (add-circle "pretty" pretty-print))] |
1097 | | - (-> (render-section-header inspector "View mode (press 'v' to cycle, 'P' to pretty-print)") |
| 1120 | + " " (add-circle "pretty" pretty-print) |
| 1121 | + (when diff? |
| 1122 | + (str " " (add-circle "only-diff" only-diff)))) |
| 1123 | + caption (format "View mode (press 'v' to cycle, 'P' to pretty-print%s)" |
| 1124 | + (if diff? ", 'D' to show only diffs" ""))] |
| 1125 | + (-> (render-section-header inspector caption) |
1098 | 1126 | (indent) |
1099 | 1127 | (render-indent view-mode-str) |
1100 | 1128 | (unindent))) |
|
1104 | 1132 | (seq (persistent! rendered))) |
1105 | 1133 |
|
1106 | 1134 | (defn inspect-render |
1107 | | - ([{:keys [max-atom-length max-value-length max-coll-size max-nested-depth value pretty-print] |
| 1135 | + ([{:keys [max-atom-length max-value-length max-coll-size max-nested-depth value |
| 1136 | + pretty-print only-diff] |
1108 | 1137 | :as inspector}] |
1109 | 1138 | (binding [print/*max-atom-length* max-atom-length |
1110 | 1139 | print/*max-total-length* max-value-length |
| 1140 | + print/*coll-show-only-diff* (boolean only-diff) |
1111 | 1141 | *print-length* max-coll-size |
1112 | 1142 | *print-level* (cond-> max-nested-depth |
1113 | 1143 | ;; In pretty mode a higher *print-level* |
|
1141 | 1171 | (assoc :view-mode (first (supported-view-modes inspector))) |
1142 | 1172 | inspect-render)))) |
1143 | 1173 |
|
1144 | | -(defn ^:deprecated clear |
1145 | | - "If necessary, use `(start inspector nil) instead.`" |
1146 | | - [inspector] |
1147 | | - (start inspector nil)) |
1148 | | - |
1149 | | -(defn ^:deprecated fresh |
1150 | | - "If necessary, use `(start nil)` instead." |
1151 | | - [] |
1152 | | - (start nil)) |
| 1174 | +(defn diff |
| 1175 | + "Perform a recursive diff on two values and return a structure suitable to be |
| 1176 | + viewed with the inspector." |
| 1177 | + [d1 d2] |
| 1178 | + (cond (= d1 d2) d1 |
| 1179 | + (not= (class d1) (class d2)) (print/->Diff d1 d2) |
| 1180 | + |
| 1181 | + (and (sequential? d1) (sequential? d2)) |
| 1182 | + (let [n (max (count d1) (count d2))] |
| 1183 | + (->> (mapv #(diff (nth d1 % print/nothing) (nth d2 % print/nothing)) |
| 1184 | + (range n)) |
| 1185 | + print/->DiffColl)) |
| 1186 | + |
| 1187 | + (and (map? d1) (map? d2)) |
| 1188 | + (print/->DiffColl |
| 1189 | + (->> (concat (keys d1) (keys d2)) |
| 1190 | + distinct |
| 1191 | + (mapv (fn [k] |
| 1192 | + [k (diff (get d1 k print/nothing) (get d2 k print/nothing))])) |
| 1193 | + (into {}))) |
| 1194 | + |
| 1195 | + :else (print/->Diff d1 d2))) |
0 commit comments