Add --check flag to tree-sitter highlight.

Recently I've been pulling a lot of grammars into GitHub's highlighting backend,
replacing legacy language support with tree-sitter highlighting queries.
Our backend systems have a standard set of highlight captures we expect, very
similar to the standard tagging captures we expect. Though end-user applications
are free to choose whatever tagging nomenclature they want, I think it's nice to
include a checking stage that will help us ensure that we know whether a capture
might be recognized or not. It will also help us figure out where we need to
expand our standard set of captures (see #1539).
This commit is contained in:
Patrick Thomson 2023-06-22 09:18:53 -04:00 committed by Amaan Qureshi
parent d30e9c9d71
commit cb58bc593f
No known key found for this signature in database
GPG key ID: E67890ADC4227273
4 changed files with 78 additions and 2 deletions

View file

@ -239,6 +239,11 @@ fn run() -> Result<()> {
.long("html")
.short("H"),
)
.arg(
Arg::with_name("check")
.help("Check that highlighting captures conform strictly to standards")
.long("check"),
)
.arg(&scope_arg)
.arg(&time_arg)
.arg(&quiet_arg)
@ -543,6 +548,7 @@ fn run() -> Result<()> {
let time = matches.is_present("time");
let quiet = matches.is_present("quiet");
let html_mode = quiet || matches.is_present("html");
let should_check = matches.is_present("check");
let paths = collect_paths(matches.value_of("paths-file"), matches.values_of("paths"))?;
if html_mode && !quiet {
@ -573,6 +579,25 @@ fn run() -> Result<()> {
};
if let Some(highlight_config) = language_config.highlight_config(language)? {
if should_check {
let names = highlight_config.nonconformant_capture_names();
if names.is_empty() {
eprintln!("All highlight captures conform to standards.");
} else {
eprintln!(
"Non-standard highlight {} detected:",
if names.len() > 1 {
"captures"
} else {
"capture"
}
);
for name in names {
eprintln!("* {}", name);
}
}
}
let source = fs::read(path)?;
if html_mode {
highlight::html(