Skip to content

Commit 856f255

Browse files
committed
Don't remove spacing before comments (fixes #242)
1 parent b6a5bb8 commit 856f255

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

cljfmt/src/cljfmt/core.cljc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,17 @@
5555
(and (deref? zloc)
5656
(unquote? (z/up* zloc))))
5757

58+
(defn- comment? [zloc]
59+
(some-> zloc z/node n/comment?))
60+
5861
(defn- surrounding-whitespace? [zloc]
5962
(and (not (top? zloc))
6063
(clojure-whitespace? zloc)
6164
(or (and (nil? (z/left* zloc))
6265
;; don't convert ~ @ to ~@
63-
(not (unquote-deref? (z/right* zloc))))
66+
(not (unquote-deref? (z/right* zloc)))
67+
;; ignore space before comments
68+
(not (comment? (z/right* zloc))))
6469
(nil? (z/skip z/right* clojure-whitespace? zloc)))))
6570

6671
(defn remove-surrounding-whitespace [form]
@@ -87,9 +92,6 @@
8792
(defn- space? [zloc]
8893
(= (z/tag zloc) :whitespace))
8994

90-
(defn- comment? [zloc]
91-
(some-> zloc z/node n/comment?))
92-
9395
(defn- line-comment? [zloc]
9496
(and (comment? zloc) (re-matches #"(?s);;([^;].*)?" (z/string zloc))))
9597

cljfmt/test/cljfmt/core_test.cljc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,15 @@
941941
"))"]
942942
["(let [x 3"
943943
" y 4]"
944-
" (+ (* x x) (* y y)))"]))))
944+
" (+ (* x x) (* y y)))"])))
945+
946+
(testing "spacing before comments left alone"
947+
(is (reformats-to?
948+
["[ ; comment"
949+
"1 2 3"
950+
"]"]
951+
["[ ; comment"
952+
" 1 2 3]"]))))
945953

946954
(deftest test-missing-whitespace
947955
(testing "unglue inner"

0 commit comments

Comments
 (0)