-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #30
- Loading branch information
Showing
7 changed files
with
241 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(ns cljstyle.format.comment | ||
(:require | ||
[cljstyle.format.zloc :as zl] | ||
[clojure.string :as str] | ||
[rewrite-clj.node :as n] | ||
[rewrite-clj.zip :as z])) | ||
|
||
|
||
(defn- comment-form? | ||
"True if the node at this location is a comment." | ||
[zloc _] | ||
(zl/comment? zloc)) | ||
|
||
|
||
(defn- leading? | ||
"True if the node at this location is a 'leading' comment. This generally | ||
means it's the first non-whitespace thing on the line. | ||
" | ||
[zloc] | ||
(let [prior (z/skip z/left* zl/space? (z/prev* zloc))] | ||
(or (zl/root? prior) | ||
(zl/comment? prior) | ||
(z/linebreak? prior) | ||
;; Comments which are the first thing in a data structure are also | ||
;; considered leading comments, as they are generally meant to align | ||
;; with the subesquent comments or elements in the structure. | ||
(and (nil? (z/left* zloc)) | ||
(contains? #{:map :vector :list :set} | ||
(z/tag (z/up zloc))))))) | ||
|
||
|
||
(defn- edit-comment | ||
[zloc rule-config] | ||
(if-let [prefix (if (leading? zloc) | ||
(:leading-prefix rule-config) | ||
(:inline-prefix rule-config))] | ||
;; Check comment prefix. | ||
(let [curr-comment (zl/zstr zloc)] | ||
(if (str/starts-with? curr-comment (str ";" prefix)) | ||
;; Prefix is already correct. | ||
zloc | ||
;; Fix comment. | ||
(z/replace zloc | ||
(-> curr-comment | ||
(subs 1) | ||
(str/replace #"^;* ?" prefix) | ||
(str/replace #" *\n$" "\n") | ||
(n/comment-node))))) | ||
;; Configured to no-op. | ||
zloc)) | ||
|
||
|
||
(def format-comments | ||
"Rule to fix comment formatting." | ||
[:comments nil comment-form? edit-comment]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
(ns cljstyle.format.comment-test | ||
(:require | ||
[cljstyle.format.comment :as comment] | ||
[cljstyle.test-util] | ||
[clojure.test :refer [deftest testing is]])) | ||
|
||
|
||
(def default-config | ||
{:enabled? true | ||
:inline-prefix " " | ||
:leading-prefix "; "}) | ||
|
||
|
||
(deftest top-level | ||
(testing "correct prefix" | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
";; standalone comment" | ||
";; standalone comment")) | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
";; comment line one\n;; comment line two\n" | ||
";; comment line one\n;; comment line two\n")) | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"abc\n;; standalone comment" | ||
"abc\n;; standalone comment")) | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"abc\n;; standalone comment\nxyz" | ||
"abc\n;; standalone comment\nxyz"))) | ||
(testing "incorrect prefix" | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
";bad comment without space" | ||
";; bad comment without space")) | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"; bad comment with space" | ||
";; bad comment with space"))) | ||
(testing "trailing whitespace" | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
";; one\n; \n;; three\n" | ||
";; one\n;;\n;; three\n")))) | ||
|
||
|
||
(deftest inline-comments | ||
(testing "correct prefix" | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"(+ 1 2) ; inline comment" | ||
"(+ 1 2) ; inline comment")) | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"[x ; comment about x\n y ; about y\n z]" | ||
"[x ; comment about x\n y ; about y\n z]"))) | ||
(testing "incorrect prefix" | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"(+ 1 2) ;; inline comment" | ||
"(+ 1 2) ; inline comment")) | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"[x ;; comment about x\n y ;about y\n z]" | ||
"[x ; comment about x\n y ; about y\n z]")))) | ||
|
||
|
||
(deftest leading-comments | ||
(testing "correct prefix" | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"(cond | ||
;; some exposition about case one | ||
(= 1 x) | ||
(do (one-thing)) | ||
;; some comment about the | ||
;; second case | ||
(two? x) | ||
(different-thing x) | ||
,,,)" | ||
"(cond | ||
;; some exposition about case one | ||
(= 1 x) | ||
(do (one-thing)) | ||
;; some comment about the | ||
;; second case | ||
(two? x) | ||
(different-thing x) | ||
,,,)"))) | ||
(testing "incorrect prefix" | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"(cond | ||
; some exposition about case one | ||
(= 1 x) | ||
(do (one-thing)) | ||
;some comment about the | ||
;;second case | ||
(two? x) | ||
(different-thing x) | ||
,,,)" | ||
"(cond | ||
;; some exposition about case one | ||
(= 1 x) | ||
(do (one-thing)) | ||
;; some comment about the | ||
;; second case | ||
(two? x) | ||
(different-thing x) | ||
,,,)")))) | ||
|
||
|
||
(deftest special-cases | ||
(testing "in structure literals" | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"(let [;an inline comment | ||
; which is really the start of many | ||
x 123] | ||
(* 2 x))" | ||
"(let [;; an inline comment | ||
;; which is really the start of many | ||
x 123] | ||
(* 2 x))")) | ||
(is (rule-reformatted? | ||
comment/format-comments default-config | ||
"{;; multiline comment | ||
; in a map | ||
:x a}" | ||
"{;; multiline comment | ||
;; in a map | ||
:x a}")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters