如何印刷一个clojure文件? [英] How to pprint a clojure file?

查看:164
本文介绍了如何印刷一个clojure文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

您可以使用 lein-zprint ,它将运行 zprint 库在您的源文件。如果你是一个启动用户,你可以使用boot-fmt来处理你的文件,它也使用zprint。



zprint库将从头开始重新格式化你的源代码就像它知道如何制作它一样漂亮。它实际上会在每个级别上尝试一些事情,看看哪个更好,并且有一些内置的启发式方法,试图在纵向空间中生成尽可能多的信息,同时仍然看起来很漂亮。
它知道关于Clojure(和Clojurescript)源代码的一大堆,知道哪些函数需要不同类型的处理,以及处理新的clojure.spec(cljs.spec)文件。

它几乎是荒谬的可配置的,所以只需要一点点工作,就可以调整它以输出代码的方式来查看它。但即使没有任何配置,它通常做得很好,使您的代码看起来不错。

在lein-zprint中使用它非常简单。
[lein-zprint0.1.16] 放置到您的project.clj的插件向量中:

:plugins [[lein-zprint0.1.16]]



然后,一个源文件,只需在该文件上调用lein zprint即可:



$ lein zprint src /< project> /< file-name> ;.除非你另有说明(在你的project.clj中做这件事是微不足道的),否则它会把现有的文件重命名为<$ $ b

c $ c>< file-name> .clj.old ,让您在试用时比较它们。下面是一个例子(格式很明显):

pre $ (defn apply-style-x
地图和新地图,如果新地图指定了
样式,则应用它,否则不做任何事情。 b $ b [doc-string doc-map existing-map new-map](let-style-name
(get new-map:style:not-specified)](if
(= style -name:未指定)[现有地图doc-map nil]
(let-style-map(if(= style-name:default)
(get-default-options)在existing-map [:style-map style-name]))]
(cond(nil?样式名称)
[现有地图doc-map无法指定样式的nil!]
样式地图[(merge-deep现有地图样式地图)(当doc-
(diff-deep-doc(str doc-stringspecified:stylestyle-name)
doc-map现有地图style-map))nil]:else
[现有-map doc-map(strStylestyle-name'not found!)])))))

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

 (defn apply-style-x 
给定一个现有的map和一个新的map,如果new-map指定了
风格,则应用它,否则不要做任何事情。 doc-map现有的地图新的地图]
(让样式名称(得到新的地图:样式:未指定)]
(如果(=样式名称:未指定)
[现有地图doc-map nil]
(让[style-map(if(= sty le-name:default)
(get-default-options)
(get-in existing-map
[:style-map style-name]))]
(cond
(无?样式名称)[现有地图doc-map
不能指定样式为零!]
样式地图
[(merge-deep现有地图样式地图)
(当doc-map
(diff-deep-doc
(str doc-stringspecified:stylestyle-name)
doc-map
existing-地图
style-map))nil]
:else [现有地图doc-map
(strStylestyle-name'not found!)])))) )


I want to be able to format an entire clojure file to look nice. The nicest thing I have found is clojures pprint. It does indentation and line breaks at correct locations. However it can only read clojure litterals. Clojure files are read in as strings. read-string will only take the first parenthesis of a string. Mapping read-string over the whole sequence has a host of issues I am running into. Does someone know of an automatic way to make a clojure file look pretty? Not just indent it correctly?

解决方案

You can use lein-zprint which will run the zprint library over your source files. If you are a boot user, you can use boot-fmt to process your files, which also uses zprint.

The zprint library will completely reformat your source from scratch to be as pretty as it knows how to make it. It actually tries a couple of things at each level to see which is "better", and has a number of heuristics built in to try to produce something that fits as much information as it can in the vertical space while still looking "pretty". It knows a lot about Clojure (and Clojurescript) source code, and knows which functions need different kinds of processing as well as handling the new clojure.spec (cljs.spec) files well too.

It is almost absurdly configurable, so with a little work you can tune it to output the code the way that you want to see it. But even without any configuration, it generally does a good job making your code look nice.

Using it with lein-zprint is pretty trivial. Place [lein-zprint "0.1.16"] into the :plugins vector of your project.clj:

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

Then, to format a source file, simply invoke lein zprint on that file:

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

Unless you tell it otherwise (which is trivial to do in your project.clj), it will rename the existing file to <file-name>.clj.old so that you have them to compare while you are trying it out.

Here is an example (obviously poorly formatted):

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

after formatting with $lein zprint 70 src/example/apply.clj which formats it for 70 columns, to make it fit better for this answer:

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

这篇关于如何印刷一个clojure文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆