<commit_before>(ns ccw.core.launch
  (:require [ccw.eclipse :as e]
            [clojure.tools.nrepl.server :refer [start-server stop-server default-handler]]
            [clojure.tools.nrepl.ack :refer [handle-ack]]
            [ccw.eclipse :as e])
  (:import ccw.CCWPlugin
           ccw.core.StaticStrings
           java.lang.System))

(defn default-nrepl-server-listener
  "Default listener printing on *out* the nrepl url"
  [{:keys [event-type port project]}]
  (when (= :creation event-type)
    (printf "nREPL server created nrepl://127.0.0.1:%s" port)
    (when project
      (printf " for project '%s'" project))
    (println)))

(defonce ^{:doc
  "Atom containing a Map of listener name -> listener fn.
   Listener fns will be called when interesting events occur wrt nrepl servers
   managed by Counterclockwise, like that: (a-listener-fn event-map).
   Events currently handled: :creation, with keys :event-type :creation, 
                             :port (Number, required) and :project (String, optional)" }
  nrepl-servers-listeners 
  (atom {:ccw/default #'default-nrepl-server-listener}))


(defn on-nrepl-server-instanciated
  "Called when a new nREPL server has been instanciated through Counterclockwise.
   port: the port to use to connect an nREPL client to the server
   project: the project name. May be nil."
  [port project]
  (doseq [[n l] @nrepl-servers-listeners]
    (try
      (l {:event-type :creation, :port port :project project})
      (catch Exception e
        (ccw.CCWPlugin/logError
          (format "Error while notifying nrepl listener '%s' of %s event with params %s"
                  n (:type l) (dissoc l :type))
          e)))))

;;;;;;;;;;;
;; nRepl ;;
;;;;;;;;;;;

(defonce ^{:author "<NAME>" :doc "Atom containing the map returned by start-server."}
  nrepl-server-map-delay (atom nil))

(defn- fill-nrepl-start-delay
  "Starts an nRepl given the port number."
  [server-map-delay port]
  (if server-map-delay
    server-map-delay
    (delay (start-server :port port :handler (handle-ack (default-handler))))))

(defn- fill-nrepl-stop-delay
  "Stops the instance of the running nRepl. Always returns nil."
  [server-map-delay]
  (if (and server-map-delay (realized? server-map-delay))
    (delay (stop-server (force server-map-delay)) nil)
    (delay nil)))

;; Public API
(defn ccw-nrepl-port
  "Return the currently open nRepl's port"
  []
  (:port (some-> @nrepl-server-map-delay force)))

(defn ccw-nrepl-start-if-necessary
  "Starts a nRepl if there is none started, reading the info from
  eclipse's runtime properties. The result is then stored in the
  ccw-nrepl-server-map atom. Returns the newly opened socket to it."
  []
  (let [nrepl-port (e/property-ccw-nrepl-port)]
    (when (= 0 nrepl-port)
      (CCWPlugin/log (str "nRepl port will be automatically selected")))
    (let [start-server-delay (swap! nrepl-server-map-delay fill-nrepl-start-delay nrepl-port)
          server-map (force start-server-delay)]
      (CCWPlugin/log (str "Started ccw nREPL server: nrepl://127.0.0.1:" (:port server-map))))))

(defn ccw-nrepl-stop
  "Stops the instance of the running nRepl on the underlying atom. Always returns nil."<commit_msg>fix(ccw.core-launch): Add check on message log "Started nREPL...." execution.<commit_after>(ns ccw.core.launch
  (:require [ccw.eclipse :as e]
            [clojure.tools.nrepl.server :refer [start-server stop-server default-handler]]
            [clojure.tools.nrepl.ack :refer [handle-ack]]
            [ccw.eclipse :as e])
  (:import ccw.CCWPlugin
           ccw.core.StaticStrings
           java.lang.System))

(defn default-nrepl-server-listener
  "Default listener printing on *out* the nrepl url"
  [{:keys [event-type port project]}]
  (when (= :creation event-type)
    (printf "nREPL server created nrepl://127.0.0.1:%s" port)
    (when project
      (printf " for project '%s'" project))
    (println)))

(defonce ^{:doc
  "Atom containing a Map of listener name -> listener fn.
   Listener fns will be called when interesting events occur wrt nrepl servers
   managed by Counterclockwise, like that: (a-listener-fn event-map).
   Events currently handled: :creation, with keys :event-type :creation,
                             :port (Number, required) and :project (String, optional)" }
  nrepl-servers-listeners
  (atom {:ccw/default #'default-nrepl-server-listener}))


(defn on-nrepl-server-instanciated
  "Called when a new nREPL server has been instanciated through Counterclockwise.
   port: the port to use to connect an nREPL client to the server
   project: the project name. May be nil."
  [port project]
  (doseq [[n l] @nrepl-servers-listeners]
    (try
      (l {:event-type :creation, :port port :project project})
      (catch Exception e
        (ccw.CCWPlugin/logError
          (format "Error while notifying nrepl listener '%s' of %s event with params %s"
                  n (:type l) (dissoc l :type))
          e)))))

;;;;;;;;;;;
;; nRepl ;;
;;;;;;;;;;;

(defonce ^{:author "<NAME>" :doc "Atom containing the map returned by start-server."}
  nrepl-server-map-delay (atom nil))

(defn- fill-nrepl-start-delay
  "Starts an nRepl given the port number."
  [server-map-delay port]
  (if server-map-delay
    server-map-delay
    (delay (start-server :port port :handler (handle-ack (default-handler))))))

(defn- fill-nrepl-stop-delay
  "Stops the instance of the running nRepl. Always returns nil."
  [server-map-delay]
  (if (and server-map-delay (realized? server-map-delay))
    (delay (stop-server (force server-map-delay)) nil)
    (delay nil)))

;; Public API
(defn ccw-nrepl-port
  "Return the currently open nRepl's port"
  []
  (:port (some-> @nrepl-server-map-delay force)))

(defn ccw-nrepl-start-if-necessary
  "Starts a nRepl if there is none started, reading the info from
  eclipse's runtime properties. The result is then stored in the
  ccw-nrepl-server-map atom. Returns the newly opened socket to it."
  []
  (let [nrepl-port (e/property-ccw-nrepl-port)]
    (when (= 0 nrepl-port)
      (CCWPlugin/log (str "nRepl port will be automatically selected")))
    (let [start-server-delay (swap! nrepl-server-map-delay fill-nrepl-start-delay nrepl-port)
          was-realized (realized? start-server-delay)
          server-map (force start-server-delay)]
      (if-not was-realized
        (CCWPlugin/log (str "Started ccw nREPL server: nrepl://127.0.0.1:" (:port server-map)))))))

(defn ccw-nrepl-stop
  "Stops the instance of the running nRepl on the underlying atom. Always returns nil."
{'fraction_non_alphanumeric': 0.13215050474151116, 'fraction_numerical': 0.0039767513000917715, 'mean_word_length': 3.1491116751269037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7041396', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2161, 'n_words': 668}