Fork me on GitHub
#portal
<
2022-03-22
>
Carlo10:03:34

It occurred to me that I probably can already set custom metadata programmatically. In my emacs config, I do:

(after! cider-mode
  (defun cider-tap (&rest r)
    (cons (concat "(let [__value "
                  (caar r)
                  "] (tap> __value) __value)")
          (cdar r)))

  (advice-add 'cider-nrepl-request:eval
              :filter-args #'cider-tap))
and when I (tap> __value), I could just calculate some function of that value to get the metadata I desire, and eval that instead

Carlo10:03:27

to do a concrete simple example, I can use pprint as the default visualization via:

Carlo10:03:37

(after! cider-mode
  (defun cider-tap (&rest r)
    (cons (concat "(let [__value "
                  (caar r)
                  "] (tap> (with-meta __value {:portal.viewer/default :portal.viewer/pprint}))
                     __value)")
          (cdar r)))

  (advice-add 'cider-nrepl-request:eval
              :filter-args #'cider-tap))

1
djblue16:03:41

I think this will cause issues when __value doesn't support metadata

Carlo17:03:33

what would be a case in which that happens?

djblue17:03:38

Any time you are dealing directly with numbers/dates/keywords/objects

Carlo17:03:45

it seems that the with-meta call also makes nrepl die at startup, so it's not really a possibility

Carlo17:03:24

This works though, that was due to nil:

(after! cider-mode
  (defun cider-tap (&rest r)
    (cons (concat "(let [__value "
                  (caar r)
                  "] (tap> (if (some? __value)
                             (with-meta __value {:portal.viewer/default :portal.viewer/pprint})
                             __value))
                     __value)")
          (cdar r)))

  (advice-add 'cider-nrepl-request:eval
              :filter-args #'cider-tap))

Carlo18:03:21

but then if I evaluate a number it fails, hmmm

Carlo18:03:38

This is a better solution:

(after! cider-mode
  (defun cider-tap (&rest r)
    (cons (concat "(let [__value "
                  (caar r)
                  "] (tap> (if (instance? clojure.lang.IObj __value)
                             (with-meta __value {:portal.viewer/default :portal.viewer/pprint})
                             __value))
                     __value)")
          (cdar r)))

  (advice-add 'cider-nrepl-request:eval
              :filter-args #'cider-tap))

👍 1