Skip to content

Commit

Permalink
diff: Fix visiting removed lines of renamed file
Browse files Browse the repository at this point in the history
Add a new internal function to do so.  Changing how
`magit-file-at-point' behaves might have negatively affect
other callers and would lead to a circular dependency.

Closes #4922.
  • Loading branch information
tarsius committed Apr 22, 2023
1 parent b2c4a26 commit dd649ba
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lisp/magit-diff.el
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,7 @@ to the line that point is on in the diff.
Note that this command only works if point is inside a diff.
In other cases `magit-find-file' (which see) has to be used."
(interactive (list (magit-file-at-point t t) current-prefix-arg))
(interactive (list (magit-diff--file-at-point t t) current-prefix-arg))
(magit-diff-visit-file--internal file nil
(if other-window
#'switch-to-buffer-other-window
Expand All @@ -1560,14 +1560,14 @@ In other cases `magit-find-file' (which see) has to be used."
"From a diff visit the appropriate version of FILE in another window.
Like `magit-diff-visit-file' but use
`switch-to-buffer-other-window'."
(interactive (list (magit-file-at-point t t)))
(interactive (list (magit-diff--file-at-point t t)))
(magit-diff-visit-file--internal file nil #'switch-to-buffer-other-window))

(defun magit-diff-visit-file-other-frame (file)
"From a diff visit the appropriate version of FILE in another frame.
Like `magit-diff-visit-file' but use
`switch-to-buffer-other-frame'."
(interactive (list (magit-file-at-point t t)))
(interactive (list (magit-diff--file-at-point t t)))
(magit-diff-visit-file--internal file nil #'switch-to-buffer-other-frame))

;;;;; Worktree Variants
Expand Down Expand Up @@ -1655,7 +1655,7 @@ the Magit-Status buffer for DIRECTORY."

(defun magit-diff-visit-file--noselect (&optional file goto-worktree)
(unless file
(setq file (magit-file-at-point t t)))
(setq file (magit-diff--file-at-point t t)))
(let* ((hunk (magit-diff-visit--hunk))
(goto-from (and hunk
(magit-diff-visit--goto-from-p hunk goto-worktree)))
Expand Down Expand Up @@ -1688,6 +1688,22 @@ the Magit-Status buffer for DIRECTORY."
(point))))
(list buf nil))))

(defun magit-diff--file-at-point (&optional expand assert)
;; This is a variation of magit-file-at-point.
(if-let* ((file-section (magit-section-case
(file it)
(hunk (oref it parent))))
(file (if (and (magit-section-match 'hunk)
(magit-diff-visit--goto-from-p
(magit-current-section) nil))
(oref file-section source)
(oref file-section value))))
(if expand
(expand-file-name file (magit-toplevel))
file)
(when assert
(user-error "No file at point"))))

(defun magit-diff-visit--hunk ()
(when-let* ((scope (magit-diff-scope)) ;debbugs#31840
(section (magit-current-section)))
Expand Down

0 comments on commit dd649ba

Please sign in to comment.