-
-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Eval into register, kill last eval result #3162
Conversation
To elaborate further on the printing issue - say the user has set in Clojure (set! *print-length* 10)
;; such that
(range 50) ;; => (0 1 2 3 4 5 6 7 8 9 ...) The obvious approach of wrapping the form does not work, I'm not sure why. (binding [*print-length* nil]
(range 50)) Using nrepl print options in additional-params also encounters a problem - the (cider-interactive-eval "(range 100)"
nil nil
'(("nrepl.middleware.print/print" "cider.nrepl.pprint/pr")
("nrepl.middleware.print/options" (dict "print-length" nil))))
;; Error: class clojure.lang.PersistentVector cannot be cast to class java.lang.Number (clojure.lang.PersistentVector is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')" Probably this is some encoding quirk / the fact that |
I think that's fine.
Fine by me.
Yeah, that's the problem. There's no way to send |
It's good timing for cutting a CIDER release, so you might want to get this PR into that train :) |
@@ -651,7 +660,9 @@ The handler simply inserts the result value in BUFFER." | |||
(nrepl-make-response-handler (or buffer eval-buffer) | |||
(lambda (_buffer value) | |||
(with-current-buffer buffer | |||
(insert value))) | |||
(insert value)) | |||
(when cider-eval-register |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this break for bigger results that are streamed in several chunks? Seems to you your register will only retain the last chunk.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late response - I spent a while trying to reproduce and understand this chunking behaviour.
It seems that by default CIDER only enables the stream chunking in certain pretty printing commands, and the handlers for other commands operate on the assumption that the result is not chunked.
See cider-eval-defun-to-comment
->
cider-eval-print-with-comment-handler
(does not handle chunks)
with option map (cider--nrepl-pr-request-map)
(disables streaming)
whereas cider-pprint-eval-defun-to-comment
->
cider-eval-pprint-with-multiline-comment-handler
(accumulates chunks)
with option map (cider--nrepl-print-request-map fill-column)
(enables streaming)
But one could conceivably call one with the other, with broken results:
(cider-interactive-eval "(range 50)"
(cider-eval-print-with-comment-handler
(current-buffer) (point-max-marker) cider-comment-prefix)
nil
'(("nrepl.middleware.print/stream?" "1")
("nrepl.middleware.print/buffer-size" 20)))
;; => )
;; => 43 44 45 46 47 48 49
;; => 6 37 38 39 40 41 42
;; => 30 31 32 33 34 35 3
;; => 23 24 25 26 27 28 29
;; => 6 17 18 19 20 21 22
;; => 10 11 12 13 14 15 1
;; => (0 1 2 3 4 5 6 7 8 9
Is this a bug in the current code or a valid assumption that the handlers are making? It looks like cider-interactive-eval-handler
is currently only called in conjunction with pr-request-map
, all other commands which use pretty printing override the handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's what currently happens when the interactive-eval-handler is called with streaming enabled:
(defun cider-eval-defun-with-streaming ()
"A hypothetical eval command"
(interactive)
(cider-interactive-eval
(cider-defun-at-point)
nil ; <- the default interactive-eval-handler
(cider-defun-at-point 'bounds)
'(("nrepl.middleware.print/stream?" "1") ; <- but with streaming
("nrepl.middleware.print/buffer-size" 20) ; <- and a small chunk size
("nrepl.middleware.print/print" "cider.nrepl.pprint/pr"))))
Is this the intended behaviour? Obviously without the artificial delay, the user would only see the very last chunk in their overlay / minibuffer.
Accumulating the result and redisplaying the overlay on each chunk seems like a better solution:
But in the first place, I believe none of the code paths in the current set of commands will lead to such a situation, unless some custom command like the above is defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you make a good point. Streaming of values wasn't enabled intentionally for interactive evaluation as I couldn't figure out a good way to print the result incrementally. I like your idea.
In an ideal world probably we should able to enable/disable streaming everywhere via defcustoms. For huge results definitely streaming results in a much nicer experience as you start seeing results faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the intended behaviour? Obviously without the artificial delay, the user would only see the very last chunk in their overlay / minibuffer.
And yeah - that's definitely not the intended behavior. :-) As mentioned about the streaming was disabled until we figure out how to best do it for buffer evaluations. Back then I recall I was planning to research potential options and as it usually happens - I never did. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to insert this eval-into-register behaviour into every handler, or just the interactive-eval-handler?
Here's the full list:
- cider-insert-eval-handler
- cider-interactive-eval-handler
- cider-load-file-handler
- cider-eval-print-handler
- cider-eval-print-with-comment-handler
- cider-eval-pprint-with-multiline-comment-handler
- cider-popup-eval-handler
Of these, I think it could be left out of the print-handler
and popup-eval-handler
, since these would be used for potentially large outputs. Accumulating a huge result in memory in addition to printing it out in chunks into an Emacs buffer could be a performance concern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I added a commit which refactors the handlers to handle streamed values. Not too happy about the code duplication but there doesn't seem to be an clean way to abstract out all the (when cider-eval-register ...
expressions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems I forgot the respond here. The duplication is definitely not great, but I guess we can live with it, as the handler code rarely changes. I think that the ieval-handler and the load-file handler are definitely the most important ones when you might want to save the result to a register, but I guess for consistency it makes sense to extend this to more handlers as suggested by you. And yeah - we should avoid storing huge results in memory, knowing how poorly GC in Emacs performs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I removed it from the print-handler and popup-eval-handler, on the basis that the results are directly output to a buffer anyway, where they can be interacted with.
Yeah, that'd be nice, but it's also not that big of a deal if it misses it. |
That's unfortunate, I was hoping there was some way of encoding a nil that I couldn't find in the bencode(?) docs - still not very familiar with the internals of nrepl communications. In this particular case maybe I do wonder why wrapping the the form in |
Or 0 or some keyword like
Probably something on the backend overrides |
@yuhan0 Do you plan to work more or this or we can merge it? Seems to me that despite its small flaws the functionality would be useful in its present form. |
Yes, thanks for the reminder! Haven't had much time to look into those issues above, but it should be alright to merge in its current form. I'll just add a changelog entry and couple of notes to the manual. |
Sounds good to me. |
It currently treats the 4-element list as a 2-element pair and ignores the intended setting of stream?, but this does not affect functionality since its default value is nil.
Results are already output to a buffer where they can be easily copied. Avoids storing huge results in memory and triggering the GC
a684d1c
to
cea9156
Compare
Should be good to go! There are some lint and test faliures which I don't think are related to this PR. |
A quick naive implementation of the features as discussed in #3143.
I think there are a few complications around
*print-level*
/*print-length*
options and print streaming.One common use case I can see for this feature is being able to copy some large eval output into a separate buffer / tool and analyse it further - eg. large strings, which Cider's builtin inspector does not handle very well. Probably the most user-friendly thing to do in that case is temporarily override the print length and level options to nil, allowing for the entire result to be copied without truncation.
But this will slow down the use of regular eval commands, where it also makes sense to just copy verbatim what the user sees in the overlay / minibuffer message into the eval register.
Maybe only override print options in the case of an explicit
cider-kill-last-result
command? Or have it be toggled by universal-argument?Note: I had tried to implement a similar
cider-inspector-copy-val
command in the past, but found the need to accumulate large results in chunks due to nrepl's print streaming and gave up halfway.cider-print-quota
also comes into this, and whether to use pretty printing.Defaults:
Should eval-register be enabled by default and clobber users'
?e
registers? Presumably a register power user would find it easy enough to disable.Suggested key binding for kill-last-result:
C-c C-v k
?No tests written yet (due to issues running the testing framework)
Before submitting the PR make sure the following things have been done (and denote this
by checking the relevant checkboxes):
eldev test
)eldev lint
) which is based onelisp-lint
and includescheckdoc
, check-declare, packaging metadata, indentation, and trailing whitespace checks.Thanks!
If you're just starting out to hack on CIDER you might find this section of its
manual extremely useful.