Skip to content

Commit

Permalink
Support reader conditionals
Browse files Browse the repository at this point in the history
The code was really written by @darwindarak but never merged:

clj-commons#175
  • Loading branch information
tsmacdonald committed Oct 12, 2023
1 parent b7e7bce commit a5f8674
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/marginalia/parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@
(let [start (.getLineNumber reader)
form (binding [*comments* sub-level-comments]
(try (. clojure.lang.LispReader
(read reader false :_eof false))
(read reader {:read-cond :allow
:eof :_eof}))
(catch Exception ex
(let [msg (str "Problem parsing near line " start
" <" (.readLine reader) ">"
Expand Down
30 changes: 21 additions & 9 deletions test/marginalia/test/parse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@
(is (= :code type))
(is (= "the docstring" docstring))))

(def reader-conditional-fn
"(defn error
\"Returns a language-appropriate error\"
[^String msg]
#?(:clj (Exception. msg)
:cljs (js/Error. msg)))")

(deftest test-reader-conditional
(let [{:keys [type docstring]} (first (marginalia.parser/parse reader-conditional-fn))]
(is (= :code type))
(is (= "Returns a language-appropriate error" docstring))))

(deftest inline-comments
(testing "inline comments ignored by default"
(binding [p/*comments-enabled* (atom true)]
(let [result (p/parse
"(ns test)
"(ns test)
(defn foo
\"docstring\"
Expand All @@ -44,7 +56,7 @@
(binding [p/*comments-enabled* (atom true)]
;; tests that prelude is appended to docstring
(let [result (p/parse
"(ns test)
"(ns test)
;; A
(defn foo
Expand All @@ -61,7 +73,7 @@
(binding [p/*comments-enabled* (atom true)
p/*lift-inline-comments* true]
(let [result (p/parse
"(ns test)
"(ns test)
(defn foo
\"docstring\"
Expand All @@ -77,7 +89,7 @@
(binding [p/*comments-enabled* (atom true)
p/*lift-inline-comments* true]
(let [result (p/parse
"(ns test)
"(ns test)
(defn foo
\"docstring\"
Expand All @@ -93,7 +105,7 @@
p/*lift-inline-comments* true]
;; A and B should be separate paragraphs
(let [result (p/parse
"(ns test)
"(ns test)
(defn foo
\"docstring\"
Expand All @@ -112,7 +124,7 @@
p/*lift-inline-comments* true]
;; prelude A follows docstring, then B and C as separate paragraphs
(let [result (p/parse
"(ns test)
"(ns test)
;; A
(defn foo
Expand All @@ -132,7 +144,7 @@
p/*lift-inline-comments* true]
;; this checks that consecutive comment lines stay in the same paragraph
(let [result (p/parse
"(ns test)
"(ns test)
;; A
(defn foo
Expand All @@ -154,7 +166,7 @@
;; this checks that a comment above the function doesn't merge in
;; when separated by a blank line
(let [result (p/parse
"(ns test)
"(ns test)
;; A
Expand All @@ -178,7 +190,7 @@
;; this checks that a comment above the function does merge in
;; when a blank comment joins it to the function
(let [result (p/parse
"(ns test)
"(ns test)
;; A
;;
Expand Down

0 comments on commit a5f8674

Please sign in to comment.