From d8a91f0bc9301a05f293dec9d37474ed7b707633 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Thu, 18 Apr 2024 23:39:30 -0400 Subject: [PATCH] (eglot--signal-textDocument/didChange): Fix tests The tests call `eglot--signal-textDocument/didChange` eagerly, which means in the case where we use `track-changes` that they call it before the tracker's signal has been called. So make sure we fetch pending changes even if we haven't yet been notified of them, and make sure `eglot--versioned-identifier` is incremented even when the signal is not called. * lisp/progmodes/eglot.el (eglot--track-changes-fetch) (eglot--after-change): Increment `eglot--versioned-identifier` here... (eglot--track-changes-signal): ...instead of here. (eglot--signal-textDocument/didChange): Try and fetch changes even if the tracker's signal wasn't called yet. * test/lisp/progmodes/eglot-tests.el (eglot-tests--get): New function. (eglot-tests--lsp-abiding-column-1): Use it. (cherry picked from commit c833892140fbf4be45161bf85d4c1253c5503949) --- lisp/progmodes/eglot.el | 7 ++++--- test/lisp/progmodes/eglot-tests.el | 12 +++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 122a1d06435..29a274fb338 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -2626,6 +2626,7 @@ buffer." (defun eglot--after-change (beg end pre-change-length) "Hook onto `after-change-functions'. Records BEG, END and PRE-CHANGE-LENGTH locally." + (cl-incf eglot--versioned-identifier) (pcase (car-safe eglot--recent-changes) (`(,lsp-beg ,lsp-end (,b-beg . ,b-beg-marker) @@ -2659,6 +2660,7 @@ Records BEG, END and PRE-CHANGE-LENGTH locally." (if (eq eglot--recent-changes :pending) (setq eglot--recent-changes nil)) (track-changes-fetch id (lambda (beg end before) + (cl-incf eglot--versioned-identifier) (cond ((eq eglot--recent-changes :emacs-messup) nil) ((eq before 'error) (setf eglot--recent-changes :emacs-messup)) @@ -2669,7 +2671,6 @@ Records BEG, END and PRE-CHANGE-LENGTH locally." eglot--recent-changes)))))) (defun eglot--track-changes-signal (id &optional distance) - (cl-incf eglot--versioned-identifier) (cond (distance ;; When distance is <100, we may as well coalesce the changes. @@ -2790,9 +2791,9 @@ When called interactively, use the currently active server" (defun eglot--signal-textDocument/didChange () "Send textDocument/didChange to server." + (when eglot--track-changes + (eglot--track-changes-fetch eglot--track-changes)) (when eglot--recent-changes - (when eglot--track-changes - (eglot--track-changes-fetch eglot--track-changes)) (let* ((server (eglot--current-server-or-lose)) (sync-capability (eglot-server-capable :textDocumentSync)) (sync-kind (if (numberp sync-capability) sync-capability diff --git a/test/lisp/progmodes/eglot-tests.el b/test/lisp/progmodes/eglot-tests.el index 4725885038e..e501e24f5d2 100644 --- a/test/lisp/progmodes/eglot-tests.el +++ b/test/lisp/progmodes/eglot-tests.el @@ -821,6 +821,12 @@ int main() { (should (looking-back "\"foo.bar\": \"")) (should (looking-at "fb\"$")))))) +(defun eglot-tests--get (object path) + (dolist (op path) + (setq object (if (natnump op) (aref object op) + (plist-get object op)))) + object) + (defun eglot-tests--lsp-abiding-column-1 () (eglot--with-fixture '(("project" . @@ -837,7 +843,11 @@ int main() { (insert "p ") (eglot--signal-textDocument/didChange) (eglot--wait-for (c-notifs 2) (&key params &allow-other-keys) - (should (equal 71 (cadddr (cadadr (aref (cadddr params) 0)))))) + (message "PARAMS=%S" params) + (should (equal 71 (eglot-tests--get + params + '(:contentChanges 0 + :range :start :character))))) (beginning-of-line) (should (eq eglot-move-to-linepos-function #'eglot-move-to-utf-16-linepos)) (funcall eglot-move-to-linepos-function 71) -- 2.39.5