From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Dmitry Gutov Newsgroups: gmane.emacs.bugs Subject: bug#45135: 28.0.50; [PATCH] Add option to print file names in *xref* buffer relative to project root Date: Sat, 26 Dec 2020 04:21:47 +0200 Message-ID: References: <6d6911b9-c604-1fca-3cd4-084a8880524f@yandex.ru> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------A3153525052BE6471F2AA67D" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16707"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 To: Tobias Rittweiler , 45135@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Dec 26 03:22:11 2020 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 1kszDf-0004Ex-CJ for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 26 Dec 2020 03:22:11 +0100 Original-Received: from localhost ([::1]:34252 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kszDd-0001L1-TG for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 25 Dec 2020 21:22:09 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53244) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kszDW-0001Ku-S0 for bug-gnu-emacs@gnu.org; Fri, 25 Dec 2020 21:22:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:47010) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kszDW-0000Li-Ku for bug-gnu-emacs@gnu.org; Fri, 25 Dec 2020 21:22:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kszDW-0007W3-Fd for bug-gnu-emacs@gnu.org; Fri, 25 Dec 2020 21:22:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Dmitry Gutov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 26 Dec 2020 02:22:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45135 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 45135-submit@debbugs.gnu.org id=B45135.160894931928881 (code B ref 45135); Sat, 26 Dec 2020 02:22:02 +0000 Original-Received: (at 45135) by debbugs.gnu.org; 26 Dec 2020 02:21:59 +0000 Original-Received: from localhost ([127.0.0.1]:58556 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kszDS-0007Vk-Ri for submit@debbugs.gnu.org; Fri, 25 Dec 2020 21:21:59 -0500 Original-Received: from mail-ej1-f53.google.com ([209.85.218.53]:45536) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kszDQ-0007VS-OR for 45135@debbugs.gnu.org; Fri, 25 Dec 2020 21:21:57 -0500 Original-Received: by mail-ej1-f53.google.com with SMTP id qw4so7635749ejb.12 for <45135@debbugs.gnu.org>; Fri, 25 Dec 2020 18:21:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:from:to:references:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=1v5Xjg1DgWCqpqOJYgoy7JLEBXPlcztN5qTsi9sqEGo=; b=r+JwEoCmt3bs1HVZ73LAl+OScPC9lNllwQUEMXkjVV3qOGfaBim5KJAtDhedPL30W4 WVRt11J2glVVZpQa76Fk64e99Xd4e0x3bcLGJT3JDJOYJiLYbXp5ytCM1QIkMffiPPG8 iIG9P01Om9AjffvhL4Vq8eqdFYnfi8UPB4DJN9/cUsxGqOFk5mh22Csi1D72dcLB77Ef Tw2ufGjcqrT6OGnL9Tmu3yI5y5wyYREmt5eYafi+mcdXyxd6vBGFoqTtMsV5gp/Ca3DV xcB5zBVKEV8u+8Ox0wgmGYjuf9TPc22mOEE4A9T5aY9SE76/Qon4aptOtNxHygBeEbnN IReA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:from:to:references:message-id :date:user-agent:mime-version:in-reply-to:content-language; bh=1v5Xjg1DgWCqpqOJYgoy7JLEBXPlcztN5qTsi9sqEGo=; b=BsH0YlZ4wsw7z0m4ZSnXTwhiBtJ5gDNfE/XA48nG1n9LBuXOwrk9yzU4NIisQF+u2Z M4vjkjsZqA/PYzaZ/4KbNDWz2cL48LT2Qv2oCE1eD4ypCPX/guhyA6o0vmjBeCys/TJa DRp2kFhwQ/nUi5OWjSA3bmA7TLx391za8YV0gSEh7JGNC5+n1Nbvvkz8L8i+MEJS61EG CeSeW5JY4dZYP+ZOWDY8HwFDGqlqmyF2GRh6rOUAYaDQB1EIAfoj9r2y/3x/7cUlFMhU /VpaA1H6y21V3EVzBVlomb8el+9kfDZMdUp48Nj+X0NFn1mEACVEoyz1u94Y8Xik/4lk 3BJA== X-Gm-Message-State: AOAM5326Py7a0BzM9iTaErOPViio8zRaS2QUUda0Dhg/SG1xQT7V3lbD jVyKdWfhdKTj41VCumI8uKEC7KNQ/loARw== X-Google-Smtp-Source: ABdhPJyQ8me5QmUkQgd90U4H60KVm1xSNvWilg7/k651cwdt6ccn5DzXI6ax+LxEvx6MuU/V/wXuxw== X-Received: by 2002:a17:906:c45a:: with SMTP id ck26mr33651996ejb.200.1608949310431; Fri, 25 Dec 2020 18:21:50 -0800 (PST) Original-Received: from [192.168.0.6] ([46.251.119.176]) by smtp.googlemail.com with ESMTPSA id q2sm27310800edv.93.2020.12.25.18.21.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 25 Dec 2020 18:21:49 -0800 (PST) In-Reply-To: <6d6911b9-c604-1fca-3cd4-084a8880524f@yandex.ru> Content-Language: en-US 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:196743 Archived-At: This is a multi-part message in MIME format. --------------A3153525052BE6471F2AA67D Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 23.12.2020 04:16, Dmitry Gutov wrote: > Re: patch 2, I have an idea that would make it a bit simpler and less > coupled to project.el, but behave the same in the usual cases. Stay tuned. Not as clean as I hoped, but here's something that should work just as well, and a lot faster to boot (project-current lookup for every file name is slow when there are a lot of matches, even if each individual project-current call looks fast). We could remove the explicit dependency on the "current project" if it was always passed in from the outside (e.g. by project-find-regexp's setup), but we probably want this display mode to work with xref-find-references as well. Also simplified the docstring a bit. Tobias, please try the attached patch. --------------A3153525052BE6471F2AA67D Content-Type: text/x-patch; charset=UTF-8; name="xref-file-name-display-relative.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="xref-file-name-display-relative.diff" diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el index 181f94b0bc..d7f9fd91f5 100644 --- a/lisp/progmodes/xref.el +++ b/lisp/progmodes/xref.el @@ -109,12 +109,20 @@ xref-match-length (defcustom xref-file-name-display 'abs "Style of file name display in *xref* buffers. + If the value is the symbol `abs', the default, show the file names in their full absolute form. + If `nondirectory', show only the nondirectory (a.k.a. \"base name\") -part of the file name." +part of the file name. + +If `relative', show only the path fragment relative to the +current project root. If there is no current project, or if the +file name resides outside of its root, show that particular file +in its full absolute form." :type '(choice (const :tag "absolute file name" abs) - (const :tag "nondirectory file name" nondirectory)) + (const :tag "nondirectory file name" nondirectory) + (const :tag "relative to project root" relative)) :version "27.1") ;; FIXME: might be useful to have an optional "hint" i.e. a string to @@ -149,10 +157,21 @@ xref-location-marker (forward-char column)) (point-marker)))))) +(defvar-local xref--search-root nil + "The current search root.") + (cl-defmethod xref-location-group ((l xref-file-location)) (cl-ecase xref-file-name-display - (abs (oref l file)) - (nondirectory (file-name-nondirectory (oref l file))))) + (abs + (oref l file)) + (nondirectory + (file-name-nondirectory (oref l file))) + (relative + (let ((file (oref l file))) + (if (and xref--search-root + (string-prefix-p xref--search-root file)) + (substring file (length xref--search-root)) + file))))) (defclass xref-buffer-location (xref-location) ((buffer :type buffer :initarg :buffer) @@ -273,10 +292,7 @@ xref-backend-references (xref-references-in-directory identifier dir)) (let ((pr (project-current t))) (cons - (if (fboundp 'project-root) - (project-root pr) - (with-no-warnings - (project-roots pr))) + (xref--project-root pr) (project-external-roots pr))))) (cl-defgeneric xref-backend-apropos (backend pattern) @@ -902,26 +918,39 @@ xref--analyze (defun xref--show-xref-buffer (fetcher alist) (cl-assert (functionp fetcher)) - (let* ((xrefs - (or - (assoc-default 'fetched-xrefs alist) - (funcall fetcher))) - (xref-alist (xref--analyze xrefs))) + (let ((xrefs + (or + (assoc-default 'fetched-xrefs alist) + (funcall fetcher)))) (with-current-buffer (get-buffer-create xref-buffer-name) (xref--xref-buffer-mode) - (xref--show-common-initialize xref-alist fetcher alist) + (xref--show-common-initialize xrefs fetcher alist) (pop-to-buffer (current-buffer)) (current-buffer)))) -(defun xref--show-common-initialize (xref-alist fetcher alist) +(defun xref--project-root (project) + (if (fboundp 'project-root) + (project-root project) + (with-no-warnings + (project-roots project)))) + +(defun xref--expanded-seach-root (alist) + (let ((root + (or (assoc-default 'search-root alist) + (let ((pr (project-current))) + (and pr (xref--project-root pr)))))) + (and root (expand-file-name root)))) + +(defun xref--show-common-initialize (xrefs fetcher alist) (setq buffer-undo-list nil) (let ((inhibit-read-only t) (buffer-undo-list t)) + (setq xref--original-window (assoc-default 'window alist) + xref--original-window-intent (assoc-default 'display-action alist) + xref--search-root (xref--expanded-seach-root alist)) (erase-buffer) - (xref--insert-xrefs xref-alist) (goto-char (point-min)) - (setq xref--original-window (assoc-default 'window alist) - xref--original-window-intent (assoc-default 'display-action alist)) + (xref--insert-xrefs (xref--analyze xrefs)) (setq xref--fetcher fetcher))) (defun xref-revert-buffer () @@ -965,7 +994,7 @@ xref--show-defs-buffer-at-bottom (t (with-current-buffer (get-buffer-create xref-buffer-name) (xref--transient-buffer-mode) - (xref--show-common-initialize (xref--analyze xrefs) fetcher alist) + (xref--show-common-initialize xrefs fetcher alist) (pop-to-buffer (current-buffer) '(display-buffer-in-direction . ((direction . below)))) (current-buffer)))))) --------------A3153525052BE6471F2AA67D--