]> git.eshelyaron.com Git - emacs.git/commitdiff
(refactor-display-edits-as-diff): Handle buffers instead of files
authorEshel Yaron <me@eshelyaron.com>
Wed, 15 Jan 2025 14:41:49 +0000 (15:41 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 15 Jan 2025 17:26:06 +0000 (18:26 +0100)
lisp/progmodes/refactor.el
lisp/vc/diff-mode.el

index fa654b513b2381b431e4e6f867dde44302f861f7..5ffa83a7f4cdfff41e603e26a2b6152908fe2ba7 100644 (file)
@@ -280,22 +280,23 @@ argument is the token corresponding to that text replacement.")
     (let ((inhibit-read-only t)
           (target (current-buffer)))
       (erase-buffer)
-      (pcase-dolist (`(,file . ,reps) edits)
-        (when (and reps file (file-readable-p file))
+      (pcase-dolist (`(,buf . ,reps) edits)
+        (when (and reps buf)
           (with-temp-buffer
             (let ((diff (current-buffer))
                   (tokens nil))
               (with-temp-buffer
-                (insert-file-contents file)
+                (insert-buffer-substring buf)
                 (let ((refactor-replacement-functions
                        (list (lambda (token) (push token tokens)))))
                   (refactor--apply-replacements reps))
-                (diff-no-select file (current-buffer) nil t diff))
+                (diff-no-select buf (current-buffer) nil t diff))
               (with-current-buffer target
                 (let ((start (point)))
                   (insert-buffer-substring diff)
                   (put-text-property start (point)
-                                     'refactor-replacement-tokens tokens))))))))
+                                     'refactor-replacement-tokens tokens)
+                  (put-text-property start (point) 'diff-new buf))))))))
     (add-hook 'diff-apply-hunk-functions
               #'refactor-run-replacement-functions-from-diff nil t)
     (buffer-enable-undo (current-buffer))
@@ -371,7 +372,8 @@ argument is the token corresponding to that text replacement.")
                    (while edits
                      (let ((buffer-reps (car edits)))
                        (refactor--query-apply-buffer-reps
-                        (car buffer-reps) (cdr buffer-reps))
+                        (car buffer-reps)
+                        (sort (cdr buffer-reps) :key #'cadr))
                        (setq edits (cdr edits)))))
             (`(all . ,reps)
              (setcdr (car edits) reps)
index cd028a6e6ba85a8f584d17f0ff12e75e6e8ddeb0..4c27ac65df4aa4287548704ba171c9de2451d348 100644 (file)
@@ -1969,20 +1969,21 @@ SWITCHED is non-nil if the patch is already applied."
                                 diff-context-mid-hunk-header-re nil t)
                         (error "Can't find the hunk separator"))
                       (match-string 1)))))
-          (file (or (diff-find-file-name other noprompt)
-                     (error "Can't find the file")))
-          (revision (and other diff-vc-backend
-                          (if reverse (nth 1 diff-vc-revisions)
-                            (or (nth 0 diff-vc-revisions)
-                                ;; When diff shows changes in working revision
-                                (vc-working-revision file)))))
-          (buf (if revision
-                    (let ((vc-find-revision-no-save t))
-                      (vc-find-revision (expand-file-name file) revision diff-vc-backend))
-                  ;; NOPROMPT is only non-nil when called from
-                  ;; `which-function-mode', so avoid "File x changed
-                  ;; on disk. Reread from disk?" warnings.
-                  (find-file-noselect file noprompt))))
+          (buf (or (get-text-property (point) (if other 'diff-old 'diff-new))
+                    (let* ((file (or (diff-find-file-name other noprompt)
+                                     (error "Can't find the file")))
+                          (revision (and other diff-vc-backend
+                                          (if reverse (nth 1 diff-vc-revisions)
+                                            (or (nth 0 diff-vc-revisions)
+                                                ;; When diff shows changes in working revision
+                                                (vc-working-revision file))))))
+                      (if revision
+                          (let ((vc-find-revision-no-save t))
+                            (vc-find-revision (expand-file-name file) revision diff-vc-backend))
+                        ;; NOPROMPT is only non-nil when called from
+                        ;; `which-function-mode', so avoid "File x changed
+                        ;; on disk. Reread from disk?" warnings.
+                        (find-file-noselect file noprompt))))))
       ;; Update the user preference if he so wished.
       (when (> (prefix-numeric-value other-file) 8)
        (setq diff-jump-to-old-file other))