Add the comment extractor and the language detector of the English-comments check

tools/english_comments.py reads the comments and docstrings of the languages of
ADR-0010 (the scanner of count_loc.py now returns the comment text), joins the
lines of a block, removes everything that is not a word and judges the block
with langid (ADR-0012). It checks the lines a branch adds, or every tracked
file, honours an ignore marker and an allowed-words file, and prints each
finding as an annotation. The rates of the corpus are tests.

Refs #199
Refs #200
Refs #203
Task: MIL-013#3
Task: MIL-013#4
This commit is contained in:
2026-10-19 12:34:00 +08:00
parent 7d8cc4522e
commit b8e63d0a29
5 changed files with 682 additions and 3 deletions
+16 -2
View File
@@ -161,8 +161,17 @@ def _skip_string(line: str, start: int, quote: str, escapes: bool) -> int:
return len(line)
def _scan_line(line: str, language: Language, state: _State) -> tuple[bool, bool]:
"""Scan one line; return (has_code, has_comment) and update the state."""
def _scan_line(
line: str,
language: Language,
state: _State,
comments: list[str] | None = None,
) -> tuple[bool, bool]:
"""Scan one line; return (has_code, has_comment) and update the state.
When ``comments`` is given, the text of every comment on the line is
appended to it (the English-comments check, ADR-0012, reads that text).
"""
has_code = False
has_comment = False
i = 0
@@ -174,6 +183,9 @@ def _scan_line(line: str, language: Language, state: _State) -> tuple[bool, bool
has_comment = True
end = line.find(block[1], i)
nested = line.find(block[0], i) if language.nested_block else -1
if comments is not None:
stop = nested if nested != -1 and (end == -1 or nested < end) else end
comments.append(line[i : size if stop == -1 else stop])
if nested != -1 and (end == -1 or nested < end):
state.block_depth += 1
i = nested + len(block[0])
@@ -212,6 +224,8 @@ def _scan_line(line: str, language: Language, state: _State) -> tuple[bool, bool
if comment is not None and line.startswith(comment, i):
word_start = i == 0 or line[i - 1] in _WORD_START
if word_start or not language.hash_at_word_start:
if comments is not None:
comments.append(line[i + len(comment) :])
return has_code, True
if language.triple_quotes and (
line.startswith('"""', i) or line.startswith("'''", i)