From: Eshel Yaron Date: Wed, 15 Jan 2025 14:41:49 +0000 (+0100) Subject: (refactor-display-edits-as-diff): Handle buffers instead of files X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=309d03a77c8fdb498384a431d0c8c76c086376b0;p=emacs.git (refactor-display-edits-as-diff): Handle buffers instead of files --- diff --git a/lisp/progmodes/refactor.el b/lisp/progmodes/refactor.el index fa654b513b2..5ffa83a7f4c 100644 --- a/lisp/progmodes/refactor.el +++ b/lisp/progmodes/refactor.el @@ -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) diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index cd028a6e6ba..4c27ac65df4 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -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))