Function: bazel--consuming-target

bazel--consuming-target is a natively compiled function defined in bazel.el.

Signature

(bazel--consuming-target BUILD-FILE SOURCE-FILE CASE-FOLD-FILE ONLY-TESTS)

Documentation

Return the name of the rule target in BUILD-FILE that consumes SOURCE-FILE.

If CASE-FOLD-FILE is non-nil, ignore filename case when searching. If ONLY-TESTS is non-nil, look only for test targets. Return nil if no consuming rule target was found.

Source Code

;; Defined in /nix/store/ws8pkszjvcad1rgzqw6wja7qxacylfhn-emacs-packages-deps/share/emacs/site-lisp/elpa/bazel-20261002.1510/bazel.el
(defun bazel--consuming-target
    (build-file source-file case-fold-file only-tests)
  "Return the name of the rule target in BUILD-FILE that consumes SOURCE-FILE.
If CASE-FOLD-FILE is non-nil, ignore filename case when
searching.  If ONLY-TESTS is non-nil, look only for test targets.
Return nil if no consuming rule target was found."
  (declare (ftype (function (string string t t) (or string null))))
  (cl-check-type build-file string)
  (cl-check-type source-file string)
  ;; Prefer a buffer that’s already visiting BUILD-FILE.
  (bazel--with-file-buffer existing build-file
    (unless existing (bazel-build-mode))  ; for correct syntax tables
    (let ((case-fold-search nil)
          (search-spaces-regexp nil))
      (save-excursion
        ;; Don’t widen; if the rule target definition isn’t found within the
        ;; accessible portion of the current buffer, that’s probably what the
        ;; user wants.
        (goto-char (point-min))
        ;; We perform a simple textual search for rule target definitions with
        ;; “srcs” attributes that contain references to SOURCE-FILE.  That’s in
        ;; no way exact, but faster than invoking “bazel query”, and most BUILD
        ;; files are regular enough for this approach to give acceptable
        ;; results.
        (cl-block nil
          (while (let ((case-fold-search case-fold-file))
                   (re-search-forward (rx (group (any ?\" ?\'))
                                          (? ?:) (literal source-file)
                                          (backref 1))
                                      nil t))
            (let ((begin (match-beginning 0))
                  (end (match-end 0)))
              (goto-char begin)
              (python-nav-up-list -1)  ; move to start of attribute value
              (when (looking-back
                     (rx symbol-start "srcs" (* blank) ?= (* blank))
                     (line-beginning-position))
                (when-let* ((target-name (bazel-current-target-name)))
                  (when (or (not only-tests) (bazel--in-test-target-p))
                    (cl-return target-name))))
              ;; Ensure we don’t loop forever if we ended up in a weird place.
              (goto-char end))))))))