你如何让clojure读者保持格式化?

时间:2013-08-12 10:35:21

标签: clojure

如果我的代码如下:

(apply + 0
   [1 2 3 4])

我将它通过读者,它变成了

(apply + 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

(manage                          ;; L2
   [1 2 (manage 3)])             ;; L1 and L0

=> [1 2 3]

2 个答案:

答案 0 :(得分:1)

clojure.pprint/writecode-dispatch一起使用。

(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)

=>

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

答案 1 :(得分:0)

sjacket是迈向这个方向的第一步。但它的发展还处于早期阶段。