Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve buffer listing performance with projectile #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 70 additions & 25 deletions ivy-rich.el
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ to hold the project name."
(ivy-rich-switch-buffer-size (:width 7))
(ivy-rich-switch-buffer-indicators (:width 4 :face error :align right))
(ivy-rich-switch-buffer-major-mode (:width 12 :face warning))
(ivy-rich-switch-buffer-project (:width 15 :face success))
(ivy-rich-switch-buffer-path (:width (lambda (x) (ivy-rich-switch-buffer-shorten-path x (ivy-rich-minibuffer-width 0.3))))))
:predicate
(lambda (cand) (get-buffer cand)))
Expand Down Expand Up @@ -200,7 +199,7 @@ using …."

;; Supports for `ivy-switch-buffer' ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defcustom ivy-rich-path-style
'relative
'abbrev
"File path style.

When set to 'full or 'absolute, absolute path will be used.
Expand Down Expand Up @@ -334,6 +333,43 @@ or /a/…/f.el."
(defun ivy-rich-switch-buffer-project (candidate)
(or (ivy-rich-switch-buffer-in-project-p candidate) ""))

(defun ivy-rich--switch-buffer-filename (candidate)
(let* ((buffer (get-buffer candidate)))
(cl-destructuring-bind
(filename mode)
(ivy-rich--local-values buffer '(buffer-file-name major-mode))
(when (and filename
(if (file-remote-p filename) ivy-rich-parse-remote-buffer t)
(not (eq mode 'dired-mode)))
;; when projectile is not available, we check for project mode and use
;; it to disable file-truename (for some reason)
(when (not (and (not (bound-and-true-p projectile-mode))
(require 'project nil t)))
(setq filename
(or (ivy-rich--local-values buffer 'buffer-file-truename)
(file-truename filename))))
(expand-file-name filename)))))

(defun ivy-rich--switch-buffer-root (candidate)
(let* ((buffer (get-buffer candidate)))
(cl-destructuring-bind
(directory mode)
(ivy-rich--local-values buffer '(default-directory major-mode))
(when (and directory
(not (eq mode 'dired-mode))
(ivy-rich-switch-buffer-in-project-p candidate))
;; Find the project root directory or `default-directory'
(setq directory
(cond ((bound-and-true-p projectile-mode)
(or (ivy-rich--local-values buffer
'projectile-project-root)
(with-current-buffer buffer
(projectile-project-root))))
((require 'project nil t)
(with-current-buffer buffer
(car (project-roots (project-current)))))))
(expand-file-name directory)))))

(defun ivy-rich--switch-buffer-root-and-filename (candidate)
(let* ((buffer (get-buffer candidate))
(truenamep t))
Expand Down Expand Up @@ -361,30 +397,39 @@ or /a/…/f.el."
(cons (expand-file-name directory)
(expand-file-name filename))))))

(defun ivy-rich--switch-buffer-file-name-or-root (candidate)
(or (ivy-rich--switch-buffer-filename candidate)
(ivy-rich--switch-buffer-root candidate)))

(defun ivy-rich-switch-buffer-path (candidate)
(if-let ((result (ivy-rich--switch-buffer-root-and-filename candidate)))
(cl-destructuring-bind (root . filename) result
(cond
;; Case: absolute
((or (memq ivy-rich-path-style '(full absolute))
(and (null ivy-rich-parse-remote-file-path)
(or (file-remote-p root))))
(or filename root))
;; Case: abbreviate
((memq ivy-rich-path-style '(abbreviate abbrev))
(abbreviate-file-name (or filename root)))
;; Case: relative
((or (eq ivy-rich-path-style 'relative)
t) ; make 'relative default
(if (and filename root)
(let ((relative-path (string-remove-prefix root filename)))
(if (string= relative-path candidate)
(file-name-as-directory
(file-name-nondirectory
(directory-file-name (file-name-directory filename))))
relative-path))
""))))
""))
(cond
;; Case: abbreviate
((memq ivy-rich-path-style '(abbreviate abbrev))
(if-let ((path (ivy-rich--switch-buffer-file-name-or-root candidate)))
(abbreviate-file-name path)))
;; Case: absolute
((memq ivy-rich-path-style '(full absolute))
(ivy-rich--switch-buffer-file-name-or-root candidate))
;; Case: remote
(t
(if-let
((result (ivy-rich--switch-buffer-root-and-filename candidate)))
(cl-destructuring-bind (root . filename) result
(cond
((and (null ivy-rich-parse-remote-file-path)
(file-remote-p root))
(or filename root))
;; Case: relative
((or (eq ivy-rich-path-style 'relative)
t) ; make 'relative default
(if (and filename root)
(let ((relative-path (string-remove-prefix root filename)))
(if (string= relative-path candidate)
(file-name-as-directory
(file-name-nondirectory
(directory-file-name (file-name-directory filename))))
relative-path))
""))))))))

;; Supports for `counsel-M-x', `counsel-describe-function', `counsel-describe-variable'
(defun ivy-rich-counsel-function-docstring (candidate)
Expand Down