Function: treemacs-find-file-node

treemacs-find-file-node is a byte-compiled function defined in treemacs-core-utils.el.

Signature

(treemacs-find-file-node PATH &optional PROJECT)

Documentation

Find position of node identified by PATH under PROJECT in the current buffer.

If PROJECT is not given it will be found with treemacs--find-project-for-path. No attempt is made to verify that PATH falls under a project in the workspace. It is assumed that this check has already been made.

PATH: File Path PROJECT: Project Struct

Source Code

;; Defined in ~/.emacs.d/elpa/treemacs-20251226.1307/treemacs-core-utils.el
(defun treemacs-find-file-node (path &optional project)
  "Find position of node identified by PATH under PROJECT in the current buffer.
If PROJECT is not given it will be found with `treemacs--find-project-for-path'.
No attempt is made to verify that PATH falls under a project in the workspace.
It is assumed that this check has already been made.

PATH: File Path
PROJECT: Project Struct"
  (unless project (setq project (treemacs--find-project-for-path path)))
  (let* (;; go back here if the search fails
         (start (prog1 (point) (goto-char (treemacs-project->position project))))
         ;; the path we're moving to minus the project root
         (path-minus-root (->> project (treemacs-project->path) (length) (substring path)))
         ;; the parts of the path that we can try to go to until we arrive at the project root
         (dir-parts (nreverse (s-split "/" path-minus-root :omit-nulls)))
         ;; the path we try to quickly move to because it's already open and thus in the dom
         (goto-path path)
         ;; manual as in to be expanded manually after we moved to the next closest node we can find
         ;; in the dom
         (manual-parts nil)
         (dom-node nil))
    ;; try to move as close as possible to the followed file, starting with its immediate parent
    ;; keep moving upwards in the path we move to until reaching the root of the project (counter = 0)
    ;; all the while collecting the parts of the path that beed manual expanding
    (-let [continue t]
      (while continue
        (setf dom-node (treemacs-find-in-dom goto-path)
              goto-path (treemacs--parent goto-path))
        (if (or (null dom-node)
                ;; dom node might exist, but a leaf's position is not always known
                (null (treemacs-dom-node->position dom-node)))
            (progn
              (push (pop dir-parts) manual-parts))
          (setf continue nil))))
    (let* ((btn (--if-let (treemacs-dom-node->position dom-node)
                    it
                  (treemacs-project->position project)))
           ;; do the rest manually - at least the actual file to move to is still left in manual-parts
           (search-result (if manual-parts (save-match-data
                                             (treemacs--follow-each-dir btn manual-parts project))
                            (goto-char btn))))
      (if (eq 'follow-failed search-result)
          (prog1 nil
            (goto-char start))
        (treemacs-dom-node->set-position! (treemacs-find-in-dom path) search-result)
        search-result))))