From 9ca737c4198c5e7faef7a74de7b1e84b11e1dbeb Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Fri, 17 Sep 2021 15:39:23 +0300 Subject: [PATCH] xref-matches-in-files: Move sorting to Lisp For better compatibility with different systems. Performance is unaffected, except in very pathological cases (~100000 matches), and even then the overhead of 'sort' is comparable. * lisp/progmodes/xref.el (xref-search-program-alist): Drop the piping through 'sort'. (xref-matches-in-files): Sort here instead. Do that to both searchers' output as well now. --- lisp/progmodes/xref.el | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el index ab7e8f6e29e..69cabd0b5a5 100644 --- a/lisp/progmodes/xref.el +++ b/lisp/progmodes/xref.el @@ -1625,13 +1625,8 @@ IGNORES is a list of glob patterns for files to ignore." "xargs -0 grep -snHE -e ") (ripgrep . - ;; Note: by default, ripgrep's output order is non-deterministic - ;; (https://github.com/BurntSushi/ripgrep/issues/152) - ;; because it does the search in parallel. You can use the template - ;; without the '| sort ...' part if GNU sort is not available on - ;; your system and/or stable ordering is not important to you. - ;; Note#2: '!*/' is there to filter out dirs (e.g. submodules). - "xargs -0 rg -nH --no-messages -g '!*/' -e | sort -t: -k1,1 -k2n,2" + ;; '!*/' is there to filter out dirs (e.g. submodules). + "xargs -0 rg -nH --no-messages -g '!*/' -e " )) "Associative list mapping program identifiers to command templates. @@ -1723,7 +1718,16 @@ FILES must be a list of absolute file names." (match-string file-group) (buffer-substring-no-properties (point) (line-end-position))) hits))) - (xref--convert-hits (nreverse hits) regexp))) + ;; By default, ripgrep's output order is non-deterministic + ;; (https://github.com/BurntSushi/ripgrep/issues/152) + ;; because it does the search in parallel. + ;; Grep's output also comes out in seemingly arbitrary order, + ;; though stable one. Let's sort both for better UI. + (setq hits + (sort (nreverse hits) + (lambda (h1 h2) + (string< (cadr h1) (cadr h2))))) + (xref--convert-hits hits regexp))) (defun xref--process-file-region ( start end program &optional buffer display -- 2.39.5