Function: treemacs--determine-insert-position

treemacs--determine-insert-position is a byte-compiled function defined in treemacs-rendering.el.

Signature

(treemacs--determine-insert-position PATH PARENT-BTN SORT-FUNCTION)

Documentation

Determine the insert location for PATH under PARENT-BTN.

Specifically this will return the node *after* which to make the new insert.

Mostly this means the position before the first node for whose path returns SORT-FUNCTION returns non-nil, but files and directories must be handled properly,and edge cases for inserting at the end of the project and buffer must be taken into account.

PATH: File Path PARENT-BTN: Button SORT-FUNCTION: Button -> Boolean.

Source Code

;; Defined in ~/.emacs.d/elpa/treemacs-20251226.1307/treemacs-rendering.el
(defun treemacs--determine-insert-position (path parent-btn sort-function)
  "Determine the insert location for PATH under PARENT-BTN.
Specifically this will return the node *after* which to make the new insert.

Mostly this means the position before the first node for whose path returns
SORT-FUNCTION returns non-nil, but files and directories must be handled
properly,and edge cases for inserting at the end of the project and buffer must
be taken into account.

PATH: File Path
PARENT-BTN: Button
SORT-FUNCTION: Button -> Boolean."
  (let* ((parent-dom-node (treemacs-find-in-dom (treemacs-button-get parent-btn :path)))
         (children (treemacs-dom-node->children parent-dom-node))
         (dirs-files (--separate (-let [path (treemacs-dom-node->key it)]
                                   (and (stringp path) (file-directory-p path)))
                                 children))
         (dirs (sort (car dirs-files) (lambda (d1 d2)
                                        (funcall sort-function
                                                 (treemacs-dom-node->key d1)
                                                 (treemacs-dom-node->key d2)))))
         (files (sort (cadr dirs-files) (lambda (f1 f2)
                                        (funcall sort-function
                                                 (treemacs-dom-node->key f1)
                                                 (treemacs-dom-node->key f2))))))
    (if (file-directory-p path)
        ;; insert directory ...
        (or
         ;; at first dir that fits sort order
         (--when-let (--first (funcall sort-function path (treemacs-dom-node->key it)) dirs)
           (previous-button (or (treemacs-dom-node->position it)
                                (treemacs-find-file-node (treemacs-dom-node->key it)))))
         ;; after last dir
         (--when-let (-last-item dirs)
           (or (treemacs-dom-node->position it)
               (treemacs-find-file-node (treemacs-dom-node->key it))))
         ;; before first file
         (--when-let (car files)
           (previous-button (or (treemacs-dom-node->position it)
                                (treemacs-find-file-node (treemacs-dom-node->key it)))))
         ;; after parent
         parent-btn)

      ;; insert file ...
      (or
       ;; at first file that fits sort order
       (--when-let (--first (funcall sort-function path (treemacs-dom-node->key it)) files)
         (previous-button (or (treemacs-dom-node->position it)
                              (treemacs-find-file-node (treemacs-dom-node->key it)))) )
       ;; after last file
       (--when-let (-last-item files)
         (or (treemacs-dom-node->position it)
             (treemacs-find-file-node (treemacs-dom-node->key it))) )
       ;; after last dir
       (--when-let (-last-item dirs)
         (or (treemacs-dom-node->position it)
             (treemacs-find-file-node (treemacs-dom-node->key it))))
       ;; after parent
       parent-btn))))