-
Notifications
You must be signed in to change notification settings - Fork 177
/
format.clj
47 lines (41 loc) · 1.43 KB
/
format.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(ns cider.nrepl.middleware.format
(:refer-clojure :exclude [read-string])
(:require [cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
[cider.nrepl.middleware.pprint :as pprint]
[clojure.string :as string]
[clojure.tools.nrepl.middleware :refer [set-descriptor!]]
[clojure.tools.reader.edn :as edn]
[clojure.tools.reader.reader-types :as readers]))
;; cljfmt.core takes many milliseconds to load and isn't used every session
;; so delay loading it.
(def ^:private reformat-string
(delay
(do
(require 'cljfmt.core)
(resolve 'cljfmt.core/reformat-string))))
(defn format-code-reply
[{:keys [code] :as msg}]
{:formatted-code (@reformat-string code)})
(defn- read-edn
"Returns a vector of EDN forms, read from the string s."
[s]
(let [reader (readers/string-push-back-reader s)
sentinel (Object.)]
(loop [forms []]
(let [form (edn/read {:eof sentinel} reader)]
(if (= sentinel form)
forms
(recur (conj forms form)))))))
(defn- format-edn
[edn pprint-fn]
(->> (read-edn edn)
(map #(with-out-str (pprint-fn %)))
string/join
string/trim))
(defn format-edn-reply
[{:keys [edn pprint-fn] :as msg}]
{:formatted-edn (format-edn edn pprint-fn)})
(defn handle-format [handler msg]
(with-safe-transport handler msg
"format-code" format-code-reply
"format-edn" format-edn-reply))