branch: externals/eglot commit cbc418c8ea01283d7e2b1d2bd243ac9d2c60bb88 Author: João Távora <joaotav...@gmail.com> Commit: João Távora <joaotav...@gmail.com>
Per #22: Simplify eglot-format-buffer Use replace-buffer-contents, as suggested by mkcms <k.mic...@zoho.com>, 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)