From 8b669b50fb33e6af5423091a42338d2304d5d7e9 Mon Sep 17 00:00:00 2001 From: Innes Anderson-Morrison Date: Tue, 18 Feb 2025 11:29:41 +0000 Subject: [PATCH] adding xtask --- .cargo/config.toml | 2 + .github/workflows/rust.yml | 8 ++ Cargo.lock | 4 + Cargo.toml | 4 +- data/tree-sitter/queries/r/highlights.scm | 2 +- xtask/Cargo.toml | 4 + xtask/src/main.rs | 109 ++++++++++++++++++++++ 7 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 xtask/Cargo.toml create mode 100644 xtask/src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..35049cb --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +xtask = "run --package xtask --" diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 572f3ce..67d47e2 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -14,6 +14,14 @@ env: RUST_BACKTRACE: 1 jobs: + lint: + name: Lint non-Rust files + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: hecrj/setup-rust-action@v2 + - run: cargo xtask lint-ts-queries + test: name: Test Rust ${{ matrix.rust }} runs-on: ubuntu-latest diff --git a/Cargo.lock b/Cargo.lock index d6a2323..9ae6048 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1006,3 +1006,7 @@ checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" dependencies = [ "memchr", ] + +[[package]] +name = "xtask" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0880717..de3c980 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,14 +14,14 @@ include = [ "Cargo.toml", "README.md" ] -keywords = [ "terminal", "editor", "text-editor", ] +keywords = [ "terminal", "editor", "text-editor" ] categories = [ "development-tools", "text-editors", "command-line-utilities" ] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [workspace] -members = [ "crates/*" ] +members = [ "crates/*" , "xtask" ] [[bin]] doc = false diff --git a/data/tree-sitter/queries/r/highlights.scm b/data/tree-sitter/queries/r/highlights.scm index 5359ce4..9b0647d 100644 --- a/data/tree-sitter/queries/r/highlights.scm +++ b/data/tree-sitter/queries/r/highlights.scm @@ -12,7 +12,7 @@ (escape_sequence) @string.escape)) ; Comments -(comment) @comment @spell +(comment) @comment ; Operators [ diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 0000000..fe005f2 --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "xtask" +version = "0.1.0" +edition = "2021" diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 0000000..670afe7 --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,109 @@ +// https://github.com/matklad/cargo-xtask +use std::{ + env, fs, + path::{Path, PathBuf}, + process::exit, +}; + +type DynResult = Result<(), Box>; + +macro_rules! err { + ($msg:expr) => { + Err($msg.to_string().into()) + }; + + ($template:expr, $($arg:expr),+) => { + Err(format!($template, $($arg),+).into()) + }; +} + +fn main() { + if let Err(e) = try_main() { + eprintln!("{e}"); + exit(1); + } +} + +fn try_main() -> DynResult { + let task = env::args().nth(1); + + match task.as_deref() { + Some("lint-ts-queries") => lint_ts_queries()?, + + _ => list_tasks(), + } + + Ok(()) +} + +fn list_tasks() { + eprintln!( + "Available tasks: + * lint-ts-queries ensure that the tree-sitter queries in /data are valid" + ); +} + +fn lint_ts_queries() -> DynResult { + let unsupported_predicates = [ + // neovim + "any-contains?", + "any-lua-match?", + "any-vim-match?", + "contains?", + "has-ancestor?", + "has-parent?", + "lua-match?", + "vim-match?", + ]; + let mut valid = true; + eprintln!(">> Linting tree-sitter queries"); + + let query_root = project_root().join("data/tree-sitter/queries"); + for entry in fs::read_dir(query_root)? { + let path = entry?.path(); + if path.is_file() { + eprintln!( + "[x] unexpected file in data/tree-sitter/queries:\n{}", + path.display() + ); + valid = false; + } + + let lang = path.file_name().unwrap().to_string_lossy(); + let highlights = path.join("highlights.scm"); + eprintln!("[ ] checking highlights for {lang}..."); + + if !highlights.exists() { + eprintln!(" [x] no highlights.scm found for {lang}"); + valid = false; + continue; + } + + let query = fs::read_to_string(highlights)?; + for p in unsupported_predicates { + if query.contains(p) { + eprintln!(" [x] highlights for {lang} contain an unsupported predicate: {p}"); + valid = false; + } + } + if query.contains("@spell") { + eprintln!(" [x] highlights for {lang} contain '@spell' which needs to be removed"); + valid = false; + } + } + + if !valid { + return err!("validation failed: see logs for details"); + } + + eprintln!("\ntree-sitter queries linted successfully"); + Ok(()) +} + +fn project_root() -> PathBuf { + Path::new(&env!("CARGO_MANIFEST_DIR")) + .ancestors() + .nth(1) + .unwrap() + .to_path_buf() +}