8888 " Record the current present directory.
8989It could either by `project-directory' or `default-directory' " )
9090
91+ (defvar ivy-file-preview--this-command nil
92+ " Record of this command when entering minibuffer." )
93+
94+ ; ;; External
95+
96+ (defvar ivy-searcher--candidates )
97+
9198; ;; Util
9299
100+ (defun ivy-file-preview--match-end (&optional pos index )
101+ " Get matching end by currently selected candidate.
102+
103+ An optional argument POS is use to calculate the position that relative to the
104+ length of `ivy-text' .
105+
106+ An optional argument INDEX is use to find current ivy candidate."
107+ (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))
111+ (or (plist-get (nth index ivy-searcher--candidates) :end )
112+ (+ pos (length ivy-text))))
113+ (t (+ pos (length ivy-text)))))
114+
93115(defun ivy-file-preview--project-path ()
94116 " Get current project path."
95117 (cdr (project-current )))
@@ -128,12 +150,12 @@ If CURRENT-OV is non-nil it create overlay that are currently selected."
128150 (push ol ivy-file-preview--overlays) ; NOTE: Eventually get managed bt list.
129151 ol))
130152
131- (defun ivy-file-preview--make-current-overlay (&optional pos len )
132- " Make current selected overlay with POS and LEN ."
133- (unless pos (setq pos (point )))
134- (unless len (setq len ( length ivy-text)))
153+ (defun ivy-file-preview--make-current-overlay (&optional beg end )
154+ " Make current selected overlay with BEG and END ."
155+ (unless beg (setq beg (point )))
156+ (unless end (setq end ( + beg ( length ivy-text) )))
135157 (setq ivy-file-preview--current-overlay
136- (ivy-file-preview--make-overlay pos ( + pos len) t )))
158+ (ivy-file-preview--make-overlay beg end t )))
137159
138160(defun ivy-file-preview--delete-overlays ()
139161 " Delete all overlays in list."
@@ -179,27 +201,29 @@ If CURRENT-OV is non-nil it create overlay that are currently selected."
179201
180202(defun ivy-file-preview--swap-current-overlay ()
181203 " Delete the previous selected overlay and swap with current selected overlay."
182- (let ((pos (point )) (len ( length ivy-text )))
183- (move-overlay ivy-file-preview--current-overlay pos ( + pos len) )))
204+ (let* ((start (point )) (end ( ivy-file-preview--match-end start )))
205+ (move-overlay ivy-file-preview--current-overlay start end )))
184206
185207(defun ivy-file-preview--make-overlays ()
186208 " Make overlays through out the whole buffer."
187209 (let ((ov-data (ivy-file-preview--extract-candidates-overlay-data))
188- pos ln col (len (length ivy-text))
210+ (index 0 )
211+ pos end ln col
189212 current-ov-p
190213 (current-ln (line-number-at-pos )) delta-ln)
191214 (dolist (data ov-data)
192- (setq ln (plist-get data :line-number )
193- col (plist-get data :column ))
215+ (setq ln (plist-get data :line-number ) col (plist-get data :column ))
194216 (if (not col)
195217 (setq pos ln)
196218 (setq ln (string-to-number ln) col (string-to-number col)
197219 delta-ln (- ln current-ln)
198- pos (ivy-file-preview--convert-pos-delta delta-ln col)))
220+ pos (ivy-file-preview--convert-pos-delta delta-ln col)
221+ end (ivy-file-preview--match-end pos index)))
199222 (setq current-ov-p (= pos (point )))
200- (ivy-file-preview--make-overlay pos ( + pos len) )
223+ (ivy-file-preview--make-overlay pos end )
201224 (when current-ov-p
202- (ivy-file-preview--make-current-overlay pos len)))))
225+ (ivy-file-preview--make-current-overlay pos end))
226+ (setq index (1+ index)))))
203227
204228(defun ivy-file-preview--delay-make-overlays ()
205229 " Seconds to delay display overlays."
@@ -289,8 +313,9 @@ FN is the file path. POS can either be one of the following type:
289313
290314(defun ivy-file-preview--enter ()
291315 " Execution after minibuffer setup."
292- (setq ivy-file-preview--window-status '())
293- (setq ivy-file-preview--current-dir default-directory)
316+ (setq ivy-file-preview--this-command this-command
317+ ivy-file-preview--window-status '()
318+ ivy-file-preview--current-dir default-directory)
294319 (with-selected-window minibuffer-scroll-window
295320 (ivy-file-preview--put-window-plist :file (current-buffer ))
296321 (ivy-file-preview--put-window-plist :window-point (window-point ))
@@ -304,10 +329,11 @@ FN is the file path. POS can either be one of the following type:
304329 (dolist (fn ivy-file-preview--preview-files)
305330 (unless (string= ivy-file-preview--selected-file fn)
306331 (ignore-errors (kill-buffer (f-filename fn)))))
307- (setq ivy-file-preview--selected-file " " )
308- (setq ivy-file-preview--ivy-text " " )
309- (setq ivy-file-preview--current-dir " " )
310- (setq ivy-file-preview--preview-files '()))
332+ (setq ivy-file-preview--selected-file " "
333+ ivy-file-preview--ivy-text " "
334+ ivy-file-preview--current-dir " "
335+ ivy-file-preview--preview-files '()
336+ ivy-file-preview--this-command nil ))
311337
312338; ;; Entry
313339
0 commit comments