2017-03-03 56 views
0

我想能够格式化整个clojure文件来看起来不错。我发现的最好的东西是clojures pprint。它在正确的位置进行缩进和换行。但它只能读clojure litterals。 Clojure文件以字符串形式读入。 read-string将仅采用字符串的第一个括号。在整个序列中映射读取字符串有许多我遇到的问题。有人知道一个自动的方法使clojure文件看起来很漂亮吗?不只是正确缩进?如何印刷clojure文件?

回答

3

您可以使用lein-zprint这将在源文件上运行zprint库。如果您是启动用户,则可以使用boot-fmt来处理您的文件,该文件也使用zprint。

zprint库将完全重新格式化您的源代码,以便像它知道如何制作它。它实际上会在每个级别上尝试几件事情,看看哪些是“更好”,并且内置了一些启发式方法,以便在垂直空间中尽可能多地生成尽可能多的信息,同时仍然看起来很“漂亮”。 它知道关于Clojure(和Clojurescript)源代码的lot,并且知道哪些函数需要不同类型的处理以及处理新的clojure.spec(cljs.spec)文件。

它几乎是荒谬的可配置的,所以通过一些工作,您可以调整它以按照您希望看到的方式输出代码。但即使没有任何配置,它通常会很好地使你的代码看起来不错。

在lein-zprint中使用它非常简单。 将[lein-zprint "0.1.16"]到:你project.clj的插件向量:

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

然后格式化源文件,只需调用在该文件上雷音的ZPrint:

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

除非你告诉它(在你的project.clj中做这件事很简单),它会将现有文件重命名为<file-name>.clj.old,这样你可以在尝试时比较它们。

下面是一个例子(显然格式不对):与$lein zprint 70 src/example/apply.clj格式化后

(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!")]))))) 

格式,它为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!")]))))) 
1

您可以使用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

+0

Weavejester是一个格式化程序,而不是代码优化器。 –