如何让clojure阅读器保持格式?
如果我有这样的代码:如何让clojure阅读器保持格式?,clojure,Clojure,如果我有这样的代码: (apply + 0 [1 2 3 4]) 我把它交给读者,它就变成了 (应用+0[1 2 3 4])和格式(回车符和间距)丢失 在读取代码时,是否仍有保存代码格式的方法 例如: (fact [{:type :section :title "Normal Operation" :tag "normal-operation"}] "The most straightforward code is one where no iss
(apply + 0
[1 2 3 4])
我把它交给读者,它就变成了
(应用+0[1 2 3 4])
和格式(回车符和间距)丢失
在读取代码时,是否仍有保存代码格式的方法
例如:
(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.1中看到:
[[ribol normal.png]]
1.1
这是朝着这个方向迈出的第一步。但是它的开发还很早。将
clojure.pprint/write
与代码调度一起使用
(clojure.pprint/write
'(apply + 0 [1 2 3 4])
:dispatch clojure.pprint/code-dispatch)
它不会给你想要的缩进,因为apply表单太短,而且你只在最后一个参数上换行是非常随意的。但是,如果您想打印Clojure代码,这是正确的做法。它将正确打印let和其他各种表单
下面是一个较长形式的示例
(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)
=>
六羟甲基三聚氰胺六甲醚。。。这不是我想要的。。。因为它需要打印任意中断,但这是一个非常有用的方法。谢谢如果要打印任意中断,需要传递缩进字符串。读者会将你的申请表作为列表打印出来。你能举个例子吗?你的输入源到底是什么?表格还是字符串?关键是,如果您使用read,字符串将被读取为一个对象,即案例中的列表(type(with in str)(apply+0[1 2 3 4])(read))=>clojure.lang.PersistentList。列表对象不会有缩进或换行。请参阅更新的问题以了解动机。我一直关注的主要问题是如何在不丢失格式化或注释的情况下获取代码
(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)])))