Skip to content

Commit f9333cd

Browse files
committed
move show-hink-inline related code to prevent circular dependency
1 parent 461b952 commit f9333cd

2 files changed

Lines changed: 73 additions & 76 deletions

File tree

diff-hl-show-hunk-inline.el

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@
4646
(make-variable-buffer-local 'diff-hl-show-hunk-inline--height)
4747
(make-variable-buffer-local 'diff-hl-show-hunk-inline--close-hook)
4848

49+
(defcustom diff-hl-show-hunk-inline-hide-hunk nil
50+
"If t, inline-popup is shown over the hunk, hiding it."
51+
:type 'boolean)
52+
53+
(defcustom diff-hl-show-hunk-inline-smart-lines t
54+
"If t, inline-popup tries to show only the deleted lines of the
55+
hunk. The added lines are shown when scrolling the popup. If
56+
the hunk consist only on added lines, then
57+
`diff-hl-show-hunk--no-lines-removed-message' it is shown."
58+
:type 'boolean)
59+
4960
(defun diff-hl-show-hunk-inline--splice (list offset length)
5061
"Compute a sublist of LIST starting at OFFSET, of LENGTH."
5162
(butlast
@@ -285,5 +296,67 @@ is closed."
285296
(dolist (o overlays)
286297
(delete-overlay o))))
287298

299+
;;;###autoload
300+
(defun diff-hl-show-hunk-inline (buffer &optional _ignored-line)
301+
"Implementation to show the hunk in a inline popup.
302+
BUFFER is a buffer with the hunk."
303+
;; prevent diff-hl-show-hunk-inline-hide from being called twice
304+
(let ((diff-hl-show-hunk-inline--close-hook nil))
305+
(diff-hl-show-hunk-inline-hide))
306+
(setq diff-hl-show-hunk--hide-function #'diff-hl-show-hunk-inline-hide)
307+
(let* ((lines (split-string (with-current-buffer buffer (buffer-string)) "[\n\r]+" ))
308+
(smart-lines diff-hl-show-hunk-inline-smart-lines)
309+
(original-lines-number (cl-count-if (lambda (s) (string-prefix-p "-" s)) lines))
310+
(lines (if (string= (car (last lines)) "" ) (butlast lines) lines))
311+
(lines (if (and (eq original-lines-number 0) smart-lines)
312+
diff-hl-show-hunk--no-lines-removed-message
313+
lines))
314+
(overlay diff-hl-show-hunk--original-overlay)
315+
(type (overlay-get overlay 'diff-hl-hunk-type))
316+
(point (if (eq type 'delete) (overlay-start overlay) (overlay-end overlay)))
317+
(propertize-line (lambda (l)
318+
(propertize l 'face
319+
(cond ((string-prefix-p "+" l)
320+
'diff-added)
321+
((string-prefix-p "-" l)
322+
'diff-removed)))))
323+
(propertized-lines (mapcar propertize-line lines)))
324+
325+
(save-excursion
326+
;; Save point in case the hunk is hidden, so next/previous works as expected
327+
;; If the hunk is delete type, then don't hide the hunk
328+
;; (because the hunk is located in a non deleted line)
329+
(when (and diff-hl-show-hunk-inline-hide-hunk
330+
(not (eq type 'delete)))
331+
(let* ((invisible-overlay (make-overlay (overlay-start overlay)
332+
(overlay-end overlay))))
333+
;; Make new overlay, since the diff-hl overlay can be changed by diff-hl-flydiff
334+
(overlay-put invisible-overlay 'invisible t)
335+
;; Change default hide popup function, to make the overlay visible
336+
(setq diff-hl-show-hunk--hide-function
337+
(lambda ()
338+
(overlay-put invisible-overlay 'invisible nil)
339+
(delete-overlay invisible-overlay)
340+
(diff-hl-show-hunk-inline-hide)))))
341+
(diff-hl-show-hunk--goto-hunk-overlay overlay)
342+
(let ((height
343+
(when smart-lines
344+
(when (not (eq 0 original-lines-number))
345+
original-lines-number)))
346+
(footer "(q)Quit (p)Previous (n)Next (r)Revert (c)Copy original"))
347+
(unless diff-hl-show-staged-changes
348+
(setq footer (concat footer " (S)Stage")))
349+
(diff-hl-show-hunk-inline-show
350+
propertized-lines
351+
(if (and (boundp 'diff-hl-reference-revision) diff-hl-reference-revision)
352+
(concat "Diff with " diff-hl-reference-revision)
353+
"Diff with HEAD")
354+
footer
355+
diff-hl-show-hunk-map
356+
#'diff-hl-show-hunk-hide
357+
point
358+
height))
359+
)))
360+
288361
(provide 'diff-hl-show-hunk-inline)
289362
;;; diff-hl-show-hunk-inline ends here

diff-hl-show-hunk.el

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
;;; Code:
3838

39-
(require 'diff-hl-show-hunk-inline)
4039
(require 'diff-hl)
4140

4241
(defvar diff-hl-show-hunk-mouse-mode-map
@@ -73,17 +72,6 @@
7372
(defconst diff-hl-show-hunk-boundary "^@@.*@@")
7473
(defconst diff-hl-show-hunk--no-lines-removed-message (list "<<no lines removed>>"))
7574

76-
(defcustom diff-hl-show-hunk-inline-hide-hunk nil
77-
"If t, inline-popup is shown over the hunk, hiding it."
78-
:type 'boolean)
79-
80-
(defcustom diff-hl-show-hunk-inline-smart-lines t
81-
"If t, inline-popup tries to show only the deleted lines of the
82-
hunk. The added lines are shown when scrolling the popup. If
83-
the hunk consist only on added lines, then
84-
`diff-hl-show-hunk--no-lines-removed-message' it is shown."
85-
:type 'boolean)
86-
8775
(defcustom diff-hl-show-hunk-function 'diff-hl-show-hunk-inline
8876
"The function used to render the hunk.
8977
The function receives as first parameter a buffer with the
@@ -226,70 +214,6 @@ Returns a list with the buffer and the line number of the clicked line."
226214
(define-key map (kbd "S") #'diff-hl-show-hunk-stage-hunk)
227215
map))
228216

229-
(defvar diff-hl-show-hunk--hide-function)
230-
231-
;;;###autoload
232-
(defun diff-hl-show-hunk-inline (buffer &optional _ignored-line)
233-
"Implementation to show the hunk in a inline popup.
234-
BUFFER is a buffer with the hunk."
235-
;; prevent diff-hl-inline-popup-hide from being called twice
236-
(let ((diff-hl-inline-popup--close-hook nil))
237-
(diff-hl-inline-popup-hide))
238-
(setq diff-hl-show-hunk--hide-function #'diff-hl-inline-popup-hide)
239-
(let* ((lines (split-string (with-current-buffer buffer (buffer-string)) "[\n\r]+" ))
240-
(smart-lines diff-hl-show-hunk-inline-smart-lines)
241-
(original-lines-number (cl-count-if (lambda (s) (string-prefix-p "-" s)) lines))
242-
(lines (if (string= (car (last lines)) "" ) (butlast lines) lines))
243-
(lines (if (and (eq original-lines-number 0) smart-lines)
244-
diff-hl-show-hunk--no-lines-removed-message
245-
lines))
246-
(overlay diff-hl-show-hunk--original-overlay)
247-
(type (overlay-get overlay 'diff-hl-hunk-type))
248-
(point (if (eq type 'delete) (overlay-start overlay) (overlay-end overlay)))
249-
(propertize-line (lambda (l)
250-
(propertize l 'face
251-
(cond ((string-prefix-p "+" l)
252-
'diff-added)
253-
((string-prefix-p "-" l)
254-
'diff-removed)))))
255-
(propertized-lines (mapcar propertize-line lines)))
256-
257-
(save-excursion
258-
;; Save point in case the hunk is hidden, so next/previous works as expected
259-
;; If the hunk is delete type, then don't hide the hunk
260-
;; (because the hunk is located in a non deleted line)
261-
(when (and diff-hl-show-hunk-inline-hide-hunk
262-
(not (eq type 'delete)))
263-
(let* ((invisible-overlay (make-overlay (overlay-start overlay)
264-
(overlay-end overlay))))
265-
;; Make new overlay, since the diff-hl overlay can be changed by diff-hl-flydiff
266-
(overlay-put invisible-overlay 'invisible t)
267-
;; Change default hide popup function, to make the overlay visible
268-
(setq diff-hl-show-hunk--hide-function
269-
(lambda ()
270-
(overlay-put invisible-overlay 'invisible nil)
271-
(delete-overlay invisible-overlay)
272-
(diff-hl-show-hunk-inline-hide)))))
273-
(diff-hl-show-hunk--goto-hunk-overlay overlay)
274-
(let ((height
275-
(when smart-lines
276-
(when (not (eq 0 original-lines-number))
277-
original-lines-number)))
278-
(footer "(q)Quit (p)Previous (n)Next (r)Revert (c)Copy original"))
279-
(unless diff-hl-show-staged-changes
280-
(setq footer (concat footer " (S)Stage")))
281-
(diff-hl-show-hunk-inline-show
282-
propertized-lines
283-
(if (and (boundp 'diff-hl-reference-revision) diff-hl-reference-revision)
284-
(concat "Diff with " diff-hl-reference-revision)
285-
"Diff with HEAD")
286-
footer
287-
diff-hl-show-hunk-map
288-
#'diff-hl-show-hunk-hide
289-
point
290-
height))
291-
)))
292-
293217
(defun diff-hl-show-hunk-copy-original-text ()
294218
"Extracts all the lines from BUFFER starting with '-' to the kill ring."
295219
(interactive)

0 commit comments

Comments
 (0)