Function: python--treesit-fontify-string

python--treesit-fontify-string is a byte-compiled function defined in python.el.gz.

Signature

(python--treesit-fontify-string NODE OVERRIDE START END &rest _)

Documentation

Fontify string.

NODE is the string node. Do not fontify the initial f for f-strings. OVERRIDE is the override flag described in treesit-font-lock-rules. START and END mark the region to be fontified.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
(defun python--treesit-fontify-string (node override start end &rest _)
  "Fontify string.
NODE is the string node.  Do not fontify the initial f for
f-strings.  OVERRIDE is the override flag described in
`treesit-font-lock-rules'.  START and END mark the region to be
fontified."
  (let* ((maybe-expression (treesit-node-parent node))
         (grandparent (treesit-node-parent
                       (treesit-node-parent
                        maybe-expression)))
         (maybe-defun grandparent)
         (face (if (and (or (member (treesit-node-type maybe-defun)
                                    '("function_definition"
                                      "class_definition"))
                            ;; If the grandparent is null, meaning the
                            ;; string is top-level, and the string has
                            ;; no node or only comment preceding it,
                            ;; it's a BOF docstring.
                            (and (null grandparent)
                                 (cl-loop
                                  for prev = (treesit-node-prev-sibling
                                              maybe-expression)
                                  then (treesit-node-prev-sibling prev)
                                  while prev
                                  if (not (equal (treesit-node-type prev)
                                                 "comment"))
                                  return nil
                                  finally return t)))
                        ;; This check filters out this case:
                        ;; def function():
                        ;;     return "some string"
                        (equal (treesit-node-type maybe-expression)
                               "expression_statement"))
                   'font-lock-doc-face
                 'font-lock-string-face))

         (ignore-interpolation (not
                                (seq-some
                                 (lambda (feats) (memq 'string-interpolation feats))
                                 (seq-take treesit-font-lock-feature-list treesit-font-lock-level))))
         ;; If interpolation is enabled, highlight only
         ;; string_start/string_content/string_end children.  Do not
         ;; touch interpolation node that can occur inside of the
         ;; string.
         (string-nodes (if ignore-interpolation
                           (list node)
                         (treesit-filter-child
                          node
                          (lambda (ch) (member (treesit-node-type ch)
                                               '("string_start"
                                                 "string_content"
                                                 "string_end")))
                          t))))

    (dolist (string-node string-nodes)
      (let ((string-beg (treesit-node-start string-node))
            (string-end (treesit-node-end string-node)))
        (when (or ignore-interpolation
                  (equal (treesit-node-type string-node) "string_start"))
          ;; Don't highlight string prefixes like f/r/b.
          (save-excursion
            (goto-char string-beg)
            (when (re-search-forward "[\"']" string-end t)
              (setq string-beg (match-beginning 0)))))

        (treesit-fontify-with-override
         string-beg string-end face override start end)))))