<commit_before>(ns shadow.cljs.npm.client
  (:require [cljs.reader :as reader]
            ["readline" :as rl]
            ["net" :as node-net]
            [shadow.cljs.npm.util :as util]
            [clojure.string :as str]))

(defn run [project-root config server-pid args]
  (let [{:keys [socket-repl] :as ports}
        (-> (util/slurp server-pid)
            (reader/read-string))]

    (if-not socket-repl
      (prn [:no-socket-repl-port server-pid ports])

      (let [socket
            (node-net/connect socket-repl "localhost")

            last-prompt-ref
            (volatile! nil)

            rl
            (rl/createInterface
              #js {:input js/process.stdin
                   :output js/process.stdout
                   :completer
                   (fn [prefix callback]
                     ;; FIXME: hook this up properly
                     (callback nil (clj->js [[] prefix])))})]

        (.on socket "connect"
          (fn [err]
            (if err
              (println "shadow-cljs - socket connect failed")

              (do (println "shadow-cljs - connected to server")

                  ;; FIXME: this is an ugly hack that will be removed soon
                  (when-not (= ["--repl"] args)
                    (.write socket (str "(shadow.cljs.devtools.cli/from-remote " (pr-str (into [] args)) ")\n"))
                    (when-not (some #{"--dev"} args)
                      (.write socket (str ":repl/quit\n"))))

                  (.on rl "line"
                    (fn [line]
                      (.write socket (str line "\n"))))

                  ;; CTRL+D closes the rl
                  (.on rl "close"
                    (fn []
                      (.end socket)
                      (println)))
                  ))))

        (.on socket "data"
          (fn [data]
            (.pause rl)
            (let [txt (.toString data)

                  prompts
                  (re-seq #"\[(\d+):(\d+)\]\~([^=> \n]+)=> " txt)]

              (doseq [[prompt root-id level-id ns :as m] prompts]
                (vreset! last-prompt-ref {:text prompt
                                          :ns (symbol ns)
                                          :level (js/parseInt level-id 10)
                                          :root (js/parseInt root-id 10)})
                (.setPrompt rl prompt))<commit_msg>tweak REPL client<commit_after>(ns shadow.cljs.npm.client
  (:require [cljs.reader :as reader]
            ["readline" :as rl]
            ["net" :as node-net]
            [shadow.cljs.npm.util :as util]
            [clojure.string :as str]))

(defn run [project-root config server-pid args]
  (let [{:keys [socket-repl] :as ports}
        (-> (util/slurp server-pid)
            (reader/read-string))]

    (if-not socket-repl
      (prn [:no-socket-repl-port server-pid ports])

      (let [socket
            (node-net/connect socket-repl "localhost")

            last-prompt-ref
            (volatile! nil)

            rl
            (rl/createInterface
              #js {:input js/process.stdin
                   :output js/process.stdout
                   :completer
                   (fn [prefix callback]
                     (let [last-prompt @last-prompt-ref]
                       ;; without a prompt we can't autocomplete
                       (if-not last-prompt
                         (callback nil (clj->js [[] prefix]))

                         ;; FIXME: hook this up properly
                         (callback nil (clj->js [[] prefix])))))})

            write
            (fn [text]
              ;; assume that everything we send is (read) which reads something
              ;; we can never autocomplete
              ;; and only a new prompt enables it
              (vreset! last-prompt-ref nil)
              (.write socket text))]

        (.on socket "connect"
          (fn [err]
            (if err
              (println "shadow-cljs - socket connect failed")

              (do (println "shadow-cljs - connected to server")

                  ;; FIXME: this is an ugly hack that will be removed soon
                  (when-not (= ["--repl"] args)
                    (write (str "(shadow.cljs.devtools.cli/from-remote " (pr-str (into [] args)) ")\n"))
                    (when-not (some #{"--dev"} args)
                      (write (str ":repl/quit\n"))))

                  (.on rl "line"
                    (fn [line]
                      (write (str line "\n"))))

                  ;; CTRL+D closes the rl
                  (.on rl "close"
                    (fn []
                      (.end socket)
                      (println)))
                  ))))

        (.on socket "data"
          (fn [data]
            (.pause rl)
            (let [txt (.toString data)

                  prompts
                  (re-seq #"\[(\d+):(\d+)\]\~([^=> \n]+)=> " txt)]

              (doseq [[prompt root-id level-id ns :as m] prompts]
                (vreset! last-prompt-ref {:text prompt
                                          :ns (symbol ns)
                                          :level (js/parseInt level-id 10)
                                          :root (js/parseInt root-id 10)})
                (.setPrompt rl prompt))
{'fraction_non_alphanumeric': 0.13011223131063343, 'fraction_numerical': 0.0015217804831653035, 'mean_word_length': 1.0981644054269752, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6798544', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1365, 'n_words': 397}