@@ -91,12 +91,31 @@ It could either by `project-directory' or `default-directory'")
9191(defvar ivy-file-preview--this-command nil
9292 " Record of this command when entering minibuffer." )
9393
94+ (defvar ivy-file-preview--first-cand-index nil
95+ " Record of first candidate's index." )
96+
9497; ;; External
9598
9699(defvar ivy-searcher--candidates )
97100
98101; ;; Util
99102
103+ (defun ivy-file-preview--ivy-searcher-p ()
104+ " Return non-nil if current command is from `ivy-searcher' ."
105+ (memq ivy-file-preview--this-command '(ivy-searcher-search-file
106+ ivy-searcher-search-project)))
107+
108+ (defun ivy-file-preview--filter-candidates ()
109+ " Filter possible candidates for overlays."
110+ (when (ivy-file-preview--ivy-searcher-p)
111+ (setq ivy-file-preview--first-cand-index
112+ (cl-position
113+ nil ivy-searcher--candidates
114+ :test
115+ (lambda (_key cand )
116+ (pop cand)
117+ (string= ivy-file-preview--selected-file (plist-get cand :file )))))))
118+
100119(defun ivy-file-preview--match-end (&optional pos index )
101120 " Get matching end by currently selected candidate.
102121
@@ -105,9 +124,11 @@ length of `ivy-text'.
105124
106125An optional argument INDEX is use to find current ivy candidate."
107126 (unless pos (setq pos (point )))
108- (unless index (setq index ivy--index))
109- (cond ((memq ivy-file-preview--this-command '(ivy-searcher-search-file
110- ivy-searcher-search-project))
127+ (if index
128+ (when ivy-file-preview--first-cand-index
129+ (setq index (+ index ivy-file-preview--first-cand-index)))
130+ (setq index ivy--index))
131+ (cond ((ivy-file-preview--ivy-searcher-p)
111132 (let* ((cand (nth index ivy-searcher--candidates))
112133 (plist-data (cdr cand))
113134 (end-pt (plist-get plist-data :end )))
@@ -183,15 +204,16 @@ If CURRENT-OV is non-nil it create overlay that are currently selected."
183204
184205(defun ivy-file-preview--extract-candidates-overlay-data ()
185206 " Extract the overlay data from current ivy candidates."
207+ (ivy-file-preview--filter-candidates)
186208 (let* ((fn (s-replace ivy-file-preview--current-dir " " ivy-file-preview--selected-file))
187209 (cands (ivy-file-preview--candidates))
188210 (cands-len (length cands)) current-cand entered ln-data
189211 ln col
190212 cand-fn (results '()) break (index 0 ))
191213 (while (and (not break) (< index cands-len))
192- (setq current-cand (nth index cands))
193- ( setq ln-data (split-string current-cand " :" ) )
194- ( setq cand-fn (nth 0 ln-data))
214+ (setq current-cand (nth index cands)
215+ ln-data (split-string current-cand " :" )
216+ cand-fn (nth 0 ln-data))
195217 (if (string= cand-fn fn)
196218 (progn
197219 (setq ln (nth 1 ln-data) col (nth 2 ln-data))
0 commit comments