我想能够格式化整个clojure文件来看起来不错。我发现的最好的东西是clojures pprint。它在正确的位置进行缩进和换行。但它只能读clojure litterals。 Clojure文件以字符串形式读入。 read-string
将仅采用字符串的第一个括号。在整个序列中映射读取字符串有许多我遇到的问题。有人知道一个自动的方法使clojure文件看起来很漂亮吗?不只是正确缩进?如何印刷clojure文件?
0
A
回答
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
相关问题
- 1. 印刷值文件
- 2. 印刷文件的外部文件
- 3. 如何执行Clojure文件?
- 4. 如何运行Clojure文件?
- 5. 盲文印刷
- 6. 印刷阿拉伯文文本文件
- 7. 印刷元件
- 8. 如何修复盲文印刷?
- 9. 印刷IF条件
- 10. 如何在clojure中打印json数据?
- 11. 印刷生产线在文件
- 12. 计数及印刷出现在文件
- 13. Pyscripter和印刷生产线文件
- 14. 印刷的plist文件的NSLog
- 15. 如何用新的印刷线更换印刷线?
- 16. Clojure的文件
- 17. 如何编译文件中的Clojure
- 18. 如何使用clojure解析html文件?
- 19. 自定义地图的印刷:类型Clojure中
- 20. Clojure打印功能
- 21. 印刷文本的屏幕
- 22. 右对齐印刷文本
- 23. Sandcastle的印刷文档
- 24. 印刷文本 - pygame的
- 25. 隐藏文本从印刷
- 26. Python的印刷文本
- 27. RichTextBox旋转文字印刷
- 28. 批处理脚本 - 验证印刷文件去打印队列
- 29. 印刷
- 30. 如何打印pdf.js文件?
Weavejester是一个格式化程序,而不是代码优化器。 –