Test files cluttering find references in Emacs
When using consult-xref
to navigate code references, test files often clutter results. This advice function sorts them to the end:
(defun my-consult-xref-sort-tests-last (original-fn fetcher alist)
"Sort test/fuzz files to the end in consult-xref."
(let* ((test-re (rx word-boundary (or "test" "fuzz" "fuzzer") word-boundary))
(wrapped-fetcher
(lambda ()
(let* ((items (funcall fetcher))
(is-test-p (lambda (item)
(string-match-p test-re
(xref-location-group
(xref-item-location item)))))
(non-test-items (seq-remove is-test-p items))
(test-items (seq-filter is-test-p items)))
(append non-test-items test-items)))))
(funcall original-fn wrapped-fetcher alist)))
(advice-add 'consult-xref :around #'my-consult-xref-sort-tests-last)
The function wraps consult-xref
's fetcher argument with a lambda that:
- Calls the original fetcher to get all xref items
- Partitions items by whether their file path contains "test", "fuzz", or "fuzzer"
- Returns non-test items followed by test items
This approach is pretty simple, but I will likely iterate on it further. Additionally I'll likely apply it to things like consult-ripgrep
.
You can see the commit to my dotfiles at https://github.com/keegancsmith/dotfiles/commit/fe1bf05