From e77702c55427f1c9a2ece7ea8078e68e2cf84ecf Mon Sep 17 00:00:00 2001 From: Alexander Miller Date: Fri, 9 Aug 2024 17:11:48 +0200 Subject: [PATCH] [Interface] Keep workspace order the same as the persist file during completion. Fixes #1108 --- src/elisp/treemacs-core-utils.el | 8 ++++++++ src/elisp/treemacs-workspaces.el | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/elisp/treemacs-core-utils.el b/src/elisp/treemacs-core-utils.el index f65ceb04..994e4087 100644 --- a/src/elisp/treemacs-core-utils.el +++ b/src/elisp/treemacs-core-utils.el @@ -1258,6 +1258,14 @@ treemacs window." (unless (eq next-window current-window) (select-window next-window)))))) +(defun treemacs--pre-sorted-list (items) + "Return a lambda that includes sorting metedata for `completing-read'. +Ensures that the order of ITEMS is not changed during completion." + (lambda (string pred action) + (pcase action + ('metadata `(metadata (display-sort-function . ,#'identity))) + (_ (complete-with-action action items string pred))))) + (provide 'treemacs-core-utils) ;;; treemacs-core-utils.el ends here diff --git a/src/elisp/treemacs-workspaces.el b/src/elisp/treemacs-workspaces.el index 3dbafc90..38bb880d 100644 --- a/src/elisp/treemacs-workspaces.el +++ b/src/elisp/treemacs-workspaces.el @@ -648,7 +648,10 @@ Return values may be as follows: (let* ((workspaces (->> treemacs--workspaces (--reject (eq it (treemacs-current-workspace))) (--map (cons (treemacs-workspace->name it) it)))) - (name (completing-read "Switch to: " workspaces nil :require-match))) + (name (completing-read + "Switch to: " + (treemacs--pre-sorted-list workspaces) + nil :require-match))) (setf new-workspace (cdr (--first (string= (car it) name) workspaces)))))) (setf (treemacs-current-workspace) new-workspace) (treemacs--invalidate-buffer-project-cache)