From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Visuwesh Newsgroups: gmane.emacs.bugs Subject: bug#28407: 26.0.50; xref should use imenu Date: Sun, 15 May 2022 18:02:14 +0530 Message-ID: <87y1z35630.fsf@gmail.com> References: <87h8wa8quw.fsf@bapiya> <3238a206-240a-aa76-87c0-bcb3bdfa00dc@yandex.ru> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39740"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: Tom Tromey , 28407@debbugs.gnu.org To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun May 15 14:34:26 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nqDS5-000ABr-I0 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 15 May 2022 14:34:25 +0200 Original-Received: from localhost ([::1]:60342 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nqDS4-0001GH-5c for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 15 May 2022 08:34:24 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48286) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqDRi-0001Fe-JF for bug-gnu-emacs@gnu.org; Sun, 15 May 2022 08:34:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54844) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nqDRi-000123-9c for bug-gnu-emacs@gnu.org; Sun, 15 May 2022 08:34:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nqDRi-00010Z-0i for bug-gnu-emacs@gnu.org; Sun, 15 May 2022 08:34:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Visuwesh Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 15 May 2022 12:34:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 28407 X-GNU-PR-Package: emacs Original-Received: via spool by 28407-submit@debbugs.gnu.org id=B28407.16526179953811 (code B ref 28407); Sun, 15 May 2022 12:34:01 +0000 Original-Received: (at 28407) by debbugs.gnu.org; 15 May 2022 12:33:15 +0000 Original-Received: from localhost ([127.0.0.1]:48741 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nqDQx-0000zO-DD for submit@debbugs.gnu.org; Sun, 15 May 2022 08:33:15 -0400 Original-Received: from mail-pl1-f193.google.com ([209.85.214.193]:44955) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nqDQv-0000z9-QO for 28407@debbugs.gnu.org; Sun, 15 May 2022 08:33:14 -0400 Original-Received: by mail-pl1-f193.google.com with SMTP id q4so11989570plr.11 for <28407@debbugs.gnu.org>; Sun, 15 May 2022 05:33:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:in-reply-to:date:lines:references:user-agent :message-id:mime-version; bh=o4GMPC0dWDpVKCE2Ociv5/k7qrTiknbafxgnkzx4fws=; b=KFP6oweDM3MZra6RX2vktLX7dicJ/pp5p1DiUhK7sKteQEa57yHTjvFl8VL0EBwICW Ouha4wEiO4pBr27rrbaU5Nhc2XwC4BgD5rDPneAowAikrUMZNMN+J8VtSyFsP/eqpnyW wnQU6OPJb6d0CRPwyAE/wKdMo5rJkyZEaiOYoo+QBYjsR3HLjCSV1T/rel0GR4hr2tTX zXp4B/mkzpspETKxaoUBB+Yf4PjEaQdPd6rtmZmB3vXUrCGgWR56iUKLjnm5rpDHHUVl M7DQa3JyRhHjyz/XBFGglTKK324eZPJJ6Eih870d6iBkcDaQ/mLmCdc7RjVowhYiCtfp kQTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:date:lines :references:user-agent:message-id:mime-version; bh=o4GMPC0dWDpVKCE2Ociv5/k7qrTiknbafxgnkzx4fws=; b=0yQw4uft7fG5GDZ8GVeUBtFKWMBpa3UDlOQCns0NU2EaNFI4ZSLSfb6zMhJ3mOYBdj eC8an4docfl5epyFpV/ZZRCpm489uUHKT9K7y4yOA3f1nB2O4ghWzDo99aw9MLSDVapw cfhympIR4OfkNLPEmjYHzou0CXs0Lp5BF4ZNOGD/j+mGtxx1yWKZVw+lgoHSS3igSp3o R3kVk9o//N6PDZKL9ZXvoiIs1nsj7cwk9QlV2km6O4MKGujIxbbxqX98Jsaf3bfdfJzV PjrFq0Y2ZvpLIZ2yDPU7fr6xln5LAXvPPOLJsB+B9OiWJrKcgDaC8GArH/Sb9oajgDVw DLOg== X-Gm-Message-State: AOAM532u5ftO0ZwbabkhHpw42YPt84n6x7fXugC8ezCpxigA8lmIMcRD gZRK21a6URqUar4nKO5P5R4= X-Google-Smtp-Source: ABdhPJwp6X3Yd8F3ghmq1duSji5/PKZ5SnH/MK241kJSXytvh3p0vLThqW22BcNzHxKn1UGnSU9+Aw== X-Received: by 2002:a17:90a:2809:b0:1df:35ca:2e6a with SMTP id e9-20020a17090a280900b001df35ca2e6amr3624933pjd.8.1652617987725; Sun, 15 May 2022 05:33:07 -0700 (PDT) Original-Received: from localhost ([49.204.115.240]) by smtp.gmail.com with ESMTPSA id d17-20020aa78691000000b0050dc76281c6sm5040310pfo.160.2022.05.15.05.33.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 May 2022 05:33:07 -0700 (PDT) In-Reply-To: <3238a206-240a-aa76-87c0-bcb3bdfa00dc@yandex.ru> (Dmitry Gutov's message of "Mon, 11 Sep 2017 00:35:56 +0300") Original-Lines: 151 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:232308 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable [=E0=AE=A4=E0=AE=BF=E0=AE=99=E0=AF=8D=E0=AE=95=E0=AE=B3=E0=AF=8D =E0=AE=9A= =E0=AF=86=E0=AE=AA=E0=AF=8D=E0=AE=9F=E0=AE=AE=E0=AF=8D=E0=AE=AA=E0=AE=B0=E0= =AF=8D 11, 2017] Dmitry Gutov wrote: > On 9/10/17 7:23 PM, Tom Tromey wrote: >> It would be nice if imenu were a back end for xref. >> Then M-. could also use symbols found by imenu. >> A further wrinkle on this would be if xref, project, and imenu >> worked >> together, so that M-. would automatically know to look at imenu results >> for other buffers in the same project. > > Agreed. It could be a nice default for when no tags table is currently > visited. I tried to write a general imenu backend in attached file (extracted from my init.el) but I hit quite a few roadblocks, 1. I activate the imenu backend iff there are no tags table defined for the buffer but this means that one cannot use the imenu backend to jump to definitions for symbols that TAGS do not know of currently. I can think of two ways to solve this problem, (a) Check if the symbol is in TAGS table. (b) Modify the etags backend so that the user can say "I have no TAGS table for this file/project/whatever." (a) is definitely not clean, and (b) sounds feasible but similar situation can also exist with other backends (like elisp). I'm lost on how to solve this problem. 2. I have not defined all the methods and the completion-table does not handle the nested case of the index alist. AFAIU from `(elisp) Programmed Completion', completion "ends" when `try-completion' returns t but I seem to be mistaken. I have to rewrite completion-table to be like `imenu--completion-buffer' but I don't know how to pull that off. 3. `imenu-xref--in-alist' is mostly a 1-1 copy of `imenu--in-alist' with the only difference being my function returns all matches of the symbol instead of just the first one. This should be easy enough to fix by adding an optional argument INCLUDE-ALL to `imenu--in-alist'. I'm testing in python-mode with the following settings, (setq imenu-name-lookup-function (lambda (symbol item) (string-prefix-p= symbol item)) python-imenu-format-parent-item-jump-label-function (lambda (_ na= me) name)) --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=imenu-xref.el Content-Transfer-Encoding: quoted-printable (defun imenu-xref-backend () (when (not tags-table-files) 'imenu)) (cl-defmethod xref-backend-identifier-at-point ((_backend (eql 'imenu))) (or (thing-at-point 'symbol) (find-tag--default))) (defun imenu-xref--in-alist (symbol alist) "Find all positions that match SYMBOL in the imenu index ALIST." (let (elt head tail res) (while alist (setq elt (car alist) tail (cdr elt) alist (cdr alist) head (car elt)) ;; A nested ALIST element looks like ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...) ;; while a bottom-level element looks like ;; (INDEX-NAME . INDEX-POSITION) ;; or ;; (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...) ;; We are only interested in the bottom-level elements, so we need to ;; recurse if TAIL is a nested ALIST. (cond ((imenu--subalist-p elt) (when-let ((item (imenu-xref--in-alist symbol tail))) (if (listp item) (setq res (append res item)) (push item res)))) ((if imenu-name-lookup-function (funcall imenu-name-lookup-function symbol head) (equal symbol head)) (push elt res)))) res)) (defun imenu-xref--make-summary (sym marker) (with-current-buffer (marker-buffer marker) (save-excursion (goto-char marker) (back-to-indentation) (buffer-substring (point) (point-at-eol))))) (cl-defmethod xref-backend-definitions ((_backend (eql 'imenu)) symbol) (unless imenu--index-alist (imenu--make-index-alist)) (let ((res (imenu-xref--in-alist symbol imenu--index-alist)) defs) (pcase-dolist (`(,sym . ,m) res) (push (xref-make (imenu-xref--make-summary sym m) (xref-make-buffer-location (marker-buffer m) (marker= -position m))) defs)) defs)) (cl-defmethod xref-backend-identifier-completion-ignore-case ((_backend (eq= l 'imenu))) completion-ignore-case) (defun imenu-xref--flatten (alist &optional prefix) (let (res) (dolist (item alist) (if (imenu--subalist-p (cdr item)) (setq res (append res (imenu-xref--flatten (cdr item) (concat prefix (when prefix imenu-level-se= parator) (car item))))) (push (cons (concat prefix (when prefix imenu-level-separator) (car= item)) (cdr item)) res))) res)) (cl-defmethod xref-backend-identifier-completion-table ((_backend (eql 'ime= nu))) (unless imenu--index-alist (imenu--make-index-alist)) (let ((collection (imenu-xref--flatten imenu--index-alist))) (lambda (string pred action) (cond ((null action) (if-let* ((completion (try-completion string collection pred)) ((eq t completion))) (string-trim-left string ".+:") completion)) (t (complete-with-action action collection string pred)))))) (add-hook 'xref-backend-functions #'imenu-xref-backend) --=-=-=--