Fork me on GitHub
#emacs
<
2022-07-31
>
Drew Verlee05:07:16

Is there any chance that someone wants to pair on trying to add some cider functionality? I think it would be cool if there a way to select a key from a hashmap. e.g Let's say your cursor is * in this expression:

(-> {:user "..." :account-id "..." :age "..."} * ...)
after hitting some set of keys it would eval the last result (a hashmap) and then give you a list of those keys you could tap through to select one.
[:user :account-id :age] 
Just think, no longer would you have to type call the clojure.core/keys fn, then print the keywords, then select one and put it as next position as a function. πŸ™‚ . This comes up all the time for me, and while there are an arbitrary number of things you might want to do with a hashamp, this one seems common enough to warrant a special function imo. I mentioned this idea like a couple months back and it still feels useful to me after all that time so i'm willing to commit. The downside to this pairing arrangement, is that i know next to nothing about programming emacs. But i'm willing to learn with someone if anyone else is a newbie and wants to start with me. Or if someone is like really excited to teach, this seems like a good example. I'm sure we can come to some mutually agreeable arrangement.

Carlo07:07:50

I'm travelling right now, if you don't do this in a week DM me and we can arrange something ☺️

Drew Verlee14:07:21

@UA7E6DU04 I'll do that, thanks!

Drew Verlee18:07:43

I submitted a cider feature request, hopefully this is the right place to start. https://github.com/clojure-emacs/cider/issues/3231

Drew Verlee01:08:36

This is what i have so far:

(defun cider-eval-sexp-up-to-point-with-clojure-keys (&optional  output-to-current-buffer)
  "Evaluate the current sexp form up to point.
If invoked with OUTPUT-TO-CURRENT-BUFFER, print the result in the current
buffer.  It constructs an expression to eval in the following manner:

- It finds the code between the point and the start of the sexp expression;
- It balances this bit of code by closing the expression;
- It evaluates the resulting code using `cider-interactive-eval'."
  (interactive "P")
  (let* ((beg-of-sexp (save-excursion (up-list) (backward-list) (point)))
         (beg-delimiter (save-excursion (up-list) (backward-list) (char-after)))
         (beg-set?  (save-excursion (up-list) (backward-list) (char-before)))
         (code (buffer-substring-no-properties beg-of-sexp (point)))
         (code (if (= beg-set? ?#) (concat (list beg-set?) code) code))
         (keys-code (concat code "keys"))
         (keys-code (concat keys-code (list (cider--matching-delimiter beg-delimiter))))
         (code (concat code (list (cider--matching-delimiter beg-delimiter))))
         )
    (cider-interactive-eval keys-code
                            (let* ((result (cider-eval-print-handler))) <--- cider-eval-print-handlder isn't correct
                              (print result)
                             ;; (completing-read "Choose:" result)
                              )
                            (list beg-of-sexp (point))
                            (cider--nrepl-pr-request-map))

    ;; (cider-interactive-eval code
    ;;                         (when output-to-current-buffer
    ;;                           (cider-eval-print-handler))
    ;;                         (list beg-of-sexp (point))
    ;;                         (cider--nrepl-pr-request-map))

    ))

Benjamin09:08:24

(defun cider-insert-select-key-dwim ()
  "Insert a key selection from the sexp before point."
  (interactive)
  (let* ((form (cider-last-sexp))
	 (ks (cider-nrepl-sync-request:eval
	      (format "(keys %s)" form)))
	 (ks (car
	      (read-from-string
	       (nrepl-dict-get ks "value"))))
	 (ks
	  (completing-read-multiple "Key: " ks)))
    (insert
     (pcase ks
       (`(,k) k)
       (_ (format
	   "(select-keys [%s])"
	   (mapconcat
	    #'identity
	    (mapcar (lambda (k) (with-output-to-string (princ k))) myks)
	    " ")))))))

πŸ‘€ 1
Benjamin09:08:34

I am thinking the next elaboration of this auto inserts (map :key) , if the thing before point is a seq

Drew Verlee02:08:36

Thanks, I'll try to devote time to this on the weekend.