[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/eglot cbc418c 06/12: Per #22: Simplify eglot-format-buf
From: |
João Távora |
Subject: |
[elpa] externals/eglot cbc418c 06/12: Per #22: Simplify eglot-format-buffer |
Date: |
Fri, 22 Jun 2018 11:52:16 -0400 (EDT) |
branch: externals/eglot
commit cbc418c8ea01283d7e2b1d2bd243ac9d2c60bb88
Author: João Távora <address@hidden>
Commit: João Távora <address@hidden>
Per #22: Simplify eglot-format-buffer
Use replace-buffer-contents, as suggested by mkcms
<address@hidden>, but do it in eglot--apply-text-edits, where it
benefits all its users.
* README.md (Commands and keybindings): Mention
eglot-format-buffer.
* eglot.el (eglot-format-buffer): Don't try to heuristically
preserve point here.
(eglot--apply-text-edits): Use replace-buffer-contents.
* eglot-tests.el (formatting): adjust test to strictly check for point
position.
---
README.md | 5 ++++-
eglot-tests.el | 4 ++--
eglot.el | 46 +++++++++++++++++++---------------------------
3 files changed, 25 insertions(+), 30 deletions(-)
diff --git a/README.md b/README.md
index c3f4434..a56d750 100644
--- a/README.md
+++ b/README.md
@@ -61,7 +61,10 @@ Here's a summary of available commands:
- `M-x eglot-shutdown` says bye-bye to the server;
-- `M-x eglot-rename` asks the server to rename the symbol at point;
+- `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-code-actions` asks the server for any code actions at
point. These may tipically be simple fixes, like deleting an unused
diff --git a/eglot-tests.el b/eglot-tests.el
index 602606c..f56280e 100644
--- a/eglot-tests.el
+++ b/eglot-tests.el
@@ -386,9 +386,9 @@ Pass TIMEOUT to `eglot--with-timeout'."
(with-current-buffer
(eglot--find-file-noselect "project/something.py")
(should (eglot--tests-connect))
- (search-forward ":")
+ (search-forward ":pa")
(eglot-format-buffer)
- (should (looking-at "pass"))
+ (should (looking-at "ss"))
(should (or
;; yapf
(string= (buffer-string) "def foo():\n pass\n")
diff --git a/eglot.el b/eglot.el
index 447e8c1..0f366c4 100644
--- a/eglot.el
+++ b/eglot.el
@@ -1403,27 +1403,14 @@ DUMMY is ignored."
(interactive)
(unless (eglot--server-capable :documentFormattingProvider)
(eglot--error "Server can't format!"))
- (let* ((server (eglot--current-server-or-lose))
- (resp
- (eglot--request
- server
- :textDocument/formatting
- (list :textDocument (eglot--TextDocumentIdentifier)
- :options (list
- :tabSize tab-width
- :insertSpaces (not indent-tabs-mode)))
- :textDocument/formatting))
- (after-point
- (buffer-substring (point) (min (+ (point) 60) (point-max))))
- (regexp (and (not (bobp))
- (replace-regexp-in-string
- "[\s\t\n\r]+" "[\s\t\n\r]+"
- (concat "\\(" (regexp-quote after-point) "\\)")))))
- (when resp
- (save-excursion
- (eglot--apply-text-edits resp))
- (when (and (bobp) regexp (search-forward-regexp regexp nil t))
- (goto-char (match-beginning 1))))))
+ (eglot--apply-text-edits
+ (eglot--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))))))
(defun eglot-completion-at-point ()
"EGLOT's `completion-at-point' function."
@@ -1595,12 +1582,17 @@ If SKIP-SIGNATURE, don't try to send
textDocument/signatureHelp."
(unless (or (not version) (equal version eglot--versioned-identifier))
(eglot--error "Edits on `%s' require version %d, you have %d"
(current-buffer) version eglot--versioned-identifier))
- (eglot--widening
- (mapc (pcase-lambda (`(,newText ,beg . ,end))
- (goto-char beg) (delete-region beg end) (insert newText))
- (mapcar (eglot--lambda (&key range newText)
- (cons newText (eglot--range-region range 'markers)))
- edits)))
+ (mapc (pcase-lambda (`(,newText ,beg . ,end))
+ (save-restriction
+ (narrow-to-region beg end)
+ (let ((source (current-buffer)))
+ (with-temp-buffer
+ (insert newText)
+ (let ((temp (current-buffer)))
+ (with-current-buffer source (replace-buffer-contents
temp)))))))
+ (mapcar (eglot--lambda (&key range newText)
+ (cons newText (eglot--range-region range 'markers)))
+ edits))
(eglot--message "%s: Performed %s edits" (current-buffer) (length edits)))
(defun eglot--apply-workspace-edit (wedit &optional confirm)
- [elpa] externals/eglot 04ef055 11/12: Close #27: empty ranges are valid in LSP, (continued)
- [elpa] externals/eglot 04ef055 11/12: Close #27: empty ranges are valid in LSP, João Távora, 2018/06/22
- [elpa] externals/eglot 0176264 12/12: * eglot.el (Version): Bump to 0.11, João Távora, 2018/06/22
- [elpa] externals/eglot 610d95b 03/12: Use gfm-mode for formatted strings (#20), João Távora, 2018/06/22
- [elpa] externals/eglot 0cde01f 05/12: Close #26: Guess server for js2-mode and rjsx-mode, João Távora, 2018/06/22
- [elpa] externals/eglot 364348d 04/12: Close #25: Improve eglot-ensure and mention it in README.md, João Távora, 2018/06/22
- [elpa] externals/eglot efc0f45 02/12: Minor cleanup to new textDocument/formatting feature, João Távora, 2018/06/22
- [elpa] externals/eglot a9010f1 01/12: Implement formatting (#19), João Távora, 2018/06/22
- [elpa] externals/eglot 17ef74e 07/12: Defer textDocument/formatting requests, João Távora, 2018/06/22
- [elpa] externals/eglot 0b9cffe 08/12: Per #22: Apply text edits atomically, João Távora, 2018/06/22
- [elpa] externals/eglot 48c3f45 10/12: Close #22: Apply text edits as a single undoable edit, João Távora, 2018/06/22
- [elpa] externals/eglot cbc418c 06/12: Per #22: Simplify eglot-format-buffer,
João Távora <=
- [elpa] externals/eglot df5e72e 09/12: Close #23: Report progress when applying edits, João Távora, 2018/06/22