如何打印 clojure 文件?

How to pprint a clojure file?

我希望能够格式化整个 clojure 文件以使其看起来不错。我发现的最好的东西是 clojures pprint。它在正确的位置进行缩进和换行。但是它只能读取 clojure 文字。 Clojure 文件作为字符串读入。 read-string 只会取字符串的第一个括号。在整个序列上映射读取字符串有很多我 运行 遇到的问题。有人知道一种使 clojure 文件看起来漂亮的自动方法吗?不只是缩进正确吗?

您可以使用 weavejester/cljfmt:

$ boot -d cljfmt:0.5.6 repl

boot.user=> (require '[cljfmt.core :as cljfmt])
nil

boot.user=> (println (cljfmt/reformat-string
       #_=> "( let [x 3
       #_=>   y 4]
       #_=>   (+ (* x x
       #_=>   )(* y y)
       #_=>  ))"))

(let [x 3
      y 4]
  (+ (* x x) (* y y)))
nil

检查其README for the supported formatting options

您可以在源文件上使用 lein-zprint which will run the zprint 库。如果你是 boot 用户,你可以使用 boot-fmt 来处理你的文件,它也使用 zprint。

zprint 库将从头开始完全重新格式化您的源代码,使其尽可能漂亮。它实际上在每个级别上尝试了一些事情来查看哪个是"better",并且内置了许多启发式方法以尝试产生适合垂直space中尽可能多的信息的东西,而仍在寻找 "pretty"。 它对 Clojure(和 Clojurescript)源代码了解很多,并且知道哪些函数需要不同类型的处理以及处理新的clojure.spec(cljs.spec)文件也很好。

它的可配置性几乎是荒谬的,因此只需稍加工作,您就可以调整它以按照您希望的方式输出代码。但即使没有任何配置,它通常也能很好地使您的代码看起来不错。

将它与 lein-zprint 一起使用非常简单。 将 [lein-zprint "0.1.16"] 放入 project.clj:

的 :plugins 向量中

:plugins [[lein-zprint "0.1.16"]]

然后,要格式化源文件,只需对该文件调用 lein zprint:

$ lein zprint src/<project>/<file-name>.clj

除非你另有说明(这在你的 project.clj 中很容易做到),它会将现有文件重命名为 <file-name>.clj.old 以便你在尝试时可以比较它们.

这是一个例子(显然格式不正确):

(defn apply-style-x
  "Given an existing-map and a new-map, if the new-map specifies a   
  style, apply it if it exists.  Otherwise do nothing. Return   
  [updated-map new-doc-map error-string]"
  [doc-string doc-map existing-map new-map] (let [style-name
  (get new-map :style :not-specified) ] (if
  (= style-name :not-specified) [existing-map doc-map nil]
  (let [style-map ( if (= style-name :default) 
  (get-default-options) (get-in existing-map [:style-map style-name]))]
  (cond (nil? style-name)
  [existing-map doc-map "Can't specify a style of nil!"] 
  style-map [(merge-deep existing-map style-map) (when doc-map 
  (diff-deep-doc (str doc-string " specified :style " style-name)
  doc-map existing-map style-map)) nil] :else
  [existing-map doc-map (str "Style '" style-name "' not found!")])))))

$lein zprint 70 src/example/apply.clj 格式化后 将其格式化为 70 列,以使其更适合此答案:

(defn apply-style-x
  "Given an existing-map and a new-map, if the new-map specifies a   
  style, apply it if it exists.  Otherwise do nothing. Return   
  [updated-map new-doc-map error-string]"
  [doc-string doc-map existing-map new-map]
  (let [style-name (get new-map :style :not-specified)]
    (if (= style-name :not-specified)
      [existing-map doc-map nil]
      (let [style-map (if (= style-name :default)
                        (get-default-options)
                        (get-in existing-map
                                [:style-map style-name]))]
        (cond
          (nil? style-name) [existing-map doc-map
                             "Can't specify a style of nil!"]
          style-map
            [(merge-deep existing-map style-map)
             (when doc-map
               (diff-deep-doc
                 (str doc-string " specified :style " style-name)
                 doc-map
                 existing-map
                 style-map)) nil]
          :else [existing-map doc-map
                 (str "Style '" style-name "' not found!")])))))