Skip to content

Commit ce3538d

Browse files
committed
[emacs] use proximity sorting for project-find-file
1 parent 16c4c79 commit ce3538d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

emacs/.config/emacs/init.el

+31
Original file line numberDiff line numberDiff line change
@@ -1539,12 +1539,43 @@ Pass ORIG-FN, BEG, END, TYPE, ARGS."
15391539

15401540
(setq enable-recursive-minibuffers t)
15411541

1542+
(defun common-directory-levels (path1 path2)
1543+
"Return the number of common directory components between PATH1 and PATH2."
1544+
(let ((dirs1 (split-string (file-name-directory (expand-file-name path1)) "/" t))
1545+
(dirs2 (split-string (file-name-directory (expand-file-name path2)) "/" t)))
1546+
(cl-loop for d1 in dirs1
1547+
for d2 in dirs2
1548+
while (string= d1 d2)
1549+
count 1)))
1550+
1551+
(defun sort-files-by-closeness (files target-file)
1552+
"Sort FILES based on how close each file is to TARGET-FILE.
1553+
Closeness is determined by the number of common directory levels."
1554+
(let* ((target-dir (file-name-directory target-file))
1555+
(files-with-levels
1556+
(cl-loop for file in files
1557+
unless (string= file target-file)
1558+
collect (cons file (common-directory-levels file target-dir)))))
1559+
(mapcar #'car
1560+
(sort files-with-levels
1561+
(lambda (a b) (> (cdr a) (cdr b)))))))
1562+
1563+
(defun meain/sort-proximity (files)
1564+
(let* ((prev-buffer (window-buffer (minibuffer-selected-window)))
1565+
(current-file (if (buffer-file-name prev-buffer)
1566+
(buffer-file-name prev-buffer)
1567+
"."))
1568+
(project-path (expand-file-name (project-root (project-current))))
1569+
(current-file-sans-project (string-remove-prefix project-path current-file)))
1570+
(sort-files-by-closeness files current-file-sans-project)))
1571+
15421572
(vertico-multiform-mode)
15431573
(setq vertico-multiform-commands
15441574
'((consult-ripgrep buffer indexed)
15451575
(consult-xref buffer indexed)
15461576
(eglot-find-implementation indexed) ;; TODO: change to vertical
15471577
(consult-imenu buffer)
1578+
(project-find-file flat (vertico-sort-function . meain/sort-proximity))
15481579
(xref-find-references buffer)
15491580
(meain/imenu-or-eglot buffer)
15501581
(tree-jump-search buffer)

0 commit comments

Comments
 (0)