From: João Távora Date: Wed, 20 Jun 2018 18:29:30 +0000 (+0100) Subject: Simplify eglot-format-buffer X-Git-Tag: emacs-29.0.90~1616^2~524^2~4^2~496 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=4f346ba25036ca5525ab298aa34a3bcb4f4807f1;p=emacs.git Simplify eglot-format-buffer Use replace-buffer-contents, as suggested by mkcms , 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. GitHub-reference: per https://github.com/joaotavora/eglot/issues/22 --- diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 447e8c17f25..0f366c41a74 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/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)