|
3 | 3 | [clojure.set :as set] |
4 | 4 | [lambdaisland.clj-diff.core :as seq-diff])) |
5 | 5 |
|
6 | | -(declare diff diff-similar) |
| 6 | +(declare diff diff-similar diff-meta) |
7 | 7 |
|
8 | 8 | (defrecord Mismatch [- +]) |
9 | 9 | (defrecord Deletion [-]) |
|
92 | 92 |
|
93 | 93 | (defn diff-seq [exp act] |
94 | 94 | (let [[rep del ins] (replacements (del+ins exp act))] |
95 | | - (->> exp |
96 | | - (diff-seq-replacements rep) |
97 | | - (diff-seq-deletions del) |
98 | | - (diff-seq-insertions ins) |
99 | | - (into [])))) |
| 95 | + (with-meta |
| 96 | + (->> exp |
| 97 | + (diff-seq-replacements rep) |
| 98 | + (diff-seq-deletions del) |
| 99 | + (diff-seq-insertions ins) |
| 100 | + (into [])) |
| 101 | + (diff-meta exp act)))) |
100 | 102 |
|
101 | 103 | (defn diff-set [exp act] |
102 | | - (into |
103 | | - (into #{} |
104 | | - (map (fn [e] |
105 | | - (if (contains? act e) |
106 | | - e |
107 | | - (->Deletion e)))) |
108 | | - exp) |
109 | | - (map ->Insertion) |
110 | | - (remove #(contains? exp %) act))) |
| 104 | + (with-meta |
| 105 | + (into |
| 106 | + (into #{} |
| 107 | + (map (fn [e] |
| 108 | + (if (contains? act e) |
| 109 | + e |
| 110 | + (->Deletion e)))) |
| 111 | + exp) |
| 112 | + (map ->Insertion) |
| 113 | + (remove #(contains? exp %) act)) |
| 114 | + (diff-meta exp act))) |
111 | 115 |
|
112 | 116 | (defn diff-map [exp act] |
113 | | - (let [exp-ks (set (keys exp)) |
114 | | - act-ks (set (keys act))] |
115 | | - (reduce |
116 | | - (fn [m k] |
117 | | - (case [(contains? exp-ks k) (contains? act-ks k)] |
118 | | - [true false] |
119 | | - (assoc m (->Deletion k) (get exp k)) |
120 | | - [false true] |
121 | | - (assoc m (->Insertion k) (get act k)) |
122 | | - [true true] |
123 | | - (assoc m k (diff (get exp k) (get act k))) |
| 117 | + (with-meta |
| 118 | + (let [exp-ks (set (keys exp)) |
| 119 | + act-ks (set (keys act))] |
| 120 | + (reduce |
| 121 | + (fn [m k] |
| 122 | + (case [(contains? exp-ks k) (contains? act-ks k)] |
| 123 | + [true false] |
| 124 | + (assoc m (->Deletion k) (get exp k)) |
| 125 | + [false true] |
| 126 | + (assoc m (->Insertion k) (get act k)) |
| 127 | + [true true] |
| 128 | + (assoc m k (diff (get exp k) (get act k))) |
124 | 129 | ; `[false false]` will never occur because `k` necessarily |
125 | 130 | ; originated from at least one of the two sets |
126 | | - )) |
127 | | - {} |
128 | | - (set/union exp-ks act-ks)))) |
| 131 | + )) |
| 132 | + {} |
| 133 | + (set/union exp-ks act-ks))) |
| 134 | + (diff-meta exp act))) |
| 135 | + |
| 136 | +(defn diff-meta [exp act] |
| 137 | + (when (or (meta exp) (meta act)) |
| 138 | + (diff-map (meta exp) (meta act)))) |
129 | 139 |
|
130 | 140 | (defn primitive? [x] |
131 | 141 | (or (number? x) (string? x) (boolean? x) (inst? x) (keyword? x) (symbol? x))) |
|
0 commit comments