Function: c-ts-mode--anchor-prev-sibling

c-ts-mode--anchor-prev-sibling is a byte-compiled function defined in c-ts-mode.el.gz.

Signature

(c-ts-mode--anchor-prev-sibling NODE PARENT BOL &rest _)

Documentation

Return the start of the previous named sibling of NODE.

This anchor handles the special case where the previous sibling is a labeled_statement, in that case, return the child of the labeled statement instead. (Actually, recursively go down until
the node isn't a labeled_statement.) Eg,

label:
  int x = 1;
  int y = 2;

The anchor of "int y = 2;" should be "int x = 1;" rather than the labeled_statement.

Return nil if a) there is no prev-sibling, or 2) prev-sibling doesn't have a child.

PARENT and BOL are like other anchor functions.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/c-ts-mode.el.gz
(defun c-ts-mode--anchor-prev-sibling (node parent bol &rest _)
  "Return the start of the previous named sibling of NODE.

This anchor handles the special case where the previous sibling
is a labeled_statement, in that case, return the child of the
labeled statement instead.  (Actually, recursively go down until
the node isn't a labeled_statement.)  Eg,

label:
  int x = 1;
  int y = 2;

The anchor of \"int y = 2;\" should be \"int x = 1;\" rather than
the labeled_statement.

Return nil if a) there is no prev-sibling, or 2) prev-sibling
doesn't have a child.

PARENT and BOL are like other anchor functions."
  (when-let ((prev-sibling
              (or (treesit-node-prev-sibling node t)
                  (treesit-node-prev-sibling
                   (treesit-node-first-child-for-pos parent bol) t)
                  (treesit-node-child parent -1 t)))
             (continue t))
    (save-excursion
      (while (and prev-sibling continue)
        (pcase (treesit-node-type prev-sibling)
          ;; Get the statement in the label.
          ("labeled_statement"
           (setq prev-sibling (treesit-node-child prev-sibling 2)))
          ;; Get the last statement in the preproc.  Tested by
          ;; "Prev-Sibling When Prev-Sibling is Preproc" test.
          ((or "preproc_if" "preproc_ifdef")
           (setq prev-sibling (treesit-node-child prev-sibling -2)))
          ((or "preproc_elif" "preproc_else")
           (setq prev-sibling (treesit-node-child prev-sibling -1)))
          ((or "#elif" "#else")
           (setq prev-sibling (treesit-node-prev-sibling
                               (treesit-node-parent prev-sibling) t)))
          ;; If the start of the previous sibling isn't at the
          ;; beginning of a line, something's probably not quite
          ;; right, go a step further. (E.g., comment after a
          ;; statement.)
          (_ (goto-char (treesit-node-start prev-sibling))
             (if (looking-back (rx bol (* whitespace))
                               (line-beginning-position))
                 (setq continue nil)
               (setq prev-sibling
                     (treesit-node-prev-sibling prev-sibling)))))))
    ;; This could be nil if a) there is no prev-sibling or b)
    ;; prev-sibling doesn't have a child.
    (treesit-node-start prev-sibling)))