Skip to content

Commit a172ecd

Browse files
authored
Merge pull request #362 from pbwolf/issue-112-indent-comment-option
Optionally indent "line comments" (set off by ;;)
2 parents c87493e + c41ac6c commit a172ecd

File tree

4 files changed

+185
-8
lines changed

4 files changed

+185
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ In order to load the standard configuration file from Leiningen, add the
238238
the same as `:indents`, except that this will **append** to the
239239
default indents.
240240

241+
* `:indent-line-comments?` -
242+
true if cljfmt should align whole-line `;;` comments with the code.
243+
Defaults to false.
244+
241245
* `:alias-map` -
242246
a map of namespace alias strings to fully qualified namespace
243247
names. This option is unnecessary in most cases, because cljfmt

cljfmt/src/cljfmt/core.cljc

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
(defn- comment? [zloc]
9191
(some-> zloc z/node n/comment?))
9292

93+
(defn- line-comment? [zloc]
94+
(and (comment? zloc) (re-matches #"(?s);;([^;].*)?" (z/string zloc))))
95+
9396
(defn- comma? [zloc]
9497
(some-> zloc z/node n/comma?))
9598

@@ -145,14 +148,27 @@
145148
(defn- comment-next? [zloc]
146149
(-> zloc z/next* skip-whitespace comment?))
147150

148-
(defn- should-indent? [zloc]
149-
(and (line-break? zloc) (not (comment-next? zloc))))
151+
(defn- comment-next-other-than-line-comment? [zloc]
152+
(when-let [znext (-> zloc z/next* skip-whitespace)]
153+
(and (comment? znext) (not (line-comment? znext)))))
154+
155+
(defn- should-indent? [zloc opts]
156+
(and (line-break? zloc)
157+
(if (:indent-line-comments? opts)
158+
(not (comment-next-other-than-line-comment? zloc))
159+
(not (comment-next? zloc)))))
150160

151-
(defn- should-unindent? [zloc]
152-
(and (indentation? zloc) (not (comment-next? zloc))))
161+
(defn- should-unindent? [zloc opts]
162+
(and (indentation? zloc)
163+
(if (:indent-line-comments? opts)
164+
(not (comment-next-other-than-line-comment? zloc))
165+
(not (comment-next? zloc)))))
153166

154-
(defn unindent [form]
155-
(transform form edit-all should-unindent? z/remove*))
167+
(defn unindent
168+
([form]
169+
(unindent form {}))
170+
([form opts]
171+
(transform form edit-all #(should-unindent? % opts) z/remove*)))
156172

157173
(def ^:private start-element
158174
{:meta "^", :meta* "#^", :vector "[", :map "{"
@@ -338,6 +354,7 @@
338354

339355
(def default-options
340356
{:indentation? true
357+
:indent-line-comments? false
341358
:insert-missing-whitespace? true
342359
:remove-consecutive-blank-lines? true
343360
:remove-multiple-non-indenting-spaces? false
@@ -416,7 +433,7 @@
416433
context (merge (select-keys opts [:function-arguments-indentation])
417434
{:alias-map alias-map
418435
:ns-name ns-name})]
419-
(transform form edit-all should-indent?
436+
(transform form edit-all #(should-indent? % opts)
420437
#(indent-line % sorted-indents context)))))
421438

422439
(defn- map-key? [zloc]
@@ -450,7 +467,7 @@
450467
([form indents alias-map]
451468
(indent (unindent form) indents alias-map))
452469
([form indents alias-map opts]
453-
(indent (unindent form) indents alias-map opts)))
470+
(indent (unindent form opts) indents alias-map opts)))
454471

455472
(defn final? [zloc]
456473
(and (nil? (z/right* zloc)) (root? (z/up* zloc))))

cljfmt/src/cljfmt/main.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
[nil "--[no-]indentation"
3333
:default (:indentation? defaults)
3434
:id :indentation?]
35+
[nil "--[no-]indent-line-comments"
36+
:default (:indent-line-comments? defaults)
37+
:id :indent-line-comments?]
3538
[nil "--[no-]remove-multiple-non-indenting-spaces"
3639
:default (:remove-multiple-non-indenting-spaces? defaults)
3740
:id :remove-multiple-non-indenting-spaces?]

cljfmt/test/cljfmt/core_test.cljc

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,3 +1848,156 @@
18481848

18491849
(deftest test-clojure-12-syntax
18501850
(is (reformats-to? ["^Long/1 a"] ["^Long/1 a"])))
1851+
1852+
(deftest test-indenting-comments
1853+
(testing "whole-line comments"
1854+
(testing "whole-line flush-left comments"
1855+
(testing "whole-line margin flush-left ; comments"
1856+
(is (reformats-to?
1857+
["(when 42"
1858+
"; leave me alone"
1859+
")"]
1860+
["(when 42"
1861+
"; leave me alone"
1862+
" )"]
1863+
{:indent-line-comments? true}))
1864+
(is (reformats-to?
1865+
["(when 42"
1866+
";"
1867+
")"]
1868+
["(when 42"
1869+
";"
1870+
" )"]
1871+
{:indent-line-comments? true})))
1872+
(testing "whole-line line flush-left ;; comments"
1873+
(is (reformats-to?
1874+
["(when 42"
1875+
";; answer"
1876+
")"]
1877+
["(when 42"
1878+
" ;; answer"
1879+
" )"]
1880+
{:indent-line-comments? true}))
1881+
(is (reformats-to?
1882+
["(when 42"
1883+
";;"
1884+
")"]
1885+
["(when 42"
1886+
" ;;"
1887+
" )"]
1888+
{:indent-line-comments? true})))
1889+
(testing "whole-line heading flush-left ;;; comments"
1890+
(is (reformats-to?
1891+
["(when 42"
1892+
";;; heading"
1893+
")"]
1894+
["(when 42"
1895+
";;; heading"
1896+
" )"]
1897+
{:indent-line-comments? true}))
1898+
(is (reformats-to?
1899+
["(when 42"
1900+
";;;"
1901+
")"]
1902+
["(when 42"
1903+
";;;"
1904+
" )"]
1905+
{:indent-line-comments? true}))))
1906+
(testing "whole-line pre-indented comments"
1907+
(testing "whole-line margin pre-indented ; comments"
1908+
(is (reformats-to?
1909+
["(when 42"
1910+
" ; leave me alone"
1911+
")"]
1912+
["(when 42"
1913+
" ; leave me alone"
1914+
" )"]
1915+
{:indent-line-comments? true}))
1916+
(is (reformats-to?
1917+
["(when 42"
1918+
" ;"
1919+
")"]
1920+
["(when 42"
1921+
" ;"
1922+
" )"]
1923+
{:indent-line-comments? true})))
1924+
(testing "whole-line line pre-indented ;; comments"
1925+
(is (reformats-to?
1926+
["(when 42"
1927+
" ;; answer"
1928+
")"]
1929+
["(when 42"
1930+
" ;; answer"
1931+
" )"]
1932+
{:indent-line-comments? true}))
1933+
(is (reformats-to?
1934+
["(when 42"
1935+
" ;;"
1936+
")"]
1937+
["(when 42"
1938+
" ;;"
1939+
" )"]
1940+
{:indent-line-comments? true})))
1941+
(testing "whole-line heading pre-indented ;;; comments"
1942+
(is (reformats-to?
1943+
["(when 42"
1944+
" ;;; heading"
1945+
")"]
1946+
["(when 42"
1947+
" ;;; heading"
1948+
" )"]
1949+
{:indent-line-comments? true}))
1950+
(is (reformats-to?
1951+
["(when 42"
1952+
" ;;;"
1953+
")"]
1954+
["(when 42"
1955+
" ;;;"
1956+
" )"]
1957+
{:indent-line-comments? true})))))
1958+
(testing "after-code comments"
1959+
(testing "after-code margin ; comments"
1960+
(is (reformats-to?
1961+
["(when 42 ; leave me alone"
1962+
" :a"
1963+
")"]
1964+
["(when 42 ; leave me alone"
1965+
" :a)"]
1966+
{:indent-line-comments? true}))
1967+
(is (reformats-to?
1968+
["(when 42 ;"
1969+
" :a"
1970+
")"]
1971+
["(when 42 ;"
1972+
" :a)"]
1973+
{:indent-line-comments? true})))
1974+
(testing "after-code line ;; comments"
1975+
(is (reformats-to?
1976+
["(when 42 ;; leave me alone"
1977+
" :a"
1978+
")"]
1979+
["(when 42 ;; leave me alone"
1980+
" :a)"]
1981+
{:indent-line-comments? true}))
1982+
(is (reformats-to?
1983+
["(when 42 ;;"
1984+
" :a"
1985+
")"]
1986+
["(when 42 ;;"
1987+
" :a)"]
1988+
{:indent-line-comments? true})))
1989+
(testing "after-code heading ;;; comments"
1990+
(is (reformats-to?
1991+
["(when 42 ;;; leave me alone"
1992+
" :a"
1993+
")"]
1994+
["(when 42 ;;; leave me alone"
1995+
" :a)"]
1996+
{:indent-line-comments? true}))
1997+
(is (reformats-to?
1998+
["(when 42 ;;;"
1999+
" :a"
2000+
")"]
2001+
["(when 42 ;;;"
2002+
" :a)"]
2003+
{:indent-line-comments? true})))))

0 commit comments

Comments
 (0)