Make publishDiagnostics faster by using cached variable

* lisp/progmodes/eglot.el (eglot--cached-tdi): Move variable.
(eglot-handle-notification): Expose 'server' and search through managed
buffers for a cached textDocumentIdentifier, which has a file-truename
resolved path.
* test/lisp/progmodes/eglot-tests.el (eglot-test-basic-symlink): Add
regression test for symlink behavior
This commit is contained in:
Theodor Thornhill 2024-04-19 20:40:25 +02:00
parent 82775f2141
commit 49ef173b02
2 changed files with 37 additions and 6 deletions

View file

@ -2381,8 +2381,11 @@ still unanswered LSP requests to the server\n")))
(lambda ()
(remhash token (eglot--progress-reporters server))))))))))
(defvar-local eglot--cached-tdi nil
"A cached LSP TextDocumentIdentifier URI string.")
(cl-defmethod eglot-handle-notification
(_server (_method (eql textDocument/publishDiagnostics)) &key uri diagnostics
(server (_method (eql textDocument/publishDiagnostics)) &key uri diagnostics
&allow-other-keys) ; FIXME: doesn't respect `eglot-strict-mode'
"Handle notification publishDiagnostics."
(cl-flet ((eglot--diag-type (sev)
@ -2391,9 +2394,18 @@ still unanswered LSP requests to the server\n")))
((= sev 2) 'eglot-warning)
(t 'eglot-note)))
(mess (source code message)
(concat source (and code (format " [%s]" code)) ": " message)))
(concat source (and code (format " [%s]" code)) ": " message))
(find-it (uri)
;; Search the managed buffers for a buffer with the
;; provided diagnostic from the server. We do this to
;; avoid calling `file-truename' too often, gaining an
;; increase in performance.
(cl-loop for b in (eglot--managed-buffers server)
when (with-current-buffer b
(equal eglot--cached-tdi uri))
return b)))
(if-let* ((path (expand-file-name (eglot-uri-to-path uri)))
(buffer (find-buffer-visiting path)))
(buffer (find-it uri)))
(with-current-buffer buffer
(cl-loop
initially
@ -2518,9 +2530,6 @@ THINGS are either registrations or unregisterations (sic)."
(t (setq success :json-false)))
`(:success ,success)))
(defvar-local eglot--cached-tdi nil
"A cached LSP TextDocumentIdentifier URI string.")
(defun eglot--TextDocumentIdentifier ()
"Compute TextDocumentIdentifier object for current buffer."
`(:uri ,(or eglot--cached-tdi

View file

@ -436,6 +436,28 @@ directory hierarchy."
(flymake-goto-next-error 1 '() t)
(should (eq 'flymake-error (face-at-point)))))))
(ert-deftest eglot-test-basic-symlink ()
"Test basic symlink support."
(skip-unless (executable-find "clangd"))
(eglot--with-fixture
`(("symlink-project" .
(("main.cpp" . "#include\"foo.h\"\nint main() { return foo(); }")
("foo.h" . "int foo();"))))
(with-current-buffer
(find-file-noselect "symlink-project/main.cpp")
(make-symbolic-link "main.cpp" "mainlink.cpp")
(eglot--tests-connect)
(find-file-noselect "mainlink.cpp")
(with-current-buffer
(find-file-noselect "foo.h")
(goto-char 5)
(xref-find-references "foo")
(with-current-buffer (get-buffer "*xref*")
(end-of-buffer)
;; Expect the xref buffer to not contain duplicate references to
;; main.c and mainlink.c. If it did total lines would be 7.
(should (= (line-number-at-pos (point)) 5)))))))
(ert-deftest eglot-test-diagnostic-tags-unnecessary-code ()
"Test rendering of diagnostics tagged \"unnecessary\"."
(skip-unless (executable-find "clangd"))