Skip to content

Commit 4190128

Browse files
committed
Fixed overlay not display correctly.
1 parent 2cf295d commit 4190128

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

ivy-file-preview.el

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@
6767
(defvar ivy-file-preview--window-status '()
6868
"Record windows status for when canceling command.")
6969

70-
(defvar ivy-file-preview--current-overlay nil
71-
"Record down the current selected overlay.")
72-
7370
(defvar ivy-file-preview--overlays '()
7471
"List of overlays.")
7572

73+
(defvar ivy-file-preview--current-overlay nil
74+
"Record down the current selected overlay.")
75+
7676
(defvar ivy-file-preview--ivy-text ""
7777
"Record down the ivy text to prevent make overlay if not need to.")
7878

@@ -93,14 +93,23 @@
9393
(forward-line ln)
9494
(+ (point) col)))
9595

96-
(defun ivy-file-preview--make-overlay (beg end fc)
97-
"Make a new overlay with BEG, END and face (FC)."
96+
(defun ivy-file-preview--make-overlay (beg end &optional current-ov)
97+
"Make a new overlay with BEG, END and face (FC).
98+
If CURRENT-OV is non-nil it create overlay that are currently selected."
9899
(let ((ol (make-overlay beg end)))
99-
(overlay-put ol 'face fc)
100-
(overlay-put ol 'priority 0)
100+
(overlay-put ol 'face (if (= beg (point)) 'ivy-current-match
101+
'ivy-minibuffer-match-highlight))
102+
(overlay-put ol 'priority (if current-ov 100 0))
101103
(push ol ivy-file-preview--overlays) ; NOTE: Eventually get managed bt list.
102104
ol))
103105

106+
(defun ivy-file-preview--make-current-overlay (&optional pos len)
107+
"Make current selected overlay with POS and LEN."
108+
(unless pos (setq pos (point)))
109+
(unless len (setq len (length ivy-text)))
110+
(setq ivy-file-preview--current-overlay
111+
(ivy-file-preview--make-overlay pos (+ pos len) t)))
112+
104113
(defun ivy-file-preview--delete-overlays ()
105114
"Delete all overlays in list."
106115
(dolist (ov ivy-file-preview--overlays) (delete-overlay ov))
@@ -145,29 +154,16 @@
145154
(setq index (1+ index)))
146155
results))
147156

148-
(defun ivy-file-preview--make-current-overlay (&optional fc pos len)
149-
"Make current selected overlay with face (FC), POS and LEN."
150-
(unless pos (setq pos (point)))
151-
(unless len (setq len (length ivy-text)))
152-
(ivy-file-preview--make-overlay pos (+ pos len) fc))
153-
154157
(defun ivy-file-preview--swap-current-overlay ()
155158
"Delete the previous selected overlay and swap with current selected overlay."
156-
(let ((beg (overlay-start ivy-file-preview--current-overlay))
157-
(end (overlay-end ivy-file-preview--current-overlay)))
158-
;; Delete previous overlay.
159-
(delete-overlay ivy-file-preview--current-overlay)
160-
;; Make previous selected overlay.
161-
(ivy-file-preview--make-overlay beg end 'ivy-minibuffer-match-highlight)
162-
;; Make current selected overlay.
163-
(setq ivy-file-preview--current-overlay
164-
(ivy-file-preview--make-current-overlay 'ivy-current-match))))
159+
(let ((pos (point)) (len (length ivy-text)))
160+
(move-overlay ivy-file-preview--current-overlay pos (+ pos len))))
165161

166162
(defun ivy-file-preview--make-overlays ()
167163
"Make overlays through out the whole buffer."
168164
(let ((ov-data (ivy-file-preview--extract-candidates-overlay-data))
169165
pos ln col (len (length ivy-text))
170-
ov current-ov-p fc
166+
current-ov-p
171167
(current-ln (line-number-at-pos)) delta-ln)
172168
(dolist (data ov-data)
173169
(setq ln (plist-get data :line-number)
@@ -177,11 +173,10 @@
177173
(setq ln (string-to-number ln) col (string-to-number col)
178174
delta-ln (- ln current-ln)
179175
pos (ivy-file-preview--convert-pos-delta delta-ln col)))
180-
(setq current-ov-p (= pos (point))
181-
fc (if current-ov-p 'ivy-current-match 'ivy-minibuffer-match-highlight)
182-
ov (ivy-file-preview--make-current-overlay fc pos len))
183-
(when current-ov-p
184-
(setq ivy-file-preview--current-overlay ov)))))
176+
(setq current-ov-p (= pos (point)))
177+
(if current-ov-p
178+
(ivy-file-preview--make-current-overlay pos len)
179+
(ivy-file-preview--make-overlay pos (+ pos len))))))
185180

186181
(defun ivy-file-preview--open-file (fn pos)
187182
"Open the file path (FN) and move to POS.
@@ -220,11 +215,10 @@ FN is the file path. POS can either be one of the following type:
220215
(when (and ivy-file-preview-overlay-p ivy-file-preview-details)
221216
(if (and (string= ivy-file-preview--ivy-text ivy-text)
222217
ivy-file-preview--current-overlay)
223-
(progn
224-
(setq ivy-file-preview--ivy-text ivy-text)
225-
(ivy-file-preview--swap-current-overlay))
218+
(ivy-file-preview--swap-current-overlay)
226219
(ivy-file-preview--delete-overlays)
227-
(ivy-file-preview--make-overlays))))))
220+
(ivy-file-preview--make-overlays))
221+
(setq ivy-file-preview--ivy-text ivy-text)))))
228222

229223
(defun ivy-file-preview--after-select (&rest _)
230224
"Execution after selection."
@@ -254,7 +248,6 @@ FN is the file path. POS can either be one of the following type:
254248

255249
(defun ivy-file-preview--cancel-revert ()
256250
"Revert frame status if user cancel the commands."
257-
(ivy-file-preview--delete-overlays)
258251
(unless ivy-exit
259252
(setq ivy-file-preview--selected-file "")
260253
(switch-to-buffer (plist-get ivy-file-preview--window-status :file))
@@ -270,12 +263,14 @@ FN is the file path. POS can either be one of the following type:
270263

271264
(defun ivy-file-preview--exit ()
272265
"Execution before minibuffer exits."
273-
(ivy-file-preview--cancel-revert) ; If already empty, revert it.
266+
(ivy-file-preview--cancel-revert)
267+
(ivy-file-preview--delete-overlays)
274268
(delete-dups ivy-file-preview--preview-files)
275269
(dolist (fn ivy-file-preview--preview-files)
276270
(unless (string= ivy-file-preview--selected-file fn)
277271
(ignore-errors (kill-buffer (f-filename fn)))))
278272
(setq ivy-file-preview--selected-file "")
273+
(setq ivy-file-preview--ivy-text "")
279274
(setq ivy-file-preview--preview-files '()))
280275

281276
;;; Entry

0 commit comments

Comments
 (0)