|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Validate highlight captures against Zed's syntax capture names.""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import re |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +CAPTURE_PATTERN = re.compile(r"@([A-Za-z0-9_.-]+)") |
| 13 | +ALLOWED_ZED_CAPTURES = { |
| 14 | + "attribute", |
| 15 | + "boolean", |
| 16 | + "comment", |
| 17 | + "comment.doc", |
| 18 | + "constant", |
| 19 | + "constructor", |
| 20 | + "embedded", |
| 21 | + "emphasis", |
| 22 | + "emphasis.strong", |
| 23 | + "enum", |
| 24 | + "function", |
| 25 | + "function.builtin", |
| 26 | + "hint", |
| 27 | + "keyword", |
| 28 | + "label", |
| 29 | + "link_text", |
| 30 | + "link_uri", |
| 31 | + "namespace", |
| 32 | + "number", |
| 33 | + "operator", |
| 34 | + "predictive", |
| 35 | + "preproc", |
| 36 | + "primary", |
| 37 | + "property", |
| 38 | + "punctuation", |
| 39 | + "punctuation.bracket", |
| 40 | + "punctuation.delimiter", |
| 41 | + "punctuation.list_marker", |
| 42 | + "punctuation.markup", |
| 43 | + "punctuation.special", |
| 44 | + "selector", |
| 45 | + "selector.pseudo", |
| 46 | + "string", |
| 47 | + "string.escape", |
| 48 | + "string.regex", |
| 49 | + "string.special", |
| 50 | + "string.special.symbol", |
| 51 | + "tag", |
| 52 | + "text", |
| 53 | + "text.literal", |
| 54 | + "title", |
| 55 | + "type", |
| 56 | + "variable", |
| 57 | + "variable.special", |
| 58 | + "variant", |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +def is_allowed(capture: str) -> bool: |
| 63 | + if capture.startswith("_"): |
| 64 | + return True |
| 65 | + |
| 66 | + parts = capture.split(".") |
| 67 | + for i in range(len(parts), 0, -1): |
| 68 | + prefix = ".".join(parts[:i]) |
| 69 | + if prefix in ALLOWED_ZED_CAPTURES: |
| 70 | + return True |
| 71 | + return False |
| 72 | + |
| 73 | + |
| 74 | +def collect_highlight_files(root: Path) -> list[Path]: |
| 75 | + return sorted(root.glob("languages/*/highlights.scm")) |
| 76 | + |
| 77 | + |
| 78 | +def main() -> int: |
| 79 | + root = Path.cwd() |
| 80 | + highlight_files = collect_highlight_files(root) |
| 81 | + if not highlight_files: |
| 82 | + print("No highlights.scm files found under languages/*/") |
| 83 | + return 1 |
| 84 | + |
| 85 | + failures: list[tuple[Path, int, str]] = [] |
| 86 | + for highlight_file in highlight_files: |
| 87 | + text = highlight_file.read_text(encoding="utf-8") |
| 88 | + for line_number, line in enumerate(text.splitlines(), start=1): |
| 89 | + for match in CAPTURE_PATTERN.finditer(line): |
| 90 | + capture = match.group(1) |
| 91 | + if not is_allowed(capture): |
| 92 | + failures.append((highlight_file, line_number, capture)) |
| 93 | + |
| 94 | + if failures: |
| 95 | + print("Unsupported highlight captures detected:") |
| 96 | + for path, line_number, capture in failures: |
| 97 | + relative_path = path.relative_to(root) |
| 98 | + print(f"- {relative_path}:{line_number} uses @{capture}") |
| 99 | + print("\nUse Zed capture names (or subscopes of them) for highlights.") |
| 100 | + return 1 |
| 101 | + |
| 102 | + print("All highlight captures are compatible with Zed capture names.") |
| 103 | + return 0 |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + sys.exit(main()) |
0 commit comments