Function: majutsu-goto-diff-line
majutsu-goto-diff-line is an interactive and natively compiled
function defined in majutsu-diff.el.
Signature
(majutsu-goto-diff-line)
Documentation
Jump to the line in the file corresponding to the diff line at point.
Key Bindings
This command is not in any keymaps.
Source Code
;; Defined in /nix/store/jagqw3nms546pad0gxi5f3wsckk6pji7-emacs-packages-deps/share/emacs/site-lisp/majutsu-diff.el
;;; Navigation
(defun majutsu-goto-diff-line ()
"Jump to the line in the file corresponding to the diff line at point."
(interactive)
(when-let* ((section (magit-current-section))
(_ (magit-section-match 'jj-hunk section))
(file (magit-section-parent-value section)))
(let* ((to-range (oref section to-range))
(start-line (and to-range (car to-range))))
(unless start-line
(let ((header (save-excursion
(goto-char (oref section start))
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))))
(when (string-match "^@@.*\\+\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?.*@@" header)
(setq start-line (string-to-number (match-string 1 header))))))
(when start-line
(let* ((start-line start-line)
;; Calculate which line within the hunk we're on
(hunk-start (oref section start))
(current-pos (point))
(line-offset 0)
(full-file-path (expand-file-name file default-directory)))
;; Count lines from hunk start to current position
(save-excursion
(goto-char hunk-start)
(forward-line 1) ; Skip hunk header
(while (< (point) current-pos)
(let ((line (buffer-substring-no-properties
(line-beginning-position) (line-end-position))))
;; Only count context and added lines for line numbering
(unless (string-prefix-p "-" line)
(setq line-offset (1+ line-offset))))
(forward-line 1)))
;; Open file and jump to calculated line
(let ((target-line (+ start-line line-offset -1))) ; -1 because we start counting from the header
(find-file full-file-path)
(goto-char (point-min))
(forward-line (max 0 target-line))
(message "Jumped to line %d in %s" (1+ target-line) file)))))))