2018-08-25 00:02:53 +02:00
|
|
|
;;; editor/format/autoload.el -*- lexical-binding: t; -*-
|
|
|
|
|
2018-10-19 11:48:47 -04:00
|
|
|
(defvar +format-region-p nil
|
|
|
|
"Is non-nil if currently reformatting a selected region, rather than the whole
|
|
|
|
buffer.")
|
2018-09-10 08:22:42 -04:00
|
|
|
|
2018-08-30 14:44:37 +02:00
|
|
|
;;;###autoload
|
2019-06-08 16:46:43 +02:00
|
|
|
(autoload 'format-all--probe "format-all")
|
2018-08-30 14:44:37 +02:00
|
|
|
|
2018-08-29 12:48:38 +02:00
|
|
|
(defun +format--delete-whole-line (&optional arg)
|
|
|
|
"Delete the current line without putting it in the `kill-ring'.
|
|
|
|
Derived from function `kill-whole-line'. ARG is defined as for that
|
2018-08-30 14:56:50 +02:00
|
|
|
function.
|
|
|
|
|
|
|
|
Stolen shamelessly from go-mode"
|
2018-08-29 12:48:38 +02:00
|
|
|
(setq arg (or arg 1))
|
|
|
|
(if (and (> arg 0)
|
|
|
|
(eobp)
|
|
|
|
(save-excursion (forward-visible-line 0) (eobp)))
|
|
|
|
(signal 'end-of-buffer nil))
|
|
|
|
(if (and (< arg 0)
|
|
|
|
(bobp)
|
|
|
|
(save-excursion (end-of-visible-line) (bobp)))
|
|
|
|
(signal 'beginning-of-buffer nil))
|
|
|
|
(cond ((zerop arg)
|
|
|
|
(delete-region (progn (forward-visible-line 0) (point))
|
|
|
|
(progn (end-of-visible-line) (point))))
|
|
|
|
((< arg 0)
|
|
|
|
(delete-region (progn (end-of-visible-line) (point))
|
|
|
|
(progn (forward-visible-line (1+ arg))
|
|
|
|
(unless (bobp)
|
|
|
|
(backward-char))
|
|
|
|
(point))))
|
|
|
|
((delete-region (progn (forward-visible-line 0) (point))
|
|
|
|
(progn (forward-visible-line arg) (point))))))
|
|
|
|
|
2018-08-25 00:02:53 +02:00
|
|
|
;;;###autoload
|
2018-08-29 12:48:38 +02:00
|
|
|
(defun +format--apply-rcs-patch (patch-buffer)
|
2018-08-30 14:56:50 +02:00
|
|
|
"Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer.
|
|
|
|
|
|
|
|
Stolen shamelessly from go-mode"
|
2018-08-29 12:48:38 +02:00
|
|
|
(let ((target-buffer (current-buffer))
|
|
|
|
;; Relative offset between buffer line numbers and line numbers
|
|
|
|
;; in patch.
|
|
|
|
;;
|
|
|
|
;; Line numbers in the patch are based on the source file, so
|
|
|
|
;; we have to keep an offset when making changes to the
|
|
|
|
;; buffer.
|
|
|
|
;;
|
|
|
|
;; Appending lines decrements the offset (possibly making it
|
|
|
|
;; negative), deleting lines increments it. This order
|
|
|
|
;; simplifies the forward-line invocations.
|
|
|
|
(line-offset 0)
|
|
|
|
(column (current-column)))
|
|
|
|
(save-excursion
|
|
|
|
(with-current-buffer patch-buffer
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (not (eobp))
|
|
|
|
(unless (looking-at "^\\([ad]\\)\\([0-9]+\\) \\([0-9]+\\)")
|
|
|
|
(error "Invalid rcs patch or internal error in +format--apply-rcs-patch"))
|
|
|
|
(forward-line)
|
|
|
|
(let ((action (match-string 1))
|
|
|
|
(from (string-to-number (match-string 2)))
|
|
|
|
(len (string-to-number (match-string 3))))
|
|
|
|
(cond
|
|
|
|
((equal action "a")
|
|
|
|
(let ((start (point)))
|
|
|
|
(forward-line len)
|
|
|
|
(let ((text (buffer-substring start (point))))
|
|
|
|
(with-current-buffer target-buffer
|
|
|
|
(cl-decf line-offset len)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(forward-line (- from len line-offset))
|
|
|
|
(insert text)))))
|
|
|
|
((equal action "d")
|
|
|
|
(with-current-buffer target-buffer
|
|
|
|
(goto-char (point-min))
|
|
|
|
(forward-line (1- (- from line-offset)))
|
|
|
|
(cl-incf line-offset len)
|
|
|
|
(+format--delete-whole-line len)))
|
|
|
|
((error "Invalid rcs patch or internal error in +format--apply-rcs-patch")))))))
|
|
|
|
(move-to-column column)))
|
|
|
|
|
2018-09-04 02:11:07 +02:00
|
|
|
(defun +format--current-indentation ()
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(skip-chars-forward " \t\n")
|
|
|
|
(current-indentation)))
|
|
|
|
|
2018-08-29 12:48:38 +02:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Public library
|
2018-09-08 23:36:24 -04:00
|
|
|
|
|
|
|
(defun +format-completing-read ()
|
2020-03-10 01:48:20 -04:00
|
|
|
"TODO"
|
2018-09-08 23:36:24 -04:00
|
|
|
(require 'format-all)
|
2019-06-08 16:46:43 +02:00
|
|
|
(let* ((fmtlist (mapcar #'symbol-name (hash-table-keys format-all--format-table)))
|
2018-09-08 23:36:24 -04:00
|
|
|
(fmt (completing-read "Formatter: " fmtlist)))
|
2020-01-10 21:14:03 +01:00
|
|
|
(if fmt (intern fmt))))
|
2018-09-08 23:36:24 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-07-23 12:30:47 +02:00
|
|
|
(defun +format-probe-a (orig-fn)
|
2020-03-10 01:48:20 -04:00
|
|
|
"Use `+format-with' instead, if it is set.
|
|
|
|
Prompts for a formatter if universal arg is set."
|
2020-05-20 04:45:19 -04:00
|
|
|
(cond ((or (eq +format-with :none)
|
|
|
|
(doom-temp-buffer-p (current-buffer))
|
|
|
|
(doom-special-buffer-p (current-buffer)))
|
2020-06-13 00:43:48 -04:00
|
|
|
(list nil nil))
|
2020-05-20 04:45:19 -04:00
|
|
|
(current-prefix-arg
|
2020-03-10 01:48:20 -04:00
|
|
|
(list (or (+format-completing-read)
|
|
|
|
(user-error "Aborted"))
|
|
|
|
t))
|
|
|
|
(+format-with
|
|
|
|
(list +format-with t))
|
|
|
|
((funcall orig-fn))))
|
2018-08-25 00:02:53 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2020-03-10 01:48:20 -04:00
|
|
|
(defun +format-buffer-a (orig-fn formatter mode-result)
|
|
|
|
"Advice that extends `format-all-buffer--with' to:
|
2018-08-29 17:02:49 +02:00
|
|
|
|
2020-03-10 01:48:20 -04:00
|
|
|
1. Enable partial/region reformatting, while preserving leading indentation,
|
|
|
|
2. Applies changes via RCS patch, line by line, to protect buffer markers and
|
|
|
|
reduce cursor movement or window scrolling.
|
2018-08-30 14:56:50 +02:00
|
|
|
|
2018-08-29 17:02:49 +02:00
|
|
|
See `+format/buffer' for the interactive version of this function, and
|
2019-07-23 12:30:47 +02:00
|
|
|
`+format-buffer-h' to use as a `before-save-hook' hook."
|
2020-03-10 01:48:20 -04:00
|
|
|
(let ((f-function (gethash formatter format-all--format-table))
|
|
|
|
(executable (format-all--formatter-executable formatter))
|
|
|
|
(indent 0)
|
|
|
|
(old-line-number (line-number-at-pos))
|
|
|
|
(old-column (current-column)))
|
|
|
|
(pcase-let*
|
|
|
|
((`(,output ,errput)
|
|
|
|
;; To reliably format regions, rather than the whole buffer, and
|
|
|
|
;; `format-all' (and various formatting functions, like `gofmt') widen
|
|
|
|
;; the buffer, we must copy the region first.
|
|
|
|
(let ((output (buffer-substring-no-properties (point-min) (point-max)))
|
|
|
|
(origin-buffer (or (buffer-base-buffer) (current-buffer))))
|
|
|
|
(with-temp-buffer
|
|
|
|
(with-silent-modifications
|
|
|
|
(insert output)
|
|
|
|
;; Ensure this temp buffer seems as much like the origin
|
|
|
|
;; buffer as possible, in case the formatter is an elisp
|
|
|
|
;; function, like `gofmt'.
|
|
|
|
(cl-loop for (var . val)
|
|
|
|
in (cl-remove-if-not #'listp (buffer-local-variables origin-buffer))
|
|
|
|
;; Making enable-multibyte-characters buffer-local
|
|
|
|
;; causes an error.
|
|
|
|
unless (eq var 'enable-multibyte-characters)
|
|
|
|
;; Using setq-local would quote var.
|
|
|
|
do (set (make-local-variable var) val))
|
|
|
|
;; Since we're piping a region of text to the formatter, remove
|
|
|
|
;; any leading indentation to make it look like a file.
|
|
|
|
(setq indent (+format--current-indentation))
|
|
|
|
(when (> indent 0)
|
|
|
|
(indent-rigidly (point-min) (point-max) (- indent)))
|
|
|
|
(funcall f-function executable mode-result)))))
|
|
|
|
(`,status
|
|
|
|
(cond ((null output) :error)
|
|
|
|
((eq output t) :already-formatted)
|
|
|
|
(t :reformatted))))
|
|
|
|
(unwind-protect
|
|
|
|
(when (eq status :reformatted)
|
|
|
|
(let ((tmpfile (make-temp-file "doom-format"))
|
|
|
|
(patchbuf (get-buffer-create " *doom format patch*"))
|
|
|
|
(coding-system-for-read coding-system-for-read)
|
|
|
|
(coding-system-for-write coding-system-for-write))
|
|
|
|
(unless IS-WINDOWS
|
|
|
|
(setq coding-system-for-read 'utf-8
|
|
|
|
coding-system-for-write 'utf-8))
|
|
|
|
(unwind-protect
|
|
|
|
(progn
|
|
|
|
(with-current-buffer patchbuf
|
|
|
|
(erase-buffer))
|
|
|
|
(with-temp-file tmpfile
|
|
|
|
(erase-buffer)
|
|
|
|
(insert output)
|
|
|
|
(when (> indent 0)
|
|
|
|
;; restore indentation without affecting new
|
|
|
|
;; indentation
|
|
|
|
(indent-rigidly (point-min) (point-max)
|
|
|
|
(max 0 (- indent (+format--current-indentation))))))
|
|
|
|
(if (zerop (call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "-" tmpfile))
|
|
|
|
(setq status :already-formatted)
|
|
|
|
(+format--apply-rcs-patch patchbuf)
|
|
|
|
(list output errput)))
|
|
|
|
(kill-buffer patchbuf)
|
|
|
|
(delete-file tmpfile))))
|
|
|
|
(format-all--show-or-hide-errors errput)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(forward-line (1- old-line-number))
|
|
|
|
(let ((line-length (- (point-at-eol) (point-at-bol))))
|
|
|
|
(goto-char (+ (point) (min old-column line-length))))
|
|
|
|
(run-hook-with-args 'format-all-after-format-functions formatter status)
|
|
|
|
(message (pcase status
|
|
|
|
(:error "Formatting error")
|
|
|
|
(:already-formatted "Already formatted")
|
|
|
|
(:reformatted (format "Reformatted with %s" formatter))))))))
|
2018-08-25 00:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
2020-03-10 01:48:20 -04:00
|
|
|
;;; Commands
|
2018-08-25 00:02:53 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2020-05-08 16:45:58 -04:00
|
|
|
(defun +format/buffer ()
|
|
|
|
"Reformat the current buffer using LSP or `format-all-buffer'."
|
|
|
|
(interactive)
|
|
|
|
(call-interactively
|
2020-05-29 13:09:30 +09:00
|
|
|
(cond ((and +format-with-lsp
|
|
|
|
(bound-and-true-p lsp-mode)
|
|
|
|
(lsp-feature? "textDocument/formatting"))
|
|
|
|
#'lsp-format-buffer)
|
|
|
|
((and +format-with-lsp
|
|
|
|
(bound-and-true-p eglot--managed-mode)
|
|
|
|
(eglot--server-capable :documentFormattingProvider))
|
|
|
|
#'eglot-format-buffer)
|
2020-07-23 01:04:04 -04:00
|
|
|
(#'format-all-buffer))))
|
2018-08-25 00:02:53 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2020-03-26 22:48:14 +09:00
|
|
|
(defun +format/region (beg end)
|
2018-08-25 00:02:53 +02:00
|
|
|
"Runs the active formatter on the lines within BEG and END.
|
|
|
|
|
|
|
|
WARNING: this may not work everywhere. It will throw errors if the region
|
|
|
|
contains a syntax error in isolation. It is mostly useful for formatting
|
|
|
|
snippets or single lines."
|
2018-09-08 23:36:24 -04:00
|
|
|
(interactive "rP")
|
2020-05-29 13:09:30 +09:00
|
|
|
(cond ((and +format-with-lsp
|
|
|
|
(bound-and-true-p lsp-mode)
|
|
|
|
(lsp-feature? "textDocument/rangeFormatting"))
|
|
|
|
(call-interactively #'lsp-format-region))
|
|
|
|
((and +format-with-lsp
|
|
|
|
(bound-and-true-p eglot--managed-mode)
|
|
|
|
(eglot--server-capable :documentRangeFormattingProvider))
|
|
|
|
(call-interactively #'eglot-format))
|
2020-07-23 01:04:04 -04:00
|
|
|
((save-restriction
|
|
|
|
(narrow-to-region beg end)
|
|
|
|
(let ((+format-region-p t))
|
|
|
|
(+format/buffer))))))
|
2018-08-25 00:02:53 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-04-02 00:51:29 -04:00
|
|
|
(defun +format/region-or-buffer ()
|
2018-08-25 00:02:53 +02:00
|
|
|
"Runs the active formatter on the selected region (or whole buffer, if nothing
|
|
|
|
is selected)."
|
2019-04-02 00:51:29 -04:00
|
|
|
(interactive)
|
|
|
|
(call-interactively
|
2020-05-06 16:29:32 -04:00
|
|
|
(if (doom-region-active-p)
|
2020-05-08 16:45:58 -04:00
|
|
|
#'+format/region
|
|
|
|
#'+format/buffer)))
|
2018-08-26 17:26:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; Hooks
|
|
|
|
|
2018-08-27 10:30:31 +02:00
|
|
|
;;;###autoload
|
2019-07-23 12:30:47 +02:00
|
|
|
(defun +format-enable-on-save-h ()
|
2018-08-28 12:02:56 +02:00
|
|
|
"Enables formatting on save."
|
2019-07-23 12:30:47 +02:00
|
|
|
(add-hook 'before-save-hook #'+format-buffer-h nil t))
|
2018-08-27 10:30:31 +02:00
|
|
|
|
2018-08-26 17:26:54 +02:00
|
|
|
;;;###autoload
|
2019-07-23 12:30:47 +02:00
|
|
|
(defalias '+format-buffer-h #'+format/buffer
|
2018-08-30 14:57:31 +02:00
|
|
|
"Format the source code in the current buffer with minimal feedback.
|
|
|
|
|
2018-09-18 13:14:39 -04:00
|
|
|
Meant for `before-save-hook'.")
|