[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] 1.2 01ec85f 011/101: Implement TextDocument/rangeFormatting
From: |
Christian Johansson |
Subject: |
[elpa] 1.2 01ec85f 011/101: Implement TextDocument/rangeFormatting |
Date: |
Thu, 29 Apr 2021 15:08:50 -0400 (EDT) |
tag: 1.2
commit 01ec85ff9dd18824625ebddc92df1509c4cecd25
Author: Michał K <k.michal@zoho.com>
Commit: João Távora <joaotavora@gmail.com>
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.
---
README.md | 6 +++---
eglot-tests.el | 34 +++++++++++++++++++++-------------
eglot.el | 41 ++++++++++++++++++++++++++++++-----------
3 files changed, 54 insertions(+), 27 deletions(-)
diff --git a/README.md b/README.md
index f32f478..860dda8 100644
--- a/README.md
+++ b/README.md
@@ -96,8 +96,8 @@ Here's a summary of available commands:
- `M-x eglot-rename` ask the server to rename the symbol at point;
-- `M-x eglot-format-buffer` ask the server to reformat the current
- buffer;
+- `M-x eglot-format` asks the server to format buffer or the active
+ region;
- `M-x eglot-code-actions` asks the server for any code actions at
point. These may tipically be simple fixes, like deleting an unused
@@ -206,7 +206,7 @@ eglot-shutdown`.
- [ ] textDocument/documentColor
- [ ] textDocument/colorPresentation (3.6.0)
- [x] textDocument/formatting
-- [ ] textDocument/rangeFormatting
+- [x] textDocument/rangeFormatting
- [ ] textDocument/onTypeFormatting
- [x] textDocument/rename
diff --git a/eglot-tests.el b/eglot-tests.el
index 3932769..6940fcd 100644
--- a/eglot-tests.el
+++ b/eglot-tests.el
@@ -376,23 +376,31 @@ Pass TIMEOUT to `eglot--with-timeout'."
(should (string-match "^exit" eldoc-last-message)))))
(ert-deftest formatting ()
- "Test document formatting in a python LSP"
+ "Test formatting in a python LSP"
(skip-unless (and (executable-find "pyls")
(or (executable-find "yapf")
(executable-find "autopep8"))))
(eglot--with-dirs-and-files
- '(("project" . (("something.py" . "def foo():pass"))))
- (with-current-buffer
- (eglot--find-file-noselect "project/something.py")
- (should (eglot--tests-connect))
- (search-forward ":pa")
- (eglot-format-buffer)
- (should (looking-at "ss"))
- (should (or
- ;; yapf
- (string= (buffer-string) "def foo():\n pass\n")
- ;; autopep8
- (string= (buffer-string) "def foo(): pass\n"))))))
+ '(("project" . (("something.py" . "def a():pass\ndef b():pass"))))
+ (with-current-buffer
+ (eglot--find-file-noselect "project/something.py")
+ (should (eglot--tests-connect))
+ (search-forward "b():pa")
+ (eglot-format (point-at-bol) (point-at-eol))
+ (should (looking-at "ss"))
+ (should
+ (or
+ ;; yapf
+ (string= (buffer-string) "def a():pass\n\n\ndef b():\n pass\n")
+ ;; autopep8
+ (string= (buffer-string) "def a():pass\n\n\ndef b(): pass\n")))
+ (eglot-format-buffer)
+ (should
+ (or
+ ;; yapf
+ (string= (buffer-string) "def a():\n pass\n\n\ndef b():\n pass\n")
+ ;; autopep8
+ (string= (buffer-string) "def a(): pass\n\n\ndef b(): pass\n"))))))
(ert-deftest javascript-basic ()
"Test basic autocompletion in a python LSP"
diff --git a/eglot.el b/eglot.el
index fbc6a53..f0a4b71 100644
--- a/eglot.el
+++ b/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."
- [elpa] 1.2 38a27be 029/101: Add a test for eglot-ensure. Make, (continued)
- [elpa] 1.2 38a27be 029/101: Add a test for eglot-ensure. Make, Christian Johansson, 2021/04/29
- [elpa] 1.2 05e5a9f 063/101: Per #63: Allow function contacts to be interactive, Christian Johansson, 2021/04/29
- [elpa] 1.2 922f4e5 060/101: Merge pull request #104 from mkcms/fix-diagnostics-wrong-type-argument, Christian Johansson, 2021/04/29
- [elpa] 1.2 7b78b15 055/101: Autoload eglot-ensure (#120), Christian Johansson, 2021/04/29
- [elpa] 1.2 bfadd9a 053/101: Close #100: Don't send other notifications before initialized, Christian Johansson, 2021/04/29
- [elpa] 1.2 737f08d 056/101: Correctly map DocumentSymbol's :kind to its name (#121), Christian Johansson, 2021/04/29
- [elpa] 1.2 3c9e00e 068/101: * .travis.yml (install): Download eclipse.jdt.ls server., Christian Johansson, 2021/04/29
- [elpa] 1.2 be464f2 005/101: Add entry for haskell-ide-engine in eglot-server-programs (#49), Christian Johansson, 2021/04/29
- [elpa] 1.2 0694579 009/101: Fix typo in willSaveWaitUntil RPC request (#51), Christian Johansson, 2021/04/29
- [elpa] 1.2 f4f93d3 014/101: Fix placement of diagnostics with same start and end positions, Christian Johansson, 2021/04/29
- [elpa] 1.2 01ec85f 011/101: Implement TextDocument/rangeFormatting,
Christian Johansson <=
- [elpa] 1.2 d1cfc9e 008/101: Work around Emacs bugs 32237, 32278 (#53), Christian Johansson, 2021/04/29
- [elpa] 1.2 e33fadb 012/101: * eglot.el (eglot-client-capabilities): Fix a typo., Christian Johansson, 2021/04/29
- [elpa] 1.2 ee372b4 022/101: * eglot.el (advice-add jsonrpc-request): Add &allow-other-keys, Christian Johansson, 2021/04/29
- [elpa] 1.2 9bf88ee 031/101: Allow tests to be run with custom jsonrpc.el, Christian Johansson, 2021/04/29
- [elpa] 1.2 cf3376a 034/101: Close #64: handle edits to same position in the correct order, Christian Johansson, 2021/04/29
- [elpa] 1.2 5e30066 032/101: Update README.md, Christian Johansson, 2021/04/29
- [elpa] 1.2 afa5439 027/101: Kill server's output and events buffers from eglot-shutdown (#66), Christian Johansson, 2021/04/29
- [elpa] 1.2 d6b49d8 028/101: * README.md (Build Status): Show status for master, Christian Johansson, 2021/04/29
- [elpa] 1.2 c2e05a3 030/101: Close #68: Implement asynchronous server connection, Christian Johansson, 2021/04/29
- [elpa] 1.2 3432d21 037/101: Per #74: Fix eglot-capabilities when querying for multiple features, Christian Johansson, 2021/04/29