c-ts-mode: Don't assume comment-start-skip is set

This patch makes it easier to use the existing C and C++
language support in other languages.  Without this patch, if the
outer mode sets some specific and more accurate
comment-start-skip value, or perhaps leaves it unset,
indentation will break.

* lisp/progmodes/c-ts-common.el (c-ts-common--comment-start-skip):
Declare to the value c-ts-common-comment-setup used to set as
comment-start-skip.
(c-ts-common-comment-setup): Use it, rather than hardcoding.
(c-ts-common-comment-2nd-line-anchor): Use it, rather than
comment-start-skip.  This makes it easier to reuse C/++
indentation rules in other TS modes for embedding C/++ segments
in other languages.

Copyright-paperwork-exempt: yes
This commit is contained in:
Arsen Arsenović
2026-02-16 16:11:42 +01:00
committed by Yuan Fu
parent 47735e0243
commit 0a80c05df0

View File

@@ -90,7 +90,7 @@ non-whitespace characters of the current line."
(save-excursion
(forward-line -1)
(back-to-indentation)
(when (looking-at comment-start-skip)
(when (looking-at c-ts-common--comment-start-skip)
(goto-char (match-end 0))
(if (looking-at (rx (* (or " " "\t")) eol))
;; Only /* at the first line.
@@ -274,6 +274,12 @@ This function should be called at BOL. Used by
;; `adaptive-fill-first-line-regexp' decide.
(t nil)))
(defvar c-ts-common--comment-start-skip
(rx (or (seq "/" (+ "/"))
(seq "/" (+ "*")))
(* (syntax whitespace)))
"`comment-start-skip' value for C and C++ tree-sitter modes.")
(defun c-ts-common-comment-setup ()
"Set up local variables for C-like comment.
@@ -291,9 +297,7 @@ Set up:
- `comment-multi-line'"
(setq-local comment-start "// ")
(setq-local comment-end "")
(setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
(seq "/" (+ "*")))
(* (syntax whitespace))))
(setq-local comment-start-skip c-ts-common--comment-start-skip)
(setq-local comment-end-skip
(rx (* (syntax whitespace))
(group (or (syntax comment-end)