Created
May 16, 2026 12:44
-
-
Save cch1/60861d4debe3072ad4d226998ee98edb to your computer and use it in GitHub Desktop.
Tools to validate .cider-history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns cider-tools.history-cleaner | |
| (:require [clojure.java.io :as io] | |
| [clojure.pprint :as pprint])) | |
| (defn read-history-strings | |
| "Stage 1: | |
| Returns one raw source string per history entry." | |
| [reader] | |
| (sequence (comp (remove nil?)) | |
| (read reader))) | |
| (def cider-history-header | |
| ";; -*- coding: utf-8-unix -*- | |
| ;; Automatically written history of CIDER REPL session | |
| ;; Edit at your own risk | |
| ") | |
| (def marker "\n(+ 1 2)") | |
| (defn write-history-strings! | |
| "Writes history strings back out in valid .cider-history format." | |
| [writer history-strings] | |
| (.write writer cider-history-header) | |
| (.write writer "(") | |
| (binding [*print-length* nil | |
| *print-level* nil] | |
| (doall (sequence (comp (map (fn [hs] (clojure.string/replace hs "\"" "\\\""))) | |
| (map (fn [hs] (.write writer (str "\"" hs "\" "))))) | |
| history-strings))) | |
| (.write writer ")")) | |
| (def validate-history-strings | |
| (comp (map-indexed (fn [i hs] (try (binding [*default-data-reader-fn* tagged-literal | |
| *read-eval* false] | |
| (read-string hs) | |
| [hs i true]) | |
| (catch Exception e | |
| (tap> [i hs]) | |
| [hs i false e])))))) | |
| (defn filter-cider-history | |
| [in-path out-path] | |
| (let [hss (with-open [r (java.io.PushbackReader. (io/reader in-path))] | |
| (read-history-strings r)) | |
| {good true bad false} (group-by #(nth % 2) (sequence validate-history-strings hss))] | |
| (if (and out-path (empty? bad)) | |
| (let [bad #{551} | |
| hss (into [] (comp (filter (comp bad second)) | |
| (map first)) good)] | |
| (tap> hss) | |
| (with-open [w (io/writer out-path)] | |
| (write-history-strings! w (conj hss marker))) | |
| (count hss)) | |
| (frequencies (sequence (map (comp #(.getMessage %) #(nth % 3))) bad))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment