Skip to content

Commit

Permalink
adding xtask
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Feb 18, 2025
1 parent a71da1f commit 8b669b5
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"
8 changes: 8 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion data/tree-sitter/queries/r/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(escape_sequence) @string.escape))

; Comments
(comment) @comment @spell
(comment) @comment

; Operators
[
Expand Down
4 changes: 4 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "xtask"
version = "0.1.0"
edition = "2021"
109 changes: 109 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// https://github.com/matklad/cargo-xtask
use std::{
env, fs,
path::{Path, PathBuf},
process::exit,
};

type DynResult = Result<(), Box<dyn std::error::Error>>;

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()
}

0 comments on commit 8b669b5

Please sign in to comment.