<commit_before>(ns cljs.go
  (:require [cljs.analyzer :as ana]
            [cljs.env :as env]
            [cljs.go.compiler]
            [clojure.string :as s]
            [clojure.pprint :as pp]
            [clojure.java.io :as io]
            [clojure.java.shell :as sh]))

(set! *warn-on-reflection* true)

(defn elide-children [_ ast]
  (dissoc ast :children))

(defn simplify-env [_ {:keys [op] :as ast}]
  (cond-> ast
          (= op :fn) (update-in [:methods] #(map (partial simplify-env nil) %))
          (= op :set!) (update-in [:target] (partial simplify-env nil))
          :then (update-in [:env] #(select-keys % [:context :column :line]))))

(defn cljs->ast
  ([in] (cljs->ast in (ana/empty-env)))
  ([in env]
     (env/ensure
      (binding [ana/*passes* [elide-children simplify-env ana/infer-type]
                ana/*cljs-ns* (or ana/*cljs-ns* 'cljs.user)]
        (cljs.compiler/setup-native-defs env)
        (doall (map #(ana/analyze (cljs.compiler/ensure-ns-exist env) %) in))))))

(defn ast->go [in]
  (try
    (env/ensure
     (with-out-str
       (binding [ana/*cljs-static-fns* true
                 cljs.compiler/*go-defs* (or cljs.compiler/*go-defs* (atom #{}))]
         (dorun (map cljs.compiler/emit in)))))
    (catch Throwable t
      (.printStackTrace t)
      (throw t))))

(defn godef
  ([var] (godef nil var))
  ([package var]
     (let [in (str "package _;"
                   (when (seq (str package))
                     (str "import \"" package "\";"))
                   "var _ = " (cond->> var package (str (last (s/split package #"\.")) ".")))
           {:keys [exit err out]} (sh/sh "godef" "-i" "-t" "-o" (str (count in)) :in in)]
       (if (zero? exit)
         (let [[loc & def] (s/split-lines out)
               [file line col] (s/split loc #":")]
           (merge
            {:file file :def (s/trim (s/join "\n" (remove (comp empty? s/trim) def)))}
            (when (and line col)
              {:line (biginteger line) :col (biginteger col)}) ))
         (println err)))))

(defn go-get [package]
  (let [{:keys [exit err]} (sh/sh "go" "get" package)]
    (assert (zero? exit) err)))

(defn get-goimports []
  (go-get "code.google.com/p/go.tools/cmd/goimports"))

(defn goimports [in]
  (get-goimports)
  (let [{:keys [exit out err]} (sh/sh "goimports" :in in)]
    (if (zero? exit)
      out
      (do (println err) in))))

(defn goimports-file [file]<commit_msg>godoc, with a pretty meaningless and simplistic classifier<commit_after>(ns cljs.go
  (:require [cljs.analyzer :as ana]
            [cljs.env :as env]
            [cljs.go.compiler]
            [clojure.string :as s]
            [clojure.pprint :as pp]
            [clojure.java.io :as io]
            [clojure.java.shell :as sh]))

(set! *warn-on-reflection* true)

(defn elide-children [_ ast]
  (dissoc ast :children))

(defn simplify-env [_ {:keys [op] :as ast}]
  (cond-> ast
          (= op :fn) (update-in [:methods] #(map (partial simplify-env nil) %))
          (= op :set!) (update-in [:target] (partial simplify-env nil))
          :then (update-in [:env] #(select-keys % [:context :column :line]))))

(defn cljs->ast
  ([in] (cljs->ast in (ana/empty-env)))
  ([in env]
     (env/ensure
      (binding [ana/*passes* [elide-children simplify-env ana/infer-type]
                ana/*cljs-ns* (or ana/*cljs-ns* 'cljs.user)]
        (cljs.compiler/setup-native-defs env)
        (doall (map #(ana/analyze (cljs.compiler/ensure-ns-exist env) %) in))))))

(defn ast->go [in]
  (try
    (env/ensure
     (with-out-str
       (binding [ana/*cljs-static-fns* true
                 cljs.compiler/*go-defs* (or cljs.compiler/*go-defs* (atom #{}))]
         (dorun (map cljs.compiler/emit in)))))
    (catch Throwable t
      (.printStackTrace t)
      (throw t))))

(defn godef
  ([var] (godef nil var))
  ([package var]
     (let [in (str "package _;"
                   (when (seq (str package))
                     (str "import \"" package "\";"))
                   "var _ = " (cond->> var package (str (last (s/split package #"\.")) ".")))
           {:keys [exit err out]} (sh/sh "godef" "-i" "-t" "-o" (str (count in)) :in in)]
       (if (zero? exit)
         (let [[loc & def] (s/split-lines out)
               [file line col] (s/split loc #":")]
           (merge
            {:file file :def (s/trim (s/join "\n" (remove (comp empty? s/trim) def)))}
            (when (and line col)
              {:line (biginteger line) :col (biginteger col)}) ))
         (println err)))))

(defn godoc [package name]
  (let [{:keys [exit err out]} (sh/sh "godoc" package name)
        out (s/trim out)]
    (if (and (zero? exit) (not= "No match found." out))
      (->> (s/split out #"\n\n+")
           (group-by #(let [[what name s-or-i method ] (s/split % #" ")
                            name (-> (if (= \( (first name)) method name)
                                     (s/split #"\(")
                                     first
                                     symbol)]
                        (or
                         (when (= "type" what)
                           (some->> s-or-i keyword #{:struct :interface} (vector name)))
                         [name (keyword what)]))))
      (println err))))

(defn go-get [package]
  (let [{:keys [exit err]} (sh/sh "go" "get" package)]
    (assert (zero? exit) err)))

(defn get-goimports []
  (go-get "code.google.com/p/go.tools/cmd/goimports"))

(defn goimports [in]
  (get-goimports)
  (let [{:keys [exit out err]} (sh/sh "goimports" :in in)]
    (if (zero? exit)
      out
      (do (println err) in))))

(defn goimports-file [file]
{'fraction_non_alphanumeric': 0.18566601458296284, 'fraction_numerical': 0.0, 'mean_word_length': 1.9506820566631689, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3462909', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1961, 'n_words': 530}