Created
December 6, 2024 21:41
-
-
Save vkjr/0e585c7f8ef2ea918bb0faaba5ba5334 to your computer and use it in GitHub Desktop.
Command to show all project errors from LSP in a convenient xref buffer
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
(defun vk/show-lsp-diagnostics-xref () | |
"Display LSP diagnostics in a persistent `xref`-style buffer." | |
(interactive) | |
(let ((diagnostics (lsp-diagnostics)) | |
(xref-items '())) ;; Initialize xref items list | |
;; Collect diagnostics as `xref-item` structures | |
(maphash | |
(lambda (file errors) | |
(dolist (error errors) | |
(let* ((range (gethash "range" error)) | |
(start (gethash "start" range)) | |
(line (1+ (gethash "line" start))) | |
(character (gethash "character" start)) | |
(message (gethash "message" error)) | |
(location (xref-make-file-location file line character))) | |
(push (xref-make message location) xref-items)))) | |
diagnostics) | |
;; Create a custom xref group for diagnostics | |
(let ((xref-buffer-name "*LSP Diagnostics Xref*")) | |
(with-current-buffer (get-buffer-create xref-buffer-name) | |
;; Use `xref` display machinery with custom buffer | |
(let ((xref-backend-functions | |
(list (lambda () (lambda () xref-items))))) | |
(xref--show-xrefs (lambda () xref-items) nil))) | |
;; Display the custom xref buffer | |
(pop-to-buffer xref-buffer-name)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to adapt the snippet to use
map-elt
because in my version of lsp theerror
instance is not a hashmap, that's why the original snippet was failing for me.The xref buffer appears to me in the minibuffer, which is very nice. I also liked to export the results with
embark-export
.