From 57b35d302494a0f32fcdc43be1673b269b5fa2c3 Mon Sep 17 00:00:00 2001 From: Mohsin Kaleem Date: Mon, 14 Aug 2023 23:21:19 +0100 Subject: [PATCH] eglot: Improve imenu DocumentSymbol handler --- init.org | 60 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/init.org b/init.org index d762376..70d5f2c 100644 --- a/init.org +++ b/init.org @@ -12972,8 +12972,8 @@ ("Array" ?A font-lock-variable-name-face) ("Boolean" ?B font-lock-variable-name-face) - ("Enum Member" ?E font-lock-variable-name-face) - ("Enum Member" ?B font-lock-variable-name-face) + ("EnumMember" ?E font-lock-variable-name-face) + ("EnumMember" ?B font-lock-variable-name-face) ("Number" ?N font-lock-variable-name-face) ("Object" ?O font-lock-variable-name-face) ("String" ?S font-lock-variable-name-face) @@ -13055,26 +13055,27 @@ (defun consult-imenu-setup-lsp+ () (consult-imenu-override-types+ - "Class" - "Struct" - "Interface" - "Module" - "Function" - "Enum" - "Namespace" - "Package" - "Type Parameter" - "Variable" "Array" "Boolean" + "Class" "Constant" - "Enum Member" + "Constructor" + "EnumMember" + "Enum" "Field" + "Function" + "Interface" "Method" + "Module" + "Namespace" "Number" "Object" + "Package" "Property" - "String")) + "String" + "Struct" + "Type Parameter" + "Variable")) :hook ((eglot-managed-mode @@ -14744,6 +14745,37 @@ (eglot-inlay-hints-mode -1)) :hook (eglot-managed-mode . eglot-turn-off-inlay-hints-mode+) + :autoload eglot--imenu-DocumentSymbol+ + :preface (advice-add 'eglot--imenu-DocumentSymbol :override #'eglot--imenu-DocumentSymbol+) + :config + (defun eglot--imenu-DocumentSymbol+ (res) + "Compute `imenu--index-alist' for RES vector of DocumentSymbol." + (cl-labels ((flatmap (function sequence) + (let ((result nil)) + (dolist (it (apply #'append (mapcar function sequence))) + (if-let ((existing-group (alist-get (car it) result))) + (setcdr existing-group (append (cdr existing-group) (cdr it))) + (push (cons (car it) (cdr it)) result))) + (nreverse result))) + (map-parent-group-prefix (name item) + (cons (car item) (list (append (list name) (cdr item))))) + (dfs (&key name children range kind &allow-other-keys) + (let ((reg (eglot--range-region range))) + (if (seq-empty-p children) + `((,kind (,name . ,(car reg)))) + (let ((result + (mapcar (apply-partially #'map-parent-group-prefix name) + (flatmap (lambda (c) (apply #'dfs c)) children))) + (parent-entry `((,name . ,(car reg))))) + (if-let ((existing-group (alist-get kind result))) + (setcdr existing-group (append parent-entry (cdr existing-group))) + (push (cons kind parent-entry) result)) + result)))) + (map-kind-names (item) + (cons (alist-get (car item) eglot--symbol-kind-names "Unknown") + (cdr item)))) + (mapcar #'map-kind-names (flatmap (lambda (c) (apply #'dfs c)) res)))) + :leader (:prefix leader-server-leader-prefix "l" 'eglot