Skip to content

Commit 61421db

Browse files
committed
Add inline comments to optimized font-locking functions
Explain the tricky parts of clojure--search-comment-macro-internal (match-data manipulation, #_ counting logic) and clojure--font-locked-as-string-p (face check semantics, syntax-ppss fields, regexp parameter behavior).
1 parent d4cf005 commit 61421db

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

clojure-mode.el

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,9 @@ what is considered a comment (affecting font locking).
788788

789789
(defun clojure--search-comment-macro-internal (limit)
790790
"Search for a comment forward stopping at LIMIT."
791+
;; Iterative search: find the next comment macro (`#_` or `(comment ...)`)
792+
;; that isn't inside a string or comment, then extend match-data group 1
793+
;; to cover the commented-out sexp(s).
791794
(let ((result nil))
792795
(while (and (not result)
793796
(search-forward-regexp clojure-comment-regexp limit t))
@@ -798,12 +801,16 @@ what is considered a comment (affecting font locking).
798801
(unless (or (nth 3 state)
799802
(nth 4 state))
800803
(goto-char start)
801-
;; Count how many #_ we got and step by that many sexps
802-
;; For (comment ...), step at least 1 sexp
804+
;; For #_#_expr, each #_ discards one sexp, so count them.
805+
;; For (comment ...), the regexp matches but has zero #_,
806+
;; so max with 1 ensures we skip the comment body.
803807
(clojure-forward-logical-sexp
804808
(max (count-matches (rx "#_") (elt md 0) (elt md 1))
805809
1))
806-
;; Data for (match-end 1).
810+
;; match-data is a flat list [beg0 end0 beg1 end1 ...],
811+
;; so index 3 = end of group 1. Set it to point (after
812+
;; stepping over the commented sexps) so that group 1 spans
813+
;; from the macro to the end of the discarded code.
807814
(setf (elt md 3) (point))
808815
(set-match-data md)
809816
(setq result t))))
@@ -1264,16 +1271,23 @@ locking in def* forms that are not at top level."
12641271
"Non-nil if the char before point is font-locked as a string.
12651272
If REGEXP is non-nil, also check whether current string is
12661273
preceded by a #."
1274+
;; Check font-lock-string-face specifically (not font-lock-doc-face)
1275+
;; so that docstrings are excluded from matching.
12671276
(let ((face (get-text-property (1- (point)) 'face)))
12681277
(when (or (and (listp face)
12691278
(memq 'font-lock-string-face face))
12701279
(eq 'font-lock-string-face face))
1280+
;; Single syntax-ppss call: nth 3 = in-string flag,
1281+
;; nth 8 = start position of the string.
12711282
(let* ((ppss (syntax-ppss))
12721283
(string-beg (nth 8 ppss)))
12731284
(when (and (nth 3 ppss) string-beg)
12741285
(if regexp
1286+
;; Regex strings are preceded by # (e.g. #"pattern").
1287+
;; Return position including the # prefix.
12751288
(when (eq ?# (char-before string-beg))
12761289
(1- string-beg))
1290+
;; When regexp is nil, match any string (both #"..." and "...").
12771291
string-beg))))))
12781292

12791293
(defun clojure-font-lock-escaped-chars (bound)

0 commit comments

Comments
 (0)