<commit_before>(ns refactor-nrepl.middleware
  (:require [cider.nrepl.middleware.util.cljs :as cljs]
            [cider.nrepl.middleware.util.misc :refer [err-info]]
            [clojure.tools.nrepl.middleware :refer [set-descriptor!]]
            [clojure.tools.nrepl.misc :refer [response-for]]
            [clojure.tools.nrepl.transport :as transport]
            [refactor-nrepl.analyzer :refer [warm-ast-cache]]
            [refactor-nrepl.artifacts
             :refer
             [artifact-list artifact-versions hotload-dependency]]
            [refactor-nrepl.config :as config]
            [refactor-nrepl.core :as core]
            [refactor-nrepl.extract-definition :refer [extract-definition]]
            [refactor-nrepl.find.find-locals :refer [find-used-locals]]
            [refactor-nrepl.find.find-symbol :refer [find-symbol]]
            [refactor-nrepl.find.find-used-publics :refer [find-used-publics]]
            [refactor-nrepl.find.find-macros :refer [warm-macro-occurrences-cache]]
            [refactor-nrepl.ns.clean-ns :refer [clean-ns]]
            [refactor-nrepl.ns.libspecs :refer [namespace-aliases]]
            [refactor-nrepl.ns.pprint :refer [pprint-ns]]
            [refactor-nrepl.ns.resolve-missing :refer [resolve-missing]]
            [refactor-nrepl.rename-file-or-dir :refer [rename-file-or-dir]]
            [refactor-nrepl.stubs-for-interface :refer [stubs-for-interface]]
            [clojure.walk :as walk]))

(defmacro ^:private with-errors-being-passed-on [transport msg & body]
  `(try
     ~@body
     (catch clojure.lang.ExceptionInfo e#
       (transport/send
        ~transport (response-for ~msg :error (.toString e#) :status :done)))
     (catch IllegalArgumentException e#
       (transport/send
        ~transport (response-for ~msg :error (.getMessage e#) :status :done)))
     (catch IllegalStateException e#
       (transport/send
        ~transport (response-for ~msg :error (.getMessage e#) :status :done)))
     (catch Exception e#
       (transport/send
        ~transport (response-for ~msg (err-info e# :refactor-nrepl-error))))))

(defmacro ^:private reply [transport msg & kvs]
  `(with-errors-being-passed-on ~transport ~msg
     (config/with-config ~msg
       (transport/send ~transport
                       (response-for ~msg ~(apply hash-map :status :done kvs))))))

(defn- bencode-friendly-data [data]
  ;; Bencode only supports byte strings, integers, lists and maps.
  ;; To prevent the bencode serializer in nrepl blowing up we manually
  ;; convert certain data types.
  ;; See refactor-nrepl#180 for more details.
  (walk/postwalk (fn [v]
                   (cond
                     (or (keyword? v) (symbol? v))
                     (str v)

                     (set? v) (list v)<commit_msg>Fix bencode serialization of keywords and symbols<commit_after>(ns refactor-nrepl.middleware
  (:require [cider.nrepl.middleware.util.cljs :as cljs]
            [cider.nrepl.middleware.util.misc :refer [err-info]]
            [clojure.tools.nrepl.middleware :refer [set-descriptor!]]
            [clojure.tools.nrepl.misc :refer [response-for]]
            [clojure.tools.nrepl.transport :as transport]
            [refactor-nrepl.analyzer :refer [warm-ast-cache]]
            [refactor-nrepl.artifacts
             :refer
             [artifact-list artifact-versions hotload-dependency]]
            [refactor-nrepl.config :as config]
            [refactor-nrepl.core :as core]
            [refactor-nrepl.extract-definition :refer [extract-definition]]
            [refactor-nrepl.find.find-locals :refer [find-used-locals]]
            [refactor-nrepl.find.find-symbol :refer [find-symbol]]
            [refactor-nrepl.find.find-used-publics :refer [find-used-publics]]
            [refactor-nrepl.find.find-macros :refer [warm-macro-occurrences-cache]]
            [refactor-nrepl.ns.clean-ns :refer [clean-ns]]
            [refactor-nrepl.ns.libspecs :refer [namespace-aliases]]
            [refactor-nrepl.ns.pprint :refer [pprint-ns]]
            [refactor-nrepl.ns.resolve-missing :refer [resolve-missing]]
            [refactor-nrepl.rename-file-or-dir :refer [rename-file-or-dir]]
            [refactor-nrepl.stubs-for-interface :refer [stubs-for-interface]]
            [clojure.walk :as walk]))

(defmacro ^:private with-errors-being-passed-on [transport msg & body]
  `(try
     ~@body
     (catch clojure.lang.ExceptionInfo e#
       (transport/send
        ~transport (response-for ~msg :error (.toString e#) :status :done)))
     (catch IllegalArgumentException e#
       (transport/send
        ~transport (response-for ~msg :error (.getMessage e#) :status :done)))
     (catch IllegalStateException e#
       (transport/send
        ~transport (response-for ~msg :error (.getMessage e#) :status :done)))
     (catch Exception e#
       (transport/send
        ~transport (response-for ~msg (err-info e# :refactor-nrepl-error))))))

(defmacro ^:private reply [transport msg & kvs]
  `(with-errors-being-passed-on ~transport ~msg
     (config/with-config ~msg
       (transport/send ~transport
                       (response-for ~msg ~(apply hash-map :status :done kvs))))))

(defn- bencode-friendly-data [data]
  ;; Bencode only supports byte strings, integers, lists and maps.
  ;; To prevent the bencode serializer in nrepl blowing up we manually
  ;; convert certain data types.
  ;; See refactor-nrepl#180 for more details.
  (walk/postwalk (fn [v]
                   (cond
                     (or (keyword? v) (symbol? v))
                     (if-let [prefix (core/prefix v)]
                       (core/fully-qualify prefix v)
                       (name v))

                     (set? v) (list v)
{'fraction_non_alphanumeric': 0.13102119460500963, 'fraction_numerical': 0.0010509721492380452, 'mean_word_length': 2.3667452830188678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1191247', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1813, 'n_words': 390}