From 0220d9d93d85658b79aef490f19324ba3cbd3104 Mon Sep 17 00:00:00 2001 From: Will Lillis Date: Sun, 13 Apr 2025 01:23:54 -0400 Subject: [PATCH] feat(cli): add `grammar-path` to `init`, `test`, `version`, `fuzz`, `query`, `highlight` and `tags` subcommands Allows users to run various commands on a grammar without being inside the directory for said grammar. --- cli/src/main.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index eadb68f9..94286797 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -80,6 +80,9 @@ struct Init { /// Update outdated files #[arg(long, short)] pub update: bool, + /// The path to the tree-sitter grammar directory + #[arg(long, short = 'p')] + pub grammar_path: Option, } #[derive(Args)] @@ -146,7 +149,7 @@ struct Build { pub output: Option, /// The path to the grammar directory #[arg(index = 1, num_args = 1)] - pub path: Option, + pub path: Option, /// Make the parser reuse the same allocator as the library #[arg(long)] pub reuse_allocator: bool, @@ -164,6 +167,9 @@ struct Parse { /// The source file(s) to use #[arg(num_args=1..)] pub paths: Option>, + /// The path to the tree-sitter grammar directory + #[arg(long, short = 'p')] + pub grammar_path: Option, /// Select a language by the scope instead of a file extension #[arg(long)] pub scope: Option, @@ -252,6 +258,9 @@ struct Test { /// Only run corpus test cases from from a given filename #[arg(long)] pub file_name: Option, + /// The path to the tree-sitter grammar directory + #[arg(long, short = 'p')] + pub grammar_path: Option, /// Update all syntax trees in corpus files with current parser output #[arg(long, short)] pub update: bool, @@ -294,6 +303,9 @@ struct Version { #[arg(num_args = 1)] /// The version to bump to pub version: SemverVersion, + /// The path to the tree-sitter grammar directory + #[arg(long, short = 'p')] + pub grammar_path: Option, } #[derive(Args)] @@ -305,6 +317,9 @@ struct Fuzz { /// Subdirectory to the language #[arg(long)] pub subdir: Option, + /// The path to the tree-sitter grammar directory + #[arg(long, short = 'p')] + pub grammar_path: Option, /// Maximum number of edits to perform per fuzz test #[arg(long)] pub edits: Option, @@ -334,6 +349,9 @@ struct Query { /// Path to a file with queries #[arg(index = 1, required = true)] query_path: PathBuf, + /// The path to the tree-sitter grammar directory + #[arg(long, short = 'p')] + pub grammar_path: Option, /// Measure execution time #[arg(long, short)] pub time: bool, @@ -403,6 +421,9 @@ struct Highlight { /// The source file(s) to use #[arg(num_args = 1..)] pub paths: Option>, + /// The path to the tree-sitter grammar directory + #[arg(long, short = 'p')] + pub grammar_path: Option, /// The path to an alternative config.json file #[arg(long)] pub config_path: Option, @@ -429,6 +450,9 @@ struct Tags { /// The source file(s) to use #[arg(num_args = 1..)] pub paths: Option>, + /// The path to the tree-sitter grammar directory + #[arg(long, short = 'p')] + pub grammar_path: Option, /// The path to an alternative config.json file #[arg(long)] pub config_path: Option, @@ -446,7 +470,7 @@ struct Playground { pub quiet: bool, /// Path to the directory containing the grammar and wasm files #[arg(long)] - pub grammar_path: Option, + pub grammar_path: Option, } #[derive(Args)] @@ -789,7 +813,7 @@ impl Generate { impl Build { fn run(self, mut loader: loader::Loader, current_dir: &Path) -> Result<()> { - let grammar_path = current_dir.join(self.path.as_deref().unwrap_or_default()); + let grammar_path = current_dir.join(self.path.unwrap_or_default()); if self.docker { eprintln!("Warning: --docker flag is no longer used, and will be removed in a future release."); @@ -1705,7 +1729,25 @@ fn run() -> Result<()> { let command = Commands::from_arg_matches(&cli.clone().get_matches())?; - let current_dir = env::current_dir().unwrap(); + let current_dir = match &command { + Commands::Init(Init { grammar_path, .. }) + | Commands::Parse(Parse { grammar_path, .. }) + | Commands::Test(Test { grammar_path, .. }) + | Commands::Version(Version { grammar_path, .. }) + | Commands::Fuzz(Fuzz { grammar_path, .. }) + | Commands::Query(Query { grammar_path, .. }) + | Commands::Highlight(Highlight { grammar_path, .. }) + | Commands::Tags(Tags { grammar_path, .. }) + | Commands::Playground(Playground { grammar_path, .. }) => grammar_path, + Commands::Build(_) + | Commands::Generate(_) + | Commands::InitConfig(_) + | Commands::DumpLanguages(_) + | Commands::Complete(_) => &None, + } + .as_ref() + .map_or_else(|| env::current_dir().unwrap(), |p| p.clone()); + let loader = loader::Loader::new()?; match command {