Function: bazel--display-coverage

bazel--display-coverage is a natively compiled function defined in bazel.el.

Signature

(bazel--display-coverage BUFFER COVERAGE)

Documentation

Add overlays for coverage information in BUFFER.

COVERAGE is a hashtable mapping line numbers to ‘bazel--coverage’ structures. Remove existing coverage overlays first.

Source Code

;; Defined in /nix/store/ws8pkszjvcad1rgzqw6wja7qxacylfhn-emacs-packages-deps/share/emacs/site-lisp/elpa/bazel-20261002.1510/bazel.el
(defun bazel--display-coverage (buffer coverage)
  "Add overlays for coverage information in BUFFER.
COVERAGE is a hashtable mapping line numbers to ‘bazel--coverage’
structures.  Remove existing coverage overlays first."
  (declare (ftype (function (buffer hashtable) null)))
  (cl-check-type buffer buffer)
  (cl-check-type coverage hash-table)
  (let ((pairs ())
        (runs ())
        (some-branch-coverage nil)
        (margin-width 20))
    (maphash
     (lambda (line coverage)
       (cl-check-type line natnum)
       (cl-check-type coverage bazel--coverage)
       (let ((face (if (cl-plusp (bazel--coverage-hits coverage))
                       'bazel-covered-line
                     'bazel-uncovered-line))
             (branches (when-let* ((blocks (bazel--coverage-blocks coverage)))
                         (bazel--branch-coverage-string blocks))))
         (push (list line face branches) pairs)))
     coverage)
    (cl-callf sort pairs #'car-less-than-car)
    ;; The Emacs Lisp manual cautions that overlays don’t scale well; see Info
    ;; node ‘(elisp) Overlays’.  Therefore, we create as few overlays as
    ;; possible by grouping consecutive lines with identical coverage status
    ;; into runs.
    (while pairs
      (cl-loop with (i . f) = (car pairs)
               for tail on (cdr pairs)
               for (j . g) = (car tail)
               for k from (1+ i)  ; expected line number for the next element
               while (and (equal f g) (eql j k))  ; stop as soon as run ends
               finally (push (list f i (1- k)) runs) (setq pairs tail)))
    (cl-callf nreverse runs)
    (with-current-buffer buffer
      (save-excursion
        (without-restriction
          ;; ‘line-beginning-position’ and ‘line-end-position’ below count lines
          ;; starting from the current line, so to be able to use absolute line
          ;; numbers from ‘coverage.dat’, we need to go to the very beginning of
          ;; the buffer.
          (goto-char (point-min))
          ;; See remark at the bottom of Info node ‘(elisp) Managing Overlays’.
          (overlay-recenter (point-max))
          ;; We first remove existing coverage overlays because they are likely
          ;; stale.
          (bazel--remove-coverage-overlays)
          (cl-loop for ((face branches) i j) in runs
                   ;; Choose overlay positions and stickiness so that inserting
                   ;; text before or after a run doesn’t appear to extend the
                   ;; covered region.
                   for o = (make-overlay (line-beginning-position i)
                                         (line-end-position j)
                                         buffer :front-advance)
                   do
                   ;; Add ‘category’ property to find the overlays later (see
                   ;; ‘bazel-remove-coverage-display’).
                   (overlay-put o 'category 'bazel-coverage)
                   (overlay-put o 'face face)
                   (when branches
                     (bazel--add-left-margin
                      (truncate-string-to-width branches margin-width
                                                nil nil "…")
                      o)
                     (setq some-branch-coverage t)))))
      (when (and some-branch-coverage (< left-margin-width margin-width))
        (setq left-margin-width margin-width)
        (bazel--update-margin-display))))
  nil)