Last active
December 6, 2022 02:29
-
-
Save jdtsmith/535fe465bd0e9704a85e2555d528214a to your computer and use it in GitHub Desktop.
Testing framework for Eglot's textDocument/didChange events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defvar-local eglot-report-didChange-marker nil) | |
(defvar-local eglot-report-didChange-buffer nil) | |
(defun eglot-report-didChange (change-beg change-end prev-len) | |
"Scan eglot events buffer and report textDocument/didChange events compactly. | |
Invoke this command in a given eglot events buffer." | |
(save-excursion | |
(goto-char eglot-report-didChange-marker) | |
(while (re-search-forward | |
"(:jsonrpc \"2.0\" :method \"textDocument/didChange\"" nil t) | |
(goto-char (match-beginning 0)) | |
(if-let ((s (sexp-at-point))) | |
(progn | |
(message "Found one at point %d" (point)) | |
(forward-sexp) | |
(set-marker eglot-report-didChange-marker (point)) | |
(let* ((pm (plist-get s :params)) | |
(changes (append (plist-get pm :contentChanges) nil)) | |
(print-escape-newlines t)) | |
(with-current-buffer eglot-report-didChange-buffer | |
(goto-char (point-max)) | |
(pcase-dolist (`( :range ( :start (:line ,ls :character ,cs) | |
:end (:line ,le :character ,ce)) | |
:rangeLength ,rl :text ,txt) | |
changes) | |
(insert (concat (propertize | |
(format "%d:%02d->%d:%02d (%d)" | |
ls cs le ce | |
(max (length txt) rl)) | |
'face `(:foreground | |
,(if (>= (length txt) rl) "green" "red"))) | |
(format " %s\n" (prin1-to-string txt)))))))) | |
(goto-char (match-end)))))) | |
(defun eglot-report-didChange-setup () | |
(interactive) | |
(with-current-buffer (setq eglot-report-didChange-buffer | |
(get-buffer-create (concat "didChange:" (buffer-name)))) | |
(erase-buffer) | |
(setq-local window-point-insertion-type t)) | |
(setq eglot-report-didChange-marker (point-min-marker)) | |
(eglot-report-didChange 0 0 0) | |
(add-hook 'after-change-functions 'eglot-report-didChange nil t) | |
(pop-to-buffer eglot-report-didChange-buffer)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simply M-x eval-buffer this and
M-x eglot-report-didChange-setup
(just once) in the relevant eglot events buffer.