Subject: bug#64274: 30.0.50; Eglot requests textDocument/documentSymbol if its not declared in the server capabilities

From: Wilhelm Kirschbaum <[email-masked]>
Date: Sat, 24 Jun 2023 22:07:59 +0200

Here is a naive patch which fixes it for me.

>From 9a1389305d92f0e08d39d2ff5540cb494c012f12 Mon Sep 17 00:00:00 2001
From: Wilhelm H Kirschbaum <[email-masked]>
Date: Sat, 24 Jun 2023 21:54:30 +0200
Subject: [PATCH 1/1] Fix eglot-imenu when the server does not support it

A language server might not support textDocument/documentSymbol, so we
need to check first.

* lisp/progmodes/eglot.el (eglot-imenu): Check for the
textDocument/documentSymbol capability before requesting.
---
 lisp/progmodes/eglot.el | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index e2478f2dde3..265cfc88579 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -3351,16 +3351,17 @@ eglot--imenu-DocumentSymbol
 (defun eglot-imenu ()
   "Eglot's `imenu-create-index-function'.
 Returns a list as described in docstring of `imenu--index-alist'."
-  (let* ((res (eglot--request (eglot--current-server-or-lose)
-                              :textDocument/documentSymbol
-                              `(:textDocument
-                                ,(eglot--TextDocumentIdentifier))
-                              :cancel-on-input non-essential))
-         (head (and (cl-plusp (length res)) (elt res 0))))
-    (when head
-      (eglot--dcase head
-        (((SymbolInformation)) (eglot--imenu-SymbolInformation res))
-        (((DocumentSymbol)) (eglot--imenu-DocumentSymbol res))))))
+  (when (eglot--server-capable :textDocument/documentSymbol)
+    (let* ((res (eglot--request (eglot--current-server-or-lose)
+                                :textDocument/documentSymbol
+                                `(:textDocument
+                                  ,(eglot--TextDocumentIdentifier))
+                                :cancel-on-input non-essential))
+           (head (and (cl-plusp (length res)) (elt res 0))))
+      (when head
+        (eglot--dcase head
+          (((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."
-- 
2.41.0

Wilhelm