From: Michal Krzywkowski Date: Tue, 10 Jul 2018 13:10:25 +0000 (+0200) Subject: Implement textdocument/rangeformatting X-Git-Tag: emacs-29.0.90~1616^2~524^2~4^2~465 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=5fc7c9a9ef51349375a73e6640a16a75f3ffbf60;p=emacs.git Implement textdocument/rangeformatting * eglot.el (eglot-format): New command. (eglot-format-buffer): Use it as implementation. (eglot-client-capabilities): Add :rangeFormatting. * eglot-tests.el (formatting): Also test range formatting. * README.md (Commands and keybindings): Mention eglot-format. (Language features): Tick textDocument/rangeFormatting. --- diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index fbc6a53db5e..f0a4b7144dc 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -183,6 +183,7 @@ lasted more than that many seconds." :documentHighlight `(:dynamicRegistration :json-false) :codeAction `(:dynamicRegistration :json-false) :formatting `(:dynamicRegistration :json-false) + :rangeFormatting `(:dynamicRegistration :json-false) :rename `(:dynamicRegistration :json-false) :publishDiagnostics `(:relatedInformation :json-false)) :experimental (list)))) @@ -1227,17 +1228,35 @@ DUMMY is ignored." (defun eglot-format-buffer () "Format contents of current buffer." (interactive) - (unless (eglot--server-capable :documentFormattingProvider) - (eglot--error "Server can't format!")) - (eglot--apply-text-edits - (jsonrpc-request - (eglot--current-server-or-lose) - :textDocument/formatting - (list :textDocument (eglot--TextDocumentIdentifier) - :options (list :tabSize tab-width - :insertSpaces - (if indent-tabs-mode :json-false t))) - :deferred :textDocument/formatting))) + (eglot-format nil nil)) + +(defun eglot-format (&optional beg end) + "Format region BEG END. +If either BEG or END is nil, format entire buffer. +Interactively, format active region, or entire buffer if region +is not active." + (interactive (and (region-active-p) (list (region-beginning) (region-end)))) + (pcase-let ((`(,method ,cap ,args) + (cond + ((and beg end) + `(:textDocument/rangeFormatting + :documentRangeFormattingProvider + (:range ,(list :start (eglot--pos-to-lsp-position beg) + :end (eglot--pos-to-lsp-position end))))) + (t + '(:textDocument/formatting :documentFormattingProvider nil))))) + (unless (eglot--server-capable cap) + (eglot--error "Server can't format!")) + (eglot--apply-text-edits + (jsonrpc-request + (eglot--current-server-or-lose) + method + (cl-list* + :textDocument (eglot--TextDocumentIdentifier) + :options (list :tabSize tab-width + :insertSpaces (if indent-tabs-mode :json-false t)) + args) + :deferred method)))) (defun eglot-completion-at-point () "EGLOT's `completion-at-point' function."