emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 7ff41bf8ed8: Eglot: fix textDocument/onTypeFormatting for 'newlin


From: João Távora
Subject: master 7ff41bf8ed8: Eglot: fix textDocument/onTypeFormatting for 'newline' command
Date: Thu, 20 Jul 2023 06:08:34 -0400 (EDT)

branch: master
commit 7ff41bf8ed8439c949af4cdd3ae8d01c8cdf997f
Author: João Távora <joaotavora@gmail.com>
Commit: João Távora <joaotavora@gmail.com>

    Eglot: fix textDocument/onTypeFormatting for 'newline' command
    
    In the newline command, last-input-event is 13 (carriage return), but
    most, if not all, language servers that support
    documentOnTypeFormattingProvider expect 10 (linefeed) to be the
    trigger, so convert 13 to 10 for the purposes of the
    textDocument/onTypeFormatting request.
    
    Also make this common edit silent in the mode-line/messages.
    
    * lisp/progmodes/eglot.el (eglot--post-self-insert-hook): Convert
    linefeed to carriage return.
    (eglot-format): Pass SILENT to eglot--apply-text-edits.
    (eglot--apply-text-edits): Take new optional SILENT arg.
    
    * etc/EGLOT-NEWS: Mention change
---
 etc/EGLOT-NEWS          |  7 +++++++
 lisp/progmodes/eglot.el | 34 +++++++++++++++++++++-------------
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/etc/EGLOT-NEWS b/etc/EGLOT-NEWS
index 6a2e9051ddc..0ccc8af3169 100644
--- a/etc/EGLOT-NEWS
+++ b/etc/EGLOT-NEWS
@@ -27,6 +27,13 @@ watching requests.  This change slightly reduces the number 
of file
 watcher objects requested from the operating system, which can be a
 problem, particularly on Mac OS.  See github#1228 and github#1226.
 
+** Fixed "onTypeFormatting" feature
+
+This feature wasn't triggered for the 'newline' command because
+language servers often expect 10 (linefeed) to be the trigger
+character, but 'newline' emits 13 (carriage return).  Also made this
+feature less chatty in the mode-line and messages buffer.
+
 
 * Changes in Eglot 1.15 (29/4/2023)
 
diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 1df3a8844f8..172fd97fdb5 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -2448,15 +2448,17 @@ buffer."
 (defun eglot--post-self-insert-hook ()
   "Set `eglot--last-inserted-char', maybe call on-type-formatting."
   (setq eglot--last-inserted-char last-input-event)
-  (let ((ot-provider (eglot--server-capable 
:documentOnTypeFormattingProvider)))
+  (let ((ot-provider (eglot--server-capable :documentOnTypeFormattingProvider))
+        ;; transform carriage return into line-feed
+        (adjusted-ie (if (= last-input-event 13) 10 last-input-event)))
     (when (and ot-provider
                (ignore-errors ; github#906, some LS's send empty strings
-                 (or (eq last-input-event
+                 (or (eq adjusted-ie
                          (seq-first (plist-get ot-provider 
:firstTriggerCharacter)))
-                     (cl-find last-input-event
+                     (cl-find adjusted-ie
                               (plist-get ot-provider :moreTriggerCharacter)
                               :key #'seq-first))))
-      (eglot-format (point) nil last-input-event))))
+      (eglot-format (point) nil adjusted-ie))))
 
 (defvar eglot--workspace-symbols-cache (make-hash-table :test #'equal)
   "Cache of `workspace/Symbol' results  used by `xref-find-definitions'.")
@@ -2973,7 +2975,9 @@ for which LSP on-type-formatting should be requested."
                       :insertSpaces (if indent-tabs-mode :json-false t)
                       :insertFinalNewline (if require-final-newline t 
:json-false)
                       :trimFinalNewlines (if delete-trailing-lines t 
:json-false))
-       args)))))
+       args))
+     nil
+     on-type-format)))
 
 (defvar eglot-cache-session-completions t
   "If non-nil Eglot caches data during completion sessions.")
@@ -3380,8 +3384,9 @@ Returns a list as described in docstring of 
`imenu--index-alist'."
         (((SymbolInformation)) (eglot--imenu-SymbolInformation res))
         (((DocumentSymbol)) (eglot--imenu-DocumentSymbol res))))))
 
-(cl-defun eglot--apply-text-edits (edits &optional version)
-  "Apply EDITS for current buffer if at VERSION, or if it's nil."
+(cl-defun eglot--apply-text-edits (edits &optional version silent)
+  "Apply EDITS for current buffer if at VERSION, or if it's nil.
+If SILENT, don't echo progress in mode-line."
   (unless edits (cl-return-from eglot--apply-text-edits))
   (unless (or (not version) (equal version eglot--versioned-identifier))
     (jsonrpc-error "Edits on `%s' require version %d, you have %d"
@@ -3389,10 +3394,11 @@ Returns a list as described in docstring of 
`imenu--index-alist'."
   (atomic-change-group
     (let* ((change-group (prepare-change-group))
            (howmany (length edits))
-           (reporter (make-progress-reporter
-                      (format "[eglot] applying %s edits to `%s'..."
-                              howmany (current-buffer))
-                      0 howmany))
+           (reporter (unless silent
+                       (make-progress-reporter
+                        (format "[eglot] applying %s edits to `%s'..."
+                                howmany (current-buffer))
+                        0 howmany)))
            (done 0))
       (mapc (pcase-lambda (`(,newText ,beg . ,end))
               (let ((source (current-buffer)))
@@ -3404,12 +3410,14 @@ Returns a list as described in docstring of 
`imenu--index-alist'."
                         (save-restriction
                           (narrow-to-region beg end)
                           (replace-buffer-contents temp)))
-                      (eglot--reporter-update reporter (cl-incf done)))))))
+                      (when reporter
+                        (eglot--reporter-update reporter (cl-incf done))))))))
             (mapcar (eglot--lambda ((TextEdit) range newText)
                       (cons newText (eglot--range-region range 'markers)))
                     (reverse edits)))
       (undo-amalgamate-change-group change-group)
-      (progress-reporter-done reporter))))
+      (when reporter
+        (progress-reporter-done reporter)))))
 
 (defun eglot--apply-workspace-edit (wedit &optional confirm)
   "Apply the workspace edit WEDIT.  If CONFIRM, ask user first."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]