[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/eglot 88ebed6 17/62: Implement TextDocument/rangeFormat
From: |
Stefan Monnier |
Subject: |
[elpa] externals/eglot 88ebed6 17/62: Implement TextDocument/rangeFormatting |
Date: |
Sat, 29 Sep 2018 17:13:30 -0400 (EDT) |
branch: externals/eglot
commit 88ebed60aa55677a029bb5f947c2d5d29ecb526f
Author: Michał K <address@hidden>
Commit: João Távora <address@hidden>
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] externals/eglot 3ffea45 44/62: Close #50: Support snippet completions, (continued)
- [elpa] externals/eglot 3ffea45 44/62: Close #50: Support snippet completions, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot e711bdb 47/62: Add go-langserver (#74), Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 83d7025 36/62: Close #68: Implement asynchronous server connection, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 2355e17 23/62: Per #63: Accept functions as entries in eglot-server-programs, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot c0f9db7 46/62: Per #74: Don't error if server replies with empty hover message, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot be15bb1 43/62: Per #74: Fix eglot-capabilities when querying for multiple features, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 15040a6 48/62: Improve snippet support, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot d88f6aa 54/62: Close #86: Handle case when :textDocumentSync isn't a number, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 42fffa5 16/62: Close #54: Correctly make LSP positions in narrowed buffers, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot e935718 20/62: Fix placement of diagnostics with same start and end positions, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 88ebed6 17/62: Implement TextDocument/rangeFormatting,
Stefan Monnier <=
- [elpa] externals/eglot 792dc6b 28/62: * eglot.el (advice-add jsonrpc-request): Add &allow-other-keys, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 21886be 08/62: Close #44: Robustify in the face of manual mode changes, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 6b14711 18/62: * eglot.el (eglot-client-capabilities): Fix a typo., Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 4c019bd 24/62: * eglot.el (eglot-initialization-options): Fix spurious typo., Stefan Monnier, 2018/09/29
- [elpa] externals/eglot a62c2da 25/62: Close #60: Notify server of recent changes before save notification, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot c8191b2 32/62: Improve eglot-execute-command API to ease overriding by servers, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 991d129 34/62: * README.md (Build Status): Show status for master, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot ae37c2a 35/62: Add a test for eglot-ensure. Make, Stefan Monnier, 2018/09/29
- [elpa] externals/eglot cac728a 33/62: Kill server's output and events buffers from eglot-shutdown (#66), Stefan Monnier, 2018/09/29
- [elpa] externals/eglot 9ae03af 39/62: Close #41: Control the size of the events buffer, Stefan Monnier, 2018/09/29