Function: web-mode-part-scan

web-mode-part-scan is a natively compiled function defined in web-mode.el.

Signature

(web-mode-part-scan REG-BEG REG-END &optional CONTENT-TYPE DEPTH)

Source Code

;; Defined in /nix/store/2qgnz1rp30nzfz6w0apx4kdm14if1bbs-emacs-packages-deps/share/emacs/site-lisp/elpa/web-mode-20260831.219/web-mode.el
(defun web-mode-part-scan (reg-beg reg-end &optional content-type depth)
  (save-excursion
    (let (token-re ch-before ch-at ch-next token-type beg continue)
      ;;(message "%S %S" reg-beg reg-end)
      (cond
        (content-type
         )
        ((member web-mode-content-type web-mode-part-content-types)
         (setq content-type web-mode-content-type))
        (t
         (setq content-type (symbol-name (get-text-property reg-beg 'part-side))))
        ) ;cond

      (goto-char reg-beg)

      (cond
        ((member content-type '("javascript" "json"))
         (setq token-re "/\\|\"\\|'\\|`"))
        ((member content-type '("typescript"))
         (setq token-re "/\\|\"\\|'\\|`\\|//\\|/\\*"))
        ((member content-type '("jsx"))
         (setq token-re "/\\|\"\\|'\\|`\\|</?[[:alpha:]>]"))
        ((string= web-mode-content-type "css")
         (setq token-re "\"\\|'\\|/\\*\\|//"))
        ((string= content-type "css")
         (setq token-re "\"\\|'\\|/\\*"))
        (t
         (setq token-re "/\\*\\|\"\\|'"))
        )

      (while (and token-re (< (point) reg-end) (web-mode-dom-rsf token-re reg-end t))

        (setq beg (match-beginning 0)
              token-type nil
              continue t
              ch-at (char-after beg)
              ch-next (or (char-after (1+ beg)) ?\d)
              ch-before (or (char-before beg) ?\d))

        ;;(message "[%S>%S|%S] %S %c %c %c" reg-beg reg-end depth beg ch-before ch-at ch-next)

        (cond

          ((eq ?\' ch-at)
           (while (and continue (search-forward "'" reg-end t))
             (cond
               ((get-text-property (1- (point)) 'block-side)
                (setq continue t))
               (t
                (setq continue (web-mode-string-continue-p reg-beg)))
               )
             ) ;while
           (setq token-type 'string))

          ((eq ?\` ch-at)
           (while (and continue (search-forward "`" reg-end t))
             (cond
               ((get-text-property (1- (point)) 'block-side)
                (setq continue t))
               (t
                (setq continue (web-mode-string-continue-p reg-beg)))
               )
             ) ;while
           (setq token-type 'string))

          ((eq ?\" ch-at)
           (while (and continue (search-forward "\"" reg-end t))
             (cond
               ((get-text-property (1- (point)) 'block-side)
                (setq continue t))
               (t
                (setq continue (web-mode-string-continue-p reg-beg)))
               ) ;cond
             ) ;while
           (cond
             ((string= content-type "json")
              (if (looking-at-p "[ ]*:")
                  (cond
                    ((eq ?\@ (char-after (1+ beg)))
                     (setq token-type 'context))
                    (t
                     (setq token-type 'key))
                    )
                  (setq token-type 'string))
              ) ;json
             (t
              (setq token-type 'string))
             ) ;cond
           )

          ((and (eq ?\< ch-at)
                (not (or (and (>= ch-before 97) (<= ch-before 122)) ;; a-z
                         (and (>= ch-before 65) (<= ch-before 90))))) ;; A-Z
           ;;(message "before [%S>%S|%S] pt=%S" reg-beg reg-end depth (point))
           (search-backward "<")
           (if (web-mode-jsx-skip reg-end)
               (web-mode-jsx-scan-element beg (point) depth)
               (forward-char))
           ;;(message "after [%S>%S|%S] pt=%S" reg-beg reg-end depth (point))
           )

          ((and (eq ?\/ ch-at) (member content-type '("javascript" "jsx" "typescript")))
           (cond
             ((eq ?\\ ch-before)
              )
             ((eq ?\* ch-next)
              ;;(message "--> %S %S" (point) reg-end)
              (when (search-forward "*/" reg-end t)
                (setq token-type 'comment))
              )
             ((eq ?\/ ch-next)
              (setq token-type 'comment)
              (goto-char (if (< reg-end (line-end-position)) reg-end (line-end-position)))
              )
             ((and (looking-at-p ".*/")
                   (looking-back "\\(^\\|case\\|[[(,=:!&|?{};]\\)[ ]*/" (point-min)))
              ;;(re-search-forward "/[gimyu]*" reg-end t))
              (let ((eol (line-end-position)))
                (while (and continue (search-forward "/" eol t))
                  (cond
                    ((get-text-property (1- (point)) 'block-side)
                     (setq continue t))
                    ((looking-back "\\\\+/" reg-beg t)
                     (setq continue (= (mod (- (point) (match-beginning 0)) 2) 0)))
                    (t
                     (re-search-forward "[gimyu]*" eol t)
                     (setq token-type 'string)
                     (setq continue nil))
                    )
                  ) ;while
                ) ;let
              )
             ) ;cond
           )

          ((eq ?\/ ch-next)
           ;;(message "%S" (point))
           (cond
             ((and (string= content-type "css")
                   (eq ?/ ch-at)
                   (eq ?: ch-before))
              )
             (t
              (unless (eq ?\\ ch-before)
                (setq token-type 'comment)
                (goto-char (if (< reg-end (line-end-position)) reg-end (line-end-position)))
                )
              )
             )

           )

          ((eq ?\* ch-next)
           (cond
             ((search-forward "*/" reg-end t)
              (setq token-type 'comment))
             ((not (eobp))
              (forward-char))
             ) ;cond
           )

          ) ;cond

        (when (and beg (>= reg-end (point)) token-type)
          (put-text-property beg (point) 'part-token token-type)
          (cond
            ((eq token-type 'comment)
             (put-text-property beg (1+ beg) 'syntax-table (string-to-syntax "<"))
             (when (< (point) (point-max))
               (if (< (point) (line-end-position))
                   (put-text-property (1- (point)) (point) 'syntax-table (string-to-syntax ">")) ;#445
                   (put-text-property (point) (1+ (point)) 'syntax-table (string-to-syntax ">")) ;#377
                   )
               ) ;when
             ) ;comment
            ((eq token-type 'string)
             (put-text-property beg (1+ beg) 'syntax-table (string-to-syntax "|"))
             (when (< (point) (point-max))
               (if (< (point) (line-end-position))
                   (put-text-property (1- (point)) (point) 'syntax-table (string-to-syntax "|"))
                   (put-text-property (point) (1+ (point)) 'syntax-table (string-to-syntax "|"))
                   )
               ) ;when
             ) ;string
            ) ;cond
          ) ;when

        (when (> (point) reg-end)
          (message "reg-beg(%S) reg-end(%S) token-type(%S) point(%S)" reg-beg reg-end token-type (point)))

        ;;(message "#[%S>%S|%S] %S %c %c %c | (%S)" reg-beg reg-end depth beg ch-before ch-at ch-next (point))

        ) ;while

      )))