你会如何让clojure读取器保持格式化? [英] how would you get the clojure reader to keep formatting?
问题描述
如果我的代码如下所示:
If I have code that looks like this:
(apply + 0
[1 2 3 4])
我把它通过读者,它变成
and I put it through the reader, it becomes
(应用+ 0 [1 2 3 4])
,并且格式化(回车和间隔)丢失
(apply + 0 [1 2 3 4])
and the formatting (the carriage return and the spacing) is lost
在读取代码时,是否还要保存代码的格式?
Is there anyway to save the formatting of the code when it is read?
(fact [{:type :section
:title "Normal Operation"
:tag "normal-operation"}]
"The most straightforward code is one where no issues raised. This
can be seen in Example {{ribol-normal-eq}} below:"
[{:type :image :href "ribol-normal.png"}]
[[{:tag "ribol-normal-eq"}]]
(manage ;; L2
[1 2 (manage 3)]) ;; L1 and L0
=> [1 2 3]
转换为:
最直接的代码是没有提出问题的代码。这个
可以在下面的例子1.1中看到:
The most straightforward code is one where no issues raised. This can be seen in Example 1.1 below:
[[ribol-normal.png]]
[[ribol-normal.png]]
(manage ;; L2
[1 2 (manage 3)]) ;; L1 and L0
=> [1 2 3]
推荐答案
使用 clojure.pprint / write
与 code-dispatch
。
(clojure.pprint/write
'(apply + 0 [1 2 3 4])
:dispatch clojure.pprint/code-dispatch)
它不会给你想要的缩进,因为应用形式太短,并且因为你的换行只对最后一个参数是相当任意的。但是如果你想打印Clojure代码,这是正确的事情。
It won't give you the indentation you desire, because the apply form is too short and because your line-break only for the last argument is quite arbitrary. However if you want to print Clojure code, this is the right thing to do. It will correctly print let- and various other forms.
以下是一个较长表单的示例
Here is an example with a longer form
(clojure.pprint/write
'(defn plugin
"A leiningen plugin project template."
[name]
(let [render (renderer "plugin")
unprefixed (if (.startsWith name "lein-")
(subs name 5)
name)
data {:name name,
:unprefixed-name unprefixed,
:sanitized (sanitize unprefixed),
:year (year)}]
(main/info
(str "Generating a fresh Leiningen plugin called " name "."))
(->files
data
["project.clj" (render "project.clj" data)]
["README.md" (render "README.md" data)]
[".gitignore" (render "gitignore" data)]
["src/leiningen/{{sanitized}}.clj" (render "name.clj" data)]
["LICENSE" (render "LICENSE" data)])))
:dispatch
clojure.pprint/code-dispatch)
=>
(defn plugin
"A leiningen plugin project template."
[name]
(let [render (renderer "plugin")
unprefixed (if (.startsWith name "lein-") (subs name 5) name)
data {:name name,
:unprefixed-name unprefixed,
:sanitized (sanitize unprefixed),
:year (year)}]
(main/info
(str "Generating a fresh Leiningen plugin called " name "."))
(->files
data
["project.clj" (render "project.clj" data)]
["README.md" (render "README.md" data)]
[".gitignore" (render "gitignore" data)]
["src/leiningen/{{sanitized}}.clj" (render "name.clj" data)]
["LICENSE" (render "LICENSE" data)])))
这篇关于你会如何让clojure读取器保持格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!