Function: projectile-search--rg-scan-roots

projectile-search--rg-scan-roots is a byte-compiled function defined in projectile.el.

Signature

(projectile-search--rg-scan-roots BUFFER TERM ROOTS GENERATION ON-DONE)

Documentation

Run rg for TERM over the first of ROOTS, chaining to the rest on exit.

Matches accumulate in BUFFER across the whole of ROOTS. GENERATION is projectile-replace--scan-generation as it stood when the scan began, so a process outliving a cancelled scan cannot resume the chain. Calls projectile-search--rg-finish with ON-DONE once ROOTS is exhausted or the match cap is hit.

Source Code

;; Defined in ~/.emacs.d/elpa/projectile-20260820.1509/projectile.el
(defun projectile-search--rg-scan-roots (buffer term roots generation on-done)
  "Run `rg' for TERM over the first of ROOTS, chaining to the rest on exit.
Matches accumulate in BUFFER across the whole of ROOTS.  GENERATION is
`projectile-replace--scan-generation' as it stood when the scan began, so
a process outliving a cancelled scan cannot resume the chain.  Calls
`projectile-search--rg-finish' with ON-DONE once ROOTS is exhausted or
the match cap is hit."
  (if (or (null roots) (not (buffer-live-p buffer)))
      (projectile-search--rg-finish buffer on-done)
    (let* (;; a group may be spelled with `~'; ripgrep needs a real working
           ;; directory, and the parsed paths have to carry the absolute
           ;; spelling the results buffer relativises against
           (root (file-name-as-directory (expand-file-name (car roots))))
           (case-fold (buffer-local-value 'projectile-replace--case-fold buffer))
           (word (buffer-local-value 'projectile-replace--word buffer))
           (pattern (buffer-local-value 'projectile-replace--rg-pattern buffer))
           (globs (projectile--project-ignore-globs root))
           (command (projectile-search--rg-command term case-fold word globs
                                                   pattern))
           (pending "")
           (continue
            (lambda ()
              (projectile-search--rg-scan-roots buffer term (cdr roots)
                                                generation on-done))))
      (let* (;; run rg IN the project root so the relative `./' search path and
             ;; the `/'-anchored ignore globs resolve against it
             (default-directory root)
             ;; keep rg's stderr out of the JSON stream, so a diagnostic line
             ;; (e.g. an unreadable directory) can't split a match across filter
             ;; chunks and get silently dropped
             (stderr-buffer (get-buffer-create " *projectile-search-rg-stderr*"))
             (_ (with-current-buffer stderr-buffer (erase-buffer)))
             (proc
              (make-process
               :name "projectile-search-rg"
               :buffer nil
               :command command
               :connection-type 'pipe
               :noquery t
               :stderr stderr-buffer
               :coding 'utf-8-unix
               :filter
               (lambda (_proc output)
                 (when (buffer-live-p buffer)
                   (with-current-buffer buffer
                     (unless projectile-replace--truncated
                       (setq pending (concat pending output))
                       (let ((parts (split-string pending "\n")))
                         ;; the last element is the (possibly empty) partial line
                         (setq pending (car (last parts)))
                         (when (projectile-search--rg-ingest
                                buffer (butlast parts) root)
                           (projectile-search--rg-finish buffer on-done)))))))
               :sentinel
               (lambda (proc _event)
                 (when (and (buffer-live-p buffer)
                            (memq (process-status proc) '(exit signal)))
                   (with-current-buffer buffer
                     (when (and (eq proc projectile-replace--scan-process)
                                (= generation projectile-replace--scan-generation))
                       (if (or projectile-replace--truncated
                               (projectile-search--rg-ingest
                                buffer (list pending) root))
                           (projectile-search--rg-finish buffer on-done)
                         (funcall continue)))))))))
        (with-current-buffer buffer
          (setq projectile-replace--scan-process proc))
        proc))))