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 4, 2017
1 parent 946a9d4 commit 3835eca
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 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
83 changes: 73 additions & 10 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) "\n"))
(dolist (elem lines)
(insert (concat continued-prefix elem "\n")))
(unless (string= comment-postfix "")
(insert comment-postfix "\n")))))))
(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 @@ -1167,9 +1209,28 @@ With a prefix arg, LOC, insert before the form, otherwise afterwards."
(insertion-point (nth (if loc 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 (loc)
"Evaluate the last sexp and insert result as comment at LOC.
With a prefix arg, LOC, insert before the form, otherwise afterwards."
(interactive "P")
(let* ((bounds (cider-last-sexp 'bounds))
(insertion-point (nth (if loc 0 1) bounds)))
(cider-interactive-eval nil
(cider-eval-pprint-with-multiline-comment-handler
(current-buffer)
insertion-point
cider-pprint-comment-prefix
cider-pprint-comment-continued-prefix
cider-pprint-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 +1247,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 +1271,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 prefix)
"Evaluate the sexp preceding point and pprint its value in a popup buffer.
If invoked with a PREFIX argument, insert as comment in the current buffer."
(interactive "P")
(if prefix
(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 3835eca

Please sign in to comment.