Skip to content

Commit

Permalink
Alternative metadata formatting, Issue #224
Browse files Browse the repository at this point in the history
  • Loading branch information
kkinnear committed Mar 29, 2022
1 parent 3cd7f0f commit ea9a69d
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 2 deletions.
10 changes: 9 additions & 1 deletion doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,8 @@ later arguments is controlled by `:list {:indent n}`
(more-process tag foo bar))
```

Note: This is implemented as a "body" function, as if it were `:arg2-body`.

#### :arg2-pair

Just like :arg2, but prints the third through last arguments as pairs.
Expand All @@ -1062,6 +1064,9 @@ or not hanging well, the flow indent is controlled by `:pair {:indent n}`.
:bar "bar"
"baz")
```
Note: This is implemented as a "body" function, as if it were `:arg2-pair-body`.


#### :arg2-fn

Just like :arg2, but prints the third through last arguments as functions.
Expand All @@ -1072,6 +1077,9 @@ Just like :arg2, but prints the third through last arguments as functions.
(foo [bar] baz))
```

Note: This is implemented as a "body" function, as if it were `:arg2-fn-body`.


#### :binding _(function type)_

The function has a binding clause as its first argument.
Expand Down Expand Up @@ -4262,7 +4270,7 @@ the symbol to which the metadata is attached. If you configure
each other, and they will be handled separately (just like they
appear to be in the code).

Thus, for a `def` with metadata in a map, the `:meta` is configured
Thus, for a `def` with metadata in a map, if the `:meta` is configured
as `:split?`, then the map for the metadata will appear in the
`:arg1-body` position, and the symbol will appear flowed below it.
The same holds true for a keyword for the metadata.
Expand Down
21 changes: 20 additions & 1 deletion src/zprint/config.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@
;;
;; :arg2
;;
;; NOTE: This is implemented as a body function, i.e., like it was
;; :arg2-body.
;;
;; Print the first argument on the same line as the function name, and
;; if it fits then print the second argument on the same line as the function
;; name if it fits.
Expand Down Expand Up @@ -815,6 +818,22 @@
:map {:indent 0, :nl-separator? true}},
:map-nl-all {:doc "Add newline between all map pairs",
:map {::indent 0, :nl-separator-all? true}},

:meta-base {:doc "Alternative format for metadata. Experimental."
:list {:option-fn
(fn ([] "meta-base-option-fn")
([opts n exprs]
(when (meta (second exprs))
{:meta {:split? true}
:list {:hang-expand 0}
:fn-style :arg2
:next-inner-restore [[:list :option-fn]
[:list :hang-expand]]})))}}

:meta-alt {:doc "Alternative for metadata. Experimental."
:fn-map {"def" [:arg2 {:style :meta-base}]
"deftest" [:arg2 {:style :meta-base}]}}

:moustache {:doc "Format moustache elements nicely",
:fn-map {"app" [:flow {:style :vector-pairs}]}},
:vector-pairs {:doc "Consider vectors 'constants' for constant pairing",
Expand Down Expand Up @@ -888,7 +907,7 @@
{:vector {:fn-format :none},
:vector-fn {:constant-pair-min
1}}))
([] "require-pair"))}}]}},
([] "require-pair-option-fn"))}}]}},
:respect-bl {:doc "Enable respect blank lines for every type",
:list {:respect-bl? true},
:map {:respect-bl? true},
Expand Down
167 changes: 167 additions & 0 deletions test/zprint/zprint_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6623,6 +6623,173 @@ ser/collect-vars-acc %1 %2) )))"
"(letfn [(first-fn [arg1 arg2]\n (this (doing-stuff)\n (and-more-stuff)))\n (second-fn [arg1 arg2]\n (test (doing-stuff)\n (and-more-stuff)))]\n (other-stuff))\n"
{:parse-string? true}))

;;
;; Alternative way to format metadata. Also, :meta :eplit? true tests.
;;
;; Issue #224
;;

(expect
"(def\n ^{:doc\n \"Json serde. This enables our default camel-case out kebab case in behaviour.\"}\n default-json-serde\n serde/json-camel-kebab-serde)"
(zprint-str
"(def ^{:doc \"Json serde. This enables our default camel-case out kebab case in behaviour.\"}\n default-json-serde\n serde/json-camel-kebab-serde)\n"
{:parse-string? true :style :meta-alt}))

(expect
"(deftest ^{:database true, ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest ^{:database true ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")]\n foo))\n"
{:parse-string? true :style :meta-alt}))

(expect
"(deftest websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest \n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")]\n foo))\n"
{:parse-string? true :style :meta-alt}))

(expect
"(deftest ^{:database true} websocket-diagnostic-and-a-bit-more\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest ^{:database true} websocket-diagnostic-and-a-bit-more\n (let [foo (bar \"1\")]\n foo))\n"
{:parse-string? true :style :meta-alt}))

(expect
"(deftest ^:database websocket-diagnostic-and-a-bit-more\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest ^:database websocket-diagnostic-and-a-bit-more\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style :meta-alt}))

(expect
"(deftest ^:database-stuff-and-bother\n websocket-diagnostic-and-a-bit-more-that-does-not-fit\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest ^:database-stuff-and-bother websocket-diagnostic-and-a-bit-more-that-does-not-fit\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style :meta-alt}))

(expect
"(def ^:private foo \"bar\")"
(zprint-str
"(def ^:private foo \"bar\")\n"
{:parse-string? true :style :meta-alt}))

(expect
"(deftest\n ^{:database true,\n ::test.hooks/system-init-keys system-keys,\n :another-map-key :another-map-value}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest\n ^{:database true,\n ::test.hooks/system-init-keys system-keys,\n :another-map-key :another-map-value}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style :meta-alt}))

(expect
"(deftest\n ^{:database true,\n ::test.hooks/system-init-keys system-keys,\n :another-map-key :another-map-value}\n websocket-diagnostic-report-measurements-updated-event\n (foo))"
(zprint-str
"(deftest\n ^{:database true,\n ::test.hooks/system-init-keys system-keys,\n :another-map-key :another-map-value}\n websocket-diagnostic-report-measurements-updated-event (foo))\n"
{:parse-string? true :style :meta-alt}))

(expect
"(deftest ^{:database true, ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest\n ^{:database true, ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style :meta-alt}))

(expect
"(deftest\n ^{:database-a-bit-longer :does-not-fit,\n ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest\n ^{:database-a-bit-longer :does-not-fit, ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style :meta-alt}))

;;
;; With :style :community
;;

; i224

(expect
"(def\n ^{:doc\n \"Json serde. This enables our default camel-case out kebab case in behaviour.\"}\n default-json-serde\n serde/json-camel-kebab-serde)"
(zprint-str
"(def ^{:doc \"Json serde. This enables our default camel-case out kebab case in behaviour.\"}\n default-json-serde\n serde/json-camel-kebab-serde)\n"
{:parse-string? true :style [:meta-alt :community]}))

; i224a

(expect
"(deftest ^{:database true, ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest ^{:database true ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")]\n foo))\n"
{:parse-string? true :style [:meta-alt :community]}))

; i224b
;
; This does odd things, and hasn't any meta data anyway

#_(expect
"(deftest websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest \n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")]\n foo))\n"
{:parse-string? true :style [:community :meta-alt]}))

; i224c

(expect
"(deftest ^{:database true} websocket-diagnostic-and-a-bit-more\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest ^{:database true} websocket-diagnostic-and-a-bit-more\n (let [foo (bar \"1\")]\n foo))\n"
{:parse-string? true :style [:community :meta-alt]}))

; i224d

(expect
"(deftest ^:database websocket-diagnostic-and-a-bit-more\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest ^:database websocket-diagnostic-and-a-bit-more\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style [:community :meta-alt]}))

; i224e

(expect
"(deftest ^:database-stuff-and-bother\n websocket-diagnostic-and-a-bit-more-that-does-not-fit\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest ^:database-stuff-and-bother websocket-diagnostic-and-a-bit-more-that-does-not-fit\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style [:meta-alt :community]}))

; i224f

(expect
"(def ^:private foo \"bar\")"
(zprint-str
"(def ^:private foo \"bar\")\n"
{:parse-string? true :style [:meta-alt :community]}))

; i224g

(expect
"(deftest\n ^{:database true,\n ::test.hooks/system-init-keys system-keys,\n :another-map-key :another-map-value}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest\n ^{:database true,\n ::test.hooks/system-init-keys system-keys,\n :another-map-key :another-map-value}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style [:community :meta-alt]}))

; i224h

(expect
"(deftest\n ^{:database true,\n ::test.hooks/system-init-keys system-keys,\n :another-map-key :another-map-value}\n websocket-diagnostic-report-measurements-updated-event\n (foo))"
(zprint-str
"(deftest\n ^{:database true,\n ::test.hooks/system-init-keys system-keys,\n :another-map-key :another-map-value}\n websocket-diagnostic-report-measurements-updated-event (foo))\n"
{:parse-string? true :style [:community :meta-alt]}))

; i224i

(expect
"(deftest ^{:database true, ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest\n ^{:database true, ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style [:meta-alt :community]}))

; i224j

(expect
"(deftest\n ^{:database-a-bit-longer :does-not-fit,\n ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))"
(zprint-str
"(deftest\n ^{:database-a-bit-longer :does-not-fit, ::test.hooks/system-init-keys system-keys}\n websocket-diagnostic-report-measurements-updated-event\n (let [foo (bar \"1\")] foo))\n"
{:parse-string? true :style [:community :meta-alt]}))





Expand Down

0 comments on commit ea9a69d

Please sign in to comment.