2017-03-03 52 views
0

我想能夠格式化整個clojure文件來看起來不錯。我發現的最好的東西是clojures pprint。它在正確的位置進行縮進和換行。但它只能讀clojure litterals。 Clojure文件以字符串形式讀入。 read-string將僅採用字符串的第一個括號。在整個序列中映射讀取字符串有許多我遇到的問題。有人知道一個自動的方法使clojure文件看起來很漂亮嗎?不只是正確縮進?如何印刷clojure文件?

回答

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 

檢查其README for the supported formatting options

+0

Weavejester是一個格式化程序,而不是代碼優化器。 –