Skip to content

Commit

Permalink
Implement cider-pprint-eval-last-sexp-to-comment
Browse files Browse the repository at this point in the history
This function has similar functionality to the other pprint interactions;
however this variant inserts the result of the evaluation directly in the
current buffer.

The comment prefix used in the output is user-configurable, and for consistency
we also use same prefix in cider-eval-defun-to-comment.
  • Loading branch information
gonewest818 committed Dec 7, 2017
1 parent 946a9d4 commit 67493a5
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### New Features

* [#2082](https://github.com/clojure-emacs/cider/pull/2082), [cider-nrepl#440](https://github.com/clojure-emacs/cider-nrepl/pull/440): Add specialized stacktraces for clojure.spec assertions.
* [#2111](https://github.com/clojure-emacs/cider/pull/2111): Add `cider-pprint-eval-last-sexp-to-comment`

### Changes

Expand Down
120 changes: 100 additions & 20 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,24 @@ namespace-qualified function of zero arity."
"Face used to highlight compilation warnings in Clojure buffers."
:group 'cider)

(defcustom cider-pprint-comment-prefix ";; => "
"The prefix to insert before the first line of pprint output."
:type 'string
:group 'cider
:package-version '(cider . "0.16.0"))

(defcustom cider-pprint-comment-continued-prefix ";; "
"The prefix to use on the second and subsequent lines of pprint output."
:type 'string
:group 'cider
:package-version '(cider . "0.16.0"))

(defcustom cider-pprint-comment-postfix ""
"The postfix to be appended after the final line of pprint output."
:type 'string
:group 'cider
:package-version '(cider . "0.16.0"))

(defconst cider-clojure-artifact-id "org.clojure/clojure"
"Artifact identifier for Clojure.")

Expand Down Expand Up @@ -795,6 +813,30 @@ COMMENT-PREFIX is the comment prefix to use."
(cider-emit-interactive-eval-err-output err))
'()))

(defun cider-eval-pprint-with-multiline-comment-handler (buffer location comment-prefix continued-prefix comment-postfix)
"Make a handler for evaluating and inserting results in BUFFER.
The inserted text is pretty-printed and region will be commented.
LOCATION is the location at which to insert.
COMMENT-PREFIX is the comment prefix for the first line of output.
CONTINUED-PREFIX is the comment prefix to use for the remaining lines.
COMMENT-POSTFIX is the text to output after the last line."
(cl-flet ((multiline-comment-handler (buffer value)
(with-current-buffer buffer
(save-excursion
(goto-char location)
(let ((lines (split-string value "[\n]+" t)))
;; only the first line gets the normal comment-prefix
(insert (concat comment-prefix (pop lines)))
(dolist (elem lines)
(insert (concat "\n" continued-prefix elem)))
(unless (string= comment-postfix "")
(insert comment-postfix)))))))
(nrepl-make-response-handler buffer
'()
#'multiline-comment-handler
#'multiline-comment-handler
'())))

(defun cider-popup-eval-out-handler (&optional buffer)
"Make a handler for evaluating and printing stdout/stderr in popup BUFFER.
Expand Down Expand Up @@ -1132,12 +1174,12 @@ arguments and only proceed with evaluation if it returns nil."
(interactive "r")
(cider-interactive-eval nil nil (list start end)))

(defun cider-eval-last-sexp (&optional prefix)
(defun cider-eval-last-sexp (&optional output-to-current-buffer)
"Evaluate the expression preceding point.
If invoked with a PREFIX argument, print the result in the current buffer."
If invoked with OUTPUT-TO-CURRENT-BUFFER, print the result in the current buffer."
(interactive "P")
(cider-interactive-eval nil
(when prefix (cider-eval-print-handler))
(when output-to-current-buffer (cider-eval-print-handler))
(cider-last-sexp 'bounds)))

(defun cider-eval-last-sexp-and-replace ()
Expand All @@ -1150,26 +1192,62 @@ If invoked with a PREFIX argument, print the result in the current buffer."
(backward-kill-sexp)
(cider-interactive-eval last-sexp (cider-eval-print-handler))))

(defun cider-eval-sexp-at-point (&optional prefix)
(defun cider-eval-sexp-at-point (&optional output-to-current-buffer)
"Evaluate the expression around point.
If invoked with a PREFIX argument, print the result in the current buffer."
If invoked with OUTPUT-TO-CURRENT-BUFFER, output the result to current buffer."
(interactive "P")
(save-excursion
(goto-char (cadr (cider-sexp-at-point 'bounds)))
(cider-eval-last-sexp prefix)))
(cider-eval-last-sexp output-to-current-buffer)))

(defun cider-eval-defun-to-comment (loc)
"Evaluate the \"top-level\" form and insert result as comment at LOC.
(defun cider-eval-defun-to-comment (&optional insert-before)
"Evaluate the \"top-level\" form and insert result as comment.
With a prefix arg, LOC, insert before the form, otherwise afterwards."
The formatting of the comment is defined in `cider-pprint-comment-prefix`
which, by default, is \";; => \" and can be customized.
With the prefix arg INSERT-BEFORE, insert before the form, otherwise afterwards."
(interactive "P")
(let* ((bounds (cider-defun-at-point 'bounds))
(insertion-point (nth (if loc 0 1) bounds)))
(insertion-point (nth (if insert-before 0 1) bounds)))
(cider-interactive-eval nil
(cider-eval-print-with-comment-handler
(current-buffer) insertion-point ";; => ")
(current-buffer)
insertion-point
cider-pprint-comment-prefix)
bounds)))

(defun cider-pprint-eval-last-sexp-to-comment (&optional insert-before)
"Evaluate the last sexp and insert result as comment.
The formatting of the comment is controlled via three options:
`cider-pprint-comment-prefix` \";; => \"
`cider-pprint-comment-continued-prefix` \";; \"
`cider-pprint-comment-postfix` \"\"
so that with customization you can optionally wrap the output
in the reader macro \"#_( .. )\", or \"(comment ... )\", or any
other desired formatting.
With a prefix arg INSERT-BEFORE, insert before the form, otherwise afterwards."
(interactive "P")
(let* ((bounds (cider-last-sexp 'bounds))
(insertion-point (nth (if insert-before 0 1) bounds))
;; when insert-before, we need a newline after the output to
;; avoid commenting the first line of the form
(comment-postfix (concat cider-pprint-comment-postfix
(if insert-before "\n" ""))))
(cider-interactive-eval nil
(cider-eval-pprint-with-multiline-comment-handler
(current-buffer)
insertion-point
cider-pprint-comment-prefix
cider-pprint-comment-continued-prefix
comment-postfix)
bounds
(cider--nrepl-pprint-request-plist (cider--pretty-print-width)))))

(declare-function cider-switch-to-repl-buffer "cider-mode")

(defun cider-eval-last-sexp-to-repl (&optional prefix)
Expand All @@ -1186,11 +1264,10 @@ If invoked with a PREFIX argument, switch to the REPL buffer."
"Evaluate expr before point and insert its pretty-printed result in the REPL.
If invoked with a PREFIX argument, switch to the REPL buffer."
(interactive "P")
(let* ((conn-buffer (cider-current-connection)))
(cider-interactive-eval nil
(cider-insert-eval-handler conn-buffer)
(cider-last-sexp 'bounds)
(cider--nrepl-pprint-request-plist (cider--pretty-print-width))))
(cider-interactive-eval nil
(cider-insert-eval-handler (cider-current-connection))
(cider-last-sexp 'bounds)
(cider--nrepl-pprint-request-plist (cider--pretty-print-width)))
(when prefix
(cider-switch-to-repl-buffer)))

Expand All @@ -1211,10 +1288,13 @@ Print its value into the current buffer."
(when (consp form) form)
(cider--nrepl-pprint-request-plist (cider--pretty-print-width)))))

(defun cider-pprint-eval-last-sexp ()
"Evaluate the sexp preceding point and pprint its value in a popup buffer."
(interactive)
(cider--pprint-eval-form (cider-last-sexp 'bounds)))
(defun cider-pprint-eval-last-sexp (&optional output-to-current-buffer)
"Evaluate the sexp preceding point and pprint its value in a popup buffer.
If invoked with OUTPUT-TO-CURRENT-BUFFER, insert as comment in the current buffer."
(interactive "P")
(if output-to-current-buffer
(cider-pprint-eval-last-sexp-to-comment nil)
(cider--pprint-eval-form (cider-last-sexp 'bounds))))

(defun cider--prompt-and-insert-inline-dbg ()
"Insert a #dbg button at the current sexp."
Expand Down
1 change: 1 addition & 0 deletions cider-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
["Eval last sexp and replace" cider-eval-last-sexp-and-replace]
["Eval last sexp to REPL" cider-eval-last-sexp-to-repl]
["Eval last sexp and pretty-print to REPL" cider-pprint-eval-last-sexp-to-repl]
["Eval last sexp and pretty-print to comment" cider-pprint-eval-last-sexp-to-comment]
["Insert last sexp in REPL" cider-insert-last-sexp-in-repl]
["Eval top-level sexp to comment" cider-eval-defun-to-comment]
"--"
Expand Down
2 changes: 1 addition & 1 deletion doc/interactive_programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Here's a list of `cider-mode`'s keybindings:
`cider-eval-last-sexp-and-replace` |<kbd>C-c C-v w</kbd> | Evaluate the form preceding point and replace it with its result.
`cider-eval-last-sexp-to-repl` |<kbd>C-c M-e</kbd> | Evaluate the form preceding point and output it result to the REPL buffer. If invoked with a prefix argument, takes you to the REPL buffer after being invoked.
`cider-insert-last-sexp-in-repl` |<kbd>C-c M-p</kbd> | Load the form preceding point in the REPL buffer.
`cider-pprint-eval-last-sexp` |<kbd>C-c C-p</kbd> | Evaluate the form preceding point and pretty-print the result in a popup buffer.
`cider-pprint-eval-last-sexp` |<kbd>C-c C-p</kbd> | Evaluate the form preceding point and pretty-print the result in a popup buffer. If invoked with a prefix argument, insert the result into the current buffer as a comment.
`cider-pprint-eval-defun-at-point` |<kbd>C-c C-f</kbd> | Evaluate the top level form under point and pretty-print the result in a popup buffer.
`cider-eval-defun-at-point` |<kbd>C-M-x</kbd> <br/> <kbd>C-c C-c</kbd> | Evaluate the top level form under point and display the result in the echo area.
`cider-eval-sexp-at-point` |<kbd>C-c C-v v</kbd> | Evaluate the form around point.
Expand Down

0 comments on commit 67493a5

Please sign in to comment.