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)