kkinnear / zprint

Executables, uberjar, and library to beautifully format Clojure and Clojurescript source code and s-expressions.
MIT License
547 stars 46 forks source link

How to force-nl on -> only when exceeds the width #272

Closed yqrashawn closed 1 year ago

yqrashawn commented 1 year ago

Added below config to the :fn-map, don't know why it won't work.

  "->"              [:noarg1-body
                     {:list               {:constant-pair? false :force-nl? false}
                      :next-inner-restore [[:list :constant-pair?]]}]

code showing where it won't work https://github.com/status-im/status-mobile/pull/14128/commits/9ba1f28dbf14215a39833a34d346dd746d58ce3b#diff-acc2864028c846a967aae52fa52b23048f98d0b5cee98b2e118047f2dbb52171R94-R96

kkinnear commented 1 year ago

Good question. This is clearly a bug, though whether of code or documentation is a open question. It turns out that :noarg1-body is in the set :fn-force-nl, which causes it to never be formatted onto on line. Since -> is the only "user" of :noarg1-body, you could get what you want by simply removing :noarg1-body from :fn-force-nl, thus:

(czprint i272 {:parse-string? true :remove {:fn-force-nl #{:noarg1-body}}})
(defn pressable-hooks
  [props]
  (let [{background-color :bgColor,
         border-radius :borderRadius,
         border-color :borderColor,
         border-width :borderWidth,
         type :type,
         disabled :disabled,
         on-press :onPress,
         on-long-press :onLongPress,
         on-press-start :onPressStart,
         accessibility-label :accessibilityLabel,
         children :children,
         :or {border-radius 0, type "primary"}}
          (bean/bean props)
        long-press-ref (react/create-ref)
        state (animated/use-value (:undetermined gesture-handler/states))
        active (animated/eq state (:began gesture-handler/states))
        gesture-handler (animated/use-gesture {:state state})
        animation (react/use-memo
                    (fn []
                      (animated/with-timing-transition
                        active
                        {:duration (animated/cond* active time-in time-out),
                         :easing (:ease-in animated/easings)}))
                    [])
        {:keys [background foreground]}
          (react/use-memo (fn []
                            (type->animation {:type (keyword type),
                                              :animation animation}))
                          [type])
        handle-press (fn [] (when on-press (on-press)))
        long-gesture-handler
          (react/callback
            (fn [^js evt]
              (let [gesture-state (-> evt .-nativeEvent .-state)]
                (when (and on-press-start
                           (= gesture-state (:began gesture-handler/states)))
                  (on-press-start))
                (when (and on-long-press
                           (= gesture-state (:active gesture-handler/states)))
                  (on-long-press)
                  (animated/set-value state
                                      (:undetermined gesture-handler/states)))))
            [on-long-press on-press-start])]
    (animated/code!
      (fn []
        (when on-press
          (animated/cond* (animated/eq state (:end gesture-handler/states))
                          [(animated/set state
                                         (:undetermined gesture-handler/states))
                           (animated/call* [] handle-press)])))
      [on-press])
    (reagent/as-element
      [gesture-handler/long-press-gesture-handler
       {:enabled (boolean (and on-long-press (not disabled))),
        :on-handler-state-change long-gesture-handler,
        :min-duration-ms long-press-duration,
        :max-dist 22,
        :ref long-press-ref}
       [animated/view
        {:accessible true, :accessibility-label accessibility-label}
        [gesture-handler/tap-gesture-handler
         (merge
           gesture-handler
           {:shouldCancelWhenOutside true,
            :wait-for long-press-ref,
            :enabled (boolean (and (or on-press on-long-press on-press-start)
                                   (not disabled)))})
         [animated/view
          [animated/view
           {:style (merge absolute-fill
                          background
                          {:background-color background-color,
                           :border-radius border-radius,
                           :border-color border-color,
                           :border-width border-width})}]
          (into [animated/view {:style foreground}]
                (react/get-children children))]]]])))

I believe this will get you what you want. I'll fix the documentation of this function style to include the fact that it is included in :fn-force-nl. Thanks!

Thanks for asking!

yqrashawn commented 1 year ago

Thanks. It works now. I tried below before and it won't work. Seems the :remove option has a higher priority.

:fn-force-nl #{:noarg1 :force-nl-body :force-nl :flow
                  :arg1-force-nl :arg1-force-nl-body :flow-body
                  :arg2-force-nl-body :arg2-force-nl}
kkinnear commented 1 year ago

Yes, the above where you defined :fn-force-nl yourself doesn't remove things. Handling sets in the options map is complicated, and so what I ended up doing was allowing anything you specify to add to the set, but it doesn't redefine the entire set. You have to use :remove to get default things out of the set. Which may not be clear. Maybe this from the zprint reference might be a little clearer.