unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dgutov@yandex.ru>
To: "Daniel Martín" <mardani29@yahoo.es>, 60285@debbugs.gnu.org
Subject: bug#60285: xref-query-replace-in-results could show an arrow pointing to the current item being replaced
Date: Sat, 24 Dec 2022 19:45:01 +0200	[thread overview]
Message-ID: <bbbdd369-f977-b688-b706-f57b4093bcae@yandex.ru> (raw)
In-Reply-To: <m15ye2j5h3.fsf@yahoo.es>

[-- Attachment #1: Type: text/plain, Size: 631 bytes --]

On 23/12/2022 21:16, Daniel Martín via Bug reports for GNU Emacs, the 
Swiss army knife of text editors wrote:
> Severity: wishlist
> 
> When you use xref-query-replace-in-results to replace some references,
> it'd be great if the xref buffer showed an overlay arrow (see
> xref--set-arrow) pointing to the reference being edited.  That'd give
> better context about the reference that is being edited and the global
> progress of the query replace operation.

SGTM.

Here's a patch which seems to work. It's a little tricky, though, and 
I'm at a loss with better variable names in some places.

Wish we had some unit tests here.

[-- Attachment #2: xref-replace-with-arrow.diff --]
[-- Type: text/x-patch, Size: 4256 bytes --]

diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 0213ab3cc58..49ad3500d15 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -771,19 +771,22 @@ xref-query-replace-in-results
                       (format "Query-replace (regexp) %s with: " fr)
                     "Query-replace all matches with: ")))
      (list fr (read-regexp prompt))))
-  (let* (item xrefs iter)
+  (let* ((positions (make-hash-table :test 'eq))
+         item xrefs iter)
     (save-excursion
       (while (setq item (xref--search-property 'xref-item))
         (when (xref-match-length item)
+          (puthash item (point) positions)
           (push item xrefs))))
     (unwind-protect
         (progn
           (goto-char (point-min))
-          (setq iter (xref--buf-pairs-iterator (nreverse xrefs)))
-          (xref--query-replace-1 from to iter))
+          (setq iter (xref--buf-pairs-iterator (nreverse xrefs)
+                                               positions))
+          (xref--query-replace-1 from to iter (current-buffer)))
       (funcall iter :cleanup))))
 
-(defun xref--buf-pairs-iterator (xrefs)
+(defun xref--buf-pairs-iterator (xrefs positions)
   (let (chunk-done item next-pair file-buf pairs all-pairs)
     (lambda (action)
       (pcase action
@@ -801,9 +804,10 @@ xref--buf-pairs-iterator
                       (beg (xref-location-marker loc))
                       (end (move-marker (make-marker)
                                         (+ beg (xref-match-length item))
-                                        (marker-buffer beg))))
-                 (let ((pair (cons beg end)))
-                   (push pair all-pairs)
+                                        (marker-buffer beg)))
+                      (xref-buf-pos (gethash item positions)))
+                 (let ((positions (list beg end xref-buf-pos)))
+                   (push positions all-pairs)
                    ;; Perform sanity check first.
                    (xref--goto-location loc)
                    (if (xref--outdated-p item)
@@ -811,17 +815,17 @@ xref--buf-pairs-iterator
                      (cond
                       ((null file-buf)
                        (setq file-buf (marker-buffer beg))
-                       (push pair pairs))
+                       (push positions pairs))
                       ((equal file-buf (marker-buffer beg))
-                       (push pair pairs))
+                       (push positions pairs))
                       (t
                        (setq chunk-done t
-                             next-pair pair))))))))
+                             next-pair positions))))))))
            (cons file-buf (nreverse pairs))))
         (:cleanup
          (dolist (pair all-pairs)
-           (move-marker (car pair) nil)
-           (move-marker (cdr pair) nil)))))))
+           (move-marker (nth 0 pair) nil)
+           (move-marker (nth 1 pair) nil)))))))
 
 (defun xref--outdated-p (item)
   "Check that the match location at current position is up-to-date.
@@ -857,7 +861,7 @@ xref--outdated-p
              (funcall check)))))))
 
 ;; FIXME: Write a nicer UI.
-(defun xref--query-replace-1 (from to iter)
+(defun xref--query-replace-1 (from to iter xref-buf)
   (let* ((query-replace-lazy-highlight nil)
          (continue t)
          did-it-once buf-pairs pairs
@@ -872,11 +876,15 @@ xref--query-replace-1
                  (<= end current-end))))
          (replace-re-search-function
           (lambda (from &optional _bound noerror)
-            (let (found pair)
+            (let (found positions)
               (while (and (not found) pairs)
-                (setq pair (pop pairs)
-                      current-beg (car pair)
-                      current-end (cdr pair))
+                (setq positions (pop pairs)
+                      current-beg (nth 0 positions)
+                      current-end (nth 1 positions))
+                (when (buffer-live-p xref-buf)
+                  (with-current-buffer xref-buf
+                    (goto-char (nth 2 positions))
+                    (xref--set-arrow)))
                 (goto-char current-beg)
                 (when (re-search-forward from current-end noerror)
                   (setq found t)))

  reply	other threads:[~2022-12-24 17:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <m15ye2j5h3.fsf.ref@yahoo.es>
2022-12-23 19:16 ` bug#60285: xref-query-replace-in-results could show an arrow pointing to the current item being replaced Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-12-24 17:45   ` Dmitry Gutov [this message]
2023-01-01 11:23     ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-04 23:48       ` Dmitry Gutov
2023-01-05 17:55         ` Juri Linkov
2023-01-16  1:41           ` Dmitry Gutov
2023-01-16 17:39             ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bbbdd369-f977-b688-b706-f57b4093bcae@yandex.ru \
    --to=dgutov@yandex.ru \
    --cc=60285@debbugs.gnu.org \
    --cc=mardani29@yahoo.es \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).