Skip to content

Commit 2cf295d

Browse files
committed
Huge boost.
1 parent 742d655 commit 2cf295d

File tree

1 file changed

+58
-19
lines changed

1 file changed

+58
-19
lines changed

ivy-file-preview.el

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@
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+
7073
(defvar ivy-file-preview--overlays '()
7174
"List of overlays.")
7275

76+
(defvar ivy-file-preview--ivy-text ""
77+
"Record down the ivy text to prevent make overlay if not need to.")
78+
7379
;;; Util
7480

7581
(defun ivy-file-preview--project-path ()
@@ -81,23 +87,25 @@
8187
(goto-char (point-min))
8288
(forward-line (1- ln)))
8389

84-
(defun ivy-file-preview--convert-pos (ln col)
90+
(defun ivy-file-preview--convert-pos-delta (ln col)
8591
"Convert LN and COL to position point."
8692
(save-excursion
87-
(ivy-file-preview--goto-line ln)
88-
(move-to-column col)
89-
(point)))
93+
(forward-line ln)
94+
(+ (point) col)))
9095

91-
(defun ivy-file-preview--make-overlay (beg end)
92-
"Make a new overlay with BEG and END."
96+
(defun ivy-file-preview--make-overlay (beg end fc)
97+
"Make a new overlay with BEG, END and face (FC)."
9398
(let ((ol (make-overlay beg end)))
94-
(overlay-put ol 'face (if (= beg (point)) 'ivy-current-match 'ivy-minibuffer-match-highlight))
99+
(overlay-put ol 'face fc)
95100
(overlay-put ol 'priority 0)
96-
(push ol ivy-file-preview--overlays)))
101+
(push ol ivy-file-preview--overlays) ; NOTE: Eventually get managed bt list.
102+
ol))
97103

98104
(defun ivy-file-preview--delete-overlays ()
99105
"Delete all overlays in list."
100-
(dolist (ov ivy-file-preview--overlays) (delete-overlay ov)))
106+
(dolist (ov ivy-file-preview--overlays) (delete-overlay ov))
107+
(setq ivy-file-preview--overlays nil)
108+
(setq ivy-file-preview--current-overlay nil))
101109

102110
(defun ivy-file-preview--put-window-plist (prop val)
103111
"Set property list with PROP and VAL."
@@ -137,20 +145,43 @@
137145
(setq index (1+ index)))
138146
results))
139147

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+
154+
(defun ivy-file-preview--swap-current-overlay ()
155+
"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))))
165+
140166
(defun ivy-file-preview--make-overlays ()
141167
"Make overlays through out the whole buffer."
142168
(let ((ov-data (ivy-file-preview--extract-candidates-overlay-data))
143-
pos ln col
144-
(len (length ivy-text)))
169+
pos ln col (len (length ivy-text))
170+
ov current-ov-p fc
171+
(current-ln (line-number-at-pos)) delta-ln)
145172
(dolist (data ov-data)
146173
(setq ln (plist-get data :line-number)
147174
col (plist-get data :column))
148175
(if (not col)
149176
(setq pos ln)
150-
(setq ln (string-to-number ln)
151-
col (string-to-number col))
152-
(setq pos (ivy-file-preview--convert-pos ln col)))
153-
(ivy-file-preview--make-overlay pos (+ pos len)))))
177+
(setq ln (string-to-number ln) col (string-to-number col)
178+
delta-ln (- ln current-ln)
179+
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)))))
154185

155186
(defun ivy-file-preview--open-file (fn pos)
156187
"Open the file path (FN) and move to POS.
@@ -163,8 +194,11 @@ If POS is nil then it won't moves."
163194
(setq ivy-file-preview--selected-file fn)
164195
(cond ((consp pos)
165196
(ivy-file-preview--goto-line (car pos))
166-
(move-to-column (cdr pos)))
167-
((integerp pos) (goto-char (1+ pos)))
197+
(move-to-column (cdr pos))
198+
(recenter))
199+
((integerp pos)
200+
(goto-char (1+ pos))
201+
(recenter))
168202
((not pos) (goto-char (point-min)))
169203
(t (error "Invalid position details: %s" pos))))))
170204

@@ -184,8 +218,13 @@ FN is the file path. POS can either be one of the following type:
184218
(ivy-file-preview--delete-overlays))
185219
(ivy-file-preview--open-file fn pos)
186220
(when (and ivy-file-preview-overlay-p ivy-file-preview-details)
187-
(ivy-file-preview--delete-overlays)
188-
(ivy-file-preview--make-overlays)))))
221+
(if (and (string= ivy-file-preview--ivy-text ivy-text)
222+
ivy-file-preview--current-overlay)
223+
(progn
224+
(setq ivy-file-preview--ivy-text ivy-text)
225+
(ivy-file-preview--swap-current-overlay))
226+
(ivy-file-preview--delete-overlays)
227+
(ivy-file-preview--make-overlays))))))
189228

190229
(defun ivy-file-preview--after-select (&rest _)
191230
"Execution after selection."

0 commit comments

Comments
 (0)