user.clj 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. (ns user
  2. (:require
  3. [clojure.core.server :as server]
  4. [clojure.java.io :as io]
  5. [clojure.tools.namespace.repl :as ns]
  6. [clojure.tools.namespace.track :as track]))
  7. (ns/disable-reload!)
  8. (ns/set-refresh-dirs "clojure")
  9. (def *reloaded
  10. (atom nil))
  11. (add-watch #'ns/refresh-tracker ::watch
  12. (fn [_ _ old new]
  13. (when (empty? (::track/load new))
  14. (reset! *reloaded (::track/load old)))))
  15. (defn after-reload []
  16. (let [cnt (count @*reloaded)]
  17. (str "Reloaded " cnt " namespace" (when (> cnt 1) "s"))))
  18. (defn reload []
  19. (set! *warn-on-reflection* true)
  20. ; (set! *unchecked-math* :warn-on-boxed)
  21. (let [res (ns/refresh :after 'user/after-reload)]
  22. (if (instance? Throwable res)
  23. (throw res)
  24. res)))
  25. (def p-lock
  26. (Object.))
  27. (defn p-pos []
  28. (let [trace (->> (Thread/currentThread)
  29. (.getStackTrace)
  30. (seq))
  31. el ^StackTraceElement (nth trace 4)]
  32. (str "[" (clojure.lang.Compiler/demunge (.getClassName el)) " " (.getFileName el) ":" (.getLineNumber el) "]")))
  33. (defn p-impl [position form res]
  34. (let [form (clojure.walk/postwalk
  35. (fn [form]
  36. (if (and
  37. (list? form)
  38. (= 'user/p-impl (first form)))
  39. (clojure.lang.TaggedLiteral/create 'p (nth form 3))
  40. form))
  41. form)]
  42. (locking p-lock
  43. (println (str position " #p " form " => " (pr-str res))))
  44. res))
  45. (defn p [form]
  46. `(p-impl (p-pos) '~form ~form))
  47. (defn -main [& args]
  48. ;; setup repl
  49. (let [args (apply array-map args)
  50. port (or
  51. (some-> (get args "--port") parse-long)
  52. (+ 1024 (rand-int 64512)))
  53. file (io/file ".repl-port")]
  54. (println "Started Server Socket REPL on port" port)
  55. (spit file port)
  56. (.deleteOnExit file)
  57. (server/start-server
  58. {:name "repl"
  59. :port port
  60. :accept 'clojure.core.server/repl
  61. :server-daemon false})))