Skip to content
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

Add support for format-edn middleware & fix #977 #978

Merged
merged 2 commits into from
Feb 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [#920](https://github.com/clojure-emacs/cider/issues/920): Support `cider-jack-in` for boot-based projects.
* [#949](https://github.com/clojure-emacs/cider/issues/949): New custom var: `cider-default-repl-command`.
* New code formatting commands - `cider-format-buffer`, `cider-format-region` and `cider-format-defun`.
* New data formatting commands - `cider-format-edn-buffer` and `cider-format-edn-region`.
* Pretty printing functionality moved to middleware, adding support for ClojureScript.
- New command to eval and pprint result: `cider-interactive-pprint-eval`.
- `cider-format-pprint-eval` has been removed.
Expand All @@ -31,6 +32,7 @@ non-functioning `cider-test-jump` from test reports.
`user` namespace when using `cider-interactive-eval`.
* [#954](https://github.com/clojure-emacs/cider/issues/954): Detect properly a project's root
when in buffer that's not visiting a file (e.g. a REPL buffer).
* [#977](https://github.com/clojure-emacs/cider/issues/977): `cider-format-region` now respects indentation of the region start position

## 0.8.2 / 2014-12-21

Expand Down
13 changes: 13 additions & 0 deletions cider-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ loaded. If CALLBACK is nil, use `cider-load-file-handler'."
(nrepl-send-sync-request)
(nrepl-dict-get "formatted-code")))

(defun cider-sync-request:format-edn (edn &optional right-margin)
"Perform \"format-edn\" op with EDN and RIGHT-MARGIN."
(let* ((response (-> (list "op" "format-edn"
"edn" edn)
(append (and right-margin (list "right-margin" right-margin)))
(nrepl-send-sync-request)))
(err (nrepl-dict-get response "err")))
(when err
;; err will be a stacktrace with a first line that looks like:
;; "clojure.lang.ExceptionInfo: Unmatched delimiter ]"
(error (first (split-string err "\n"))))
(nrepl-dict-get response "formatted-edn")))

(provide 'cider-client)

;;; cider-client.el ends here
59 changes: 48 additions & 11 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ which will use the default REPL connection."
:group 'cider)

(defvar cider-required-nrepl-ops
'("apropos" "classpath" "complete" "eldoc" "format-code" "info"
'("apropos" "classpath" "complete" "eldoc" "format-code" "format-edn" "info"
"inspect-start" "inspect-refresh"
"inspect-pop" "inspect-push" "inspect-reset"
"macroexpand" "ns-list" "ns-vars"
Expand Down Expand Up @@ -1770,25 +1770,62 @@ If no buffer is provided the command acts on the current buffer."
(defalias 'cider-eval-buffer 'cider-load-buffer
"A convenience alias as some people are confused by the load-* names.")

(defun cider-format-buffer ()
"Format the code in the current buffer."
(interactive)
(defun cider--format-buffer (formatter)
"Format the contents of the current buffer.

Uses FORMATTER, a function of one argument, to convert the string contents
of the buffer into a formatted string."
(unless buffer-file-name
(error "Buffer %s is not associated with a file" (buffer-name)))
(let* ((original-code (cider-file-string buffer-file-name))
(formatted-code (cider-sync-request:format-code original-code)))
(formatted-code (funcall formatter)))
(unless (equal original-code formatted-code)
(erase-buffer)
(insert formatted-code))))

(defun cider-format-buffer ()
"Format the Clojure code in the current buffer."
(interactive)
(cider--format-buffer #'cider-sync-request:format-code))

(defun cider-format-edn-buffer ()
"Format the EDN data in the current buffer."
(interactive)
(cider--format-buffer (lambda (edn)
(cider-sync-request:format-edn edn fill-column))))

(defun cider--format-reindent (formatted start)
"Reindent FORMATTED to align with buffer position START."
(let* ((start-column (save-excursion (goto-char start) (current-column)))
(indent-line (concat "\n" (make-string start-column ? ))))
(replace-regexp-in-string "\n" indent-line formatted)))

(defun cider--format-region (start end formatter)
"Format the contents of the given region.

START and END are the character positions of the start and end of the
region. FORMATTER is a function of one argument which is used to convert
the string contents of the region into a formatted string."
(let* ((original (buffer-substring-no-properties start end))
(formatted (funcall formatter original))
(indented (cider--format-reindent formatted start)))
(unless (equal original indented)
(delete-region start end)
(insert indented))))

(defun cider-format-region (start end)
"Format the code in the current region."
"Format the Clojure code in the current region."
(interactive "r")
(let* ((original-code (buffer-substring-no-properties start end))
(formatted-code (cider-sync-request:format-code original-code)))
(unless (equal original-code formatted-code)
(delete-region start end)
(insert formatted-code))))
(cider--format-region start end #'cider-sync-request:format-code))

(defun cider-format-edn-region (start end)
"Format the EDN data in the current region."
(interactive "r")
(let* ((start-column (save-excursion (goto-char start) (current-column)))
(right-margin (- fill-column start-column)))
(cider--format-region start end
(lambda (edn)
(cider-sync-request:format-edn edn right-margin)))))

(defun cider-format-defun ()
"Format the code in the current defun."
Expand Down