2021-06-09 12:32:22 -04:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2019-01-17 15:15:40 -08:00
|
|
|
use clap::{App, AppSettings, Arg, SubCommand};
|
2019-12-06 11:41:21 -08:00
|
|
|
use glob::glob;
|
2019-01-07 17:57:27 -08:00
|
|
|
use std::path::Path;
|
2019-08-07 17:41:45 -07:00
|
|
|
use std::{env, fs, u64};
|
2019-02-25 12:33:24 -08:00
|
|
|
use tree_sitter_cli::{
|
2021-06-09 12:51:28 -04:00
|
|
|
config, generate, highlight, logger, parse, query, tags, test, test_highlight, util, wasm,
|
|
|
|
|
web_ui,
|
2019-02-25 12:33:24 -08:00
|
|
|
};
|
2021-06-09 12:51:28 -04:00
|
|
|
use tree_sitter_loader as loader;
|
2018-12-05 12:50:12 -08:00
|
|
|
|
2019-06-06 13:06:28 -07:00
|
|
|
const BUILD_VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
|
|
|
|
const BUILD_SHA: Option<&'static str> = option_env!("BUILD_SHA");
|
|
|
|
|
|
2021-06-09 12:32:22 -04:00
|
|
|
fn main() -> Result<()> {
|
2021-06-09 15:13:14 -04:00
|
|
|
let result = run();
|
|
|
|
|
// Ignore BrokenPipe errors
|
|
|
|
|
if let Err(err) = &result {
|
|
|
|
|
if let Some(error) = err.downcast_ref::<std::io::Error>() {
|
|
|
|
|
if error.kind() == std::io::ErrorKind::BrokenPipe {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run() -> Result<()> {
|
2019-06-06 13:06:28 -07:00
|
|
|
let version = if let Some(build_sha) = BUILD_SHA {
|
|
|
|
|
format!("{} ({})", BUILD_VERSION, build_sha)
|
|
|
|
|
} else {
|
|
|
|
|
BUILD_VERSION.to_string()
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-05 12:50:12 -08:00
|
|
|
let matches = App::new("tree-sitter")
|
2019-06-06 13:06:28 -07:00
|
|
|
.version(version.as_str())
|
2019-01-17 15:15:40 -08:00
|
|
|
.setting(AppSettings::SubcommandRequiredElseHelp)
|
2018-12-05 12:50:12 -08:00
|
|
|
.author("Max Brunsfeld <maxbrunsfeld@gmail.com>")
|
|
|
|
|
.about("Generates and tests parsers")
|
2019-02-25 12:33:24 -08:00
|
|
|
.subcommand(SubCommand::with_name("init-config").about("Generate a default config file"))
|
2019-01-02 16:48:44 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("generate")
|
|
|
|
|
.about("Generate a parser")
|
2019-01-14 14:07:42 -08:00
|
|
|
.arg(Arg::with_name("grammar-path").index(1))
|
2019-01-04 09:11:44 -08:00
|
|
|
.arg(Arg::with_name("log").long("log"))
|
2019-12-06 11:51:55 -08:00
|
|
|
.arg(Arg::with_name("prev-abi").long("prev-abi"))
|
2021-03-08 12:01:45 -08:00
|
|
|
.arg(Arg::with_name("no-bindings").long("no-bindings"))
|
2019-05-20 13:25:01 -07:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("report-states-for-rule")
|
|
|
|
|
.long("report-states-for-rule")
|
|
|
|
|
.value_name("rule-name")
|
|
|
|
|
.takes_value(true),
|
|
|
|
|
)
|
2019-01-04 09:11:44 -08:00
|
|
|
.arg(Arg::with_name("no-minimize").long("no-minimize")),
|
2019-01-02 16:48:44 -08:00
|
|
|
)
|
2018-12-05 12:50:12 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("parse")
|
2019-09-11 14:44:49 -07:00
|
|
|
.about("Parse files")
|
2020-07-14 15:04:39 -07:00
|
|
|
.arg(Arg::with_name("paths-file").long("paths").takes_value(true))
|
2019-01-17 17:15:10 -08:00
|
|
|
.arg(
|
2020-07-14 15:04:39 -07:00
|
|
|
Arg::with_name("paths")
|
2019-01-17 17:15:10 -08:00
|
|
|
.index(1)
|
|
|
|
|
.multiple(true)
|
2020-07-14 15:04:39 -07:00
|
|
|
.required(false),
|
2019-01-17 17:15:10 -08:00
|
|
|
)
|
2019-03-14 12:39:04 -07:00
|
|
|
.arg(Arg::with_name("scope").long("scope").takes_value(true))
|
2019-01-08 21:03:51 -08:00
|
|
|
.arg(Arg::with_name("debug").long("debug").short("d"))
|
2019-01-17 17:15:10 -08:00
|
|
|
.arg(Arg::with_name("debug-graph").long("debug-graph").short("D"))
|
2021-01-04 22:07:38 +00:00
|
|
|
.arg(Arg::with_name("debug-xml").long("xml").short("x"))
|
2019-01-17 17:15:10 -08:00
|
|
|
.arg(Arg::with_name("quiet").long("quiet").short("q"))
|
2020-09-29 12:34:25 -04:00
|
|
|
.arg(Arg::with_name("stat").long("stat").short("s"))
|
2019-03-14 11:52:50 -07:00
|
|
|
.arg(Arg::with_name("time").long("time").short("t"))
|
2019-04-08 09:21:03 -07:00
|
|
|
.arg(Arg::with_name("timeout").long("timeout").takes_value(true))
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("edits")
|
|
|
|
|
.long("edit")
|
|
|
|
|
.short("edit")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.multiple(true)
|
|
|
|
|
.number_of_values(1),
|
|
|
|
|
),
|
2019-01-08 21:03:51 -08:00
|
|
|
)
|
2019-09-11 14:44:49 -07:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("query")
|
|
|
|
|
.about("Search files using a syntax tree query")
|
|
|
|
|
.arg(Arg::with_name("query-path").index(1).required(true))
|
2020-07-14 15:04:39 -07:00
|
|
|
.arg(Arg::with_name("paths-file").long("paths").takes_value(true))
|
2019-09-11 14:44:49 -07:00
|
|
|
.arg(
|
2020-07-14 15:04:39 -07:00
|
|
|
Arg::with_name("paths")
|
2019-09-11 14:44:49 -07:00
|
|
|
.index(2)
|
|
|
|
|
.multiple(true)
|
2020-07-14 15:04:39 -07:00
|
|
|
.required(false),
|
2019-09-11 14:44:49 -07:00
|
|
|
)
|
2020-07-12 20:45:17 +07:00
|
|
|
.arg(
|
2020-07-14 15:04:39 -07:00
|
|
|
Arg::with_name("byte-range")
|
2020-07-12 20:45:17 +07:00
|
|
|
.help("The range of byte offsets in which the query will be executed")
|
|
|
|
|
.long("byte-range")
|
2020-07-14 15:04:39 -07:00
|
|
|
.takes_value(true),
|
2020-07-12 20:45:17 +07:00
|
|
|
)
|
2019-09-18 14:22:55 -07:00
|
|
|
.arg(Arg::with_name("scope").long("scope").takes_value(true))
|
2020-10-21 12:37:24 -04:00
|
|
|
.arg(Arg::with_name("captures").long("captures").short("c"))
|
|
|
|
|
.arg(Arg::with_name("test").long("test")),
|
2019-09-11 14:44:49 -07:00
|
|
|
)
|
2020-03-04 14:27:31 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("tags")
|
2020-07-09 11:28:07 -07:00
|
|
|
.arg(Arg::with_name("quiet").long("quiet").short("q"))
|
2020-07-24 09:26:54 -07:00
|
|
|
.arg(Arg::with_name("time").long("time").short("t"))
|
2020-03-04 14:27:31 -08:00
|
|
|
.arg(Arg::with_name("scope").long("scope").takes_value(true))
|
2020-07-14 15:04:39 -07:00
|
|
|
.arg(Arg::with_name("paths-file").long("paths").takes_value(true))
|
2020-03-04 14:27:31 -08:00
|
|
|
.arg(
|
2020-07-14 15:04:39 -07:00
|
|
|
Arg::with_name("paths")
|
2020-03-04 14:27:31 -08:00
|
|
|
.help("The source file to use")
|
|
|
|
|
.index(1)
|
|
|
|
|
.multiple(true),
|
|
|
|
|
),
|
|
|
|
|
)
|
2019-01-08 21:03:51 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("test")
|
|
|
|
|
.about("Run a parser's tests")
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("filter")
|
|
|
|
|
.long("filter")
|
|
|
|
|
.short("f")
|
2021-03-13 19:53:10 +01:00
|
|
|
.takes_value(true)
|
2021-03-13 19:55:04 +01:00
|
|
|
.help("Only run corpus test cases whose name includes the given string"),
|
2019-01-08 21:03:51 -08:00
|
|
|
)
|
2021-05-16 17:55:58 +03:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("update")
|
|
|
|
|
.long("update")
|
|
|
|
|
.short("u")
|
|
|
|
|
.help("Update all syntax trees in corpus files with current parser output"),
|
|
|
|
|
)
|
2019-01-08 21:03:51 -08:00
|
|
|
.arg(Arg::with_name("debug").long("debug").short("d"))
|
2019-09-06 10:42:37 +08:00
|
|
|
.arg(Arg::with_name("debug-graph").long("debug-graph").short("D")),
|
2018-12-20 13:36:39 -08:00
|
|
|
)
|
2019-02-19 11:24:50 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("highlight")
|
|
|
|
|
.about("Highlight a file")
|
2020-07-14 15:04:39 -07:00
|
|
|
.arg(Arg::with_name("paths-file").long("paths").takes_value(true))
|
2019-02-19 11:24:50 -08:00
|
|
|
.arg(
|
2020-07-14 15:04:39 -07:00
|
|
|
Arg::with_name("paths")
|
2019-02-19 11:24:50 -08:00
|
|
|
.index(1)
|
|
|
|
|
.multiple(true)
|
2020-07-14 15:04:39 -07:00
|
|
|
.required(false),
|
2019-02-19 11:24:50 -08:00
|
|
|
)
|
2019-02-20 14:38:19 -08:00
|
|
|
.arg(Arg::with_name("scope").long("scope").takes_value(true))
|
2019-03-13 15:51:27 -07:00
|
|
|
.arg(Arg::with_name("html").long("html").short("h"))
|
2019-09-18 17:35:47 -07:00
|
|
|
.arg(Arg::with_name("time").long("time").short("t"))
|
2020-10-22 15:55:51 -07:00
|
|
|
.arg(Arg::with_name("quiet").long("quiet").short("q")),
|
2019-02-19 11:24:50 -08:00
|
|
|
)
|
2019-04-23 14:29:46 -07:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("build-wasm")
|
|
|
|
|
.about("Compile a parser to WASM")
|
2019-05-14 11:12:56 -07:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("docker")
|
|
|
|
|
.long("docker")
|
|
|
|
|
.help("Run emscripten via docker even if it is installed locally"),
|
|
|
|
|
)
|
2019-04-23 14:29:46 -07:00
|
|
|
.arg(Arg::with_name("path").index(1).multiple(true)),
|
|
|
|
|
)
|
2019-05-14 11:12:56 -07:00
|
|
|
.subcommand(
|
2020-07-09 11:28:07 -07:00
|
|
|
SubCommand::with_name("web-ui")
|
|
|
|
|
.about("Test a parser interactively in the browser")
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("quiet")
|
|
|
|
|
.long("quiet")
|
|
|
|
|
.short("q")
|
|
|
|
|
.help("open in default browser"),
|
|
|
|
|
),
|
2019-05-14 11:12:56 -07:00
|
|
|
)
|
2019-08-07 17:41:45 -07:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("dump-languages")
|
|
|
|
|
.about("Print info about all known language parsers"),
|
|
|
|
|
)
|
2018-12-20 13:36:39 -08:00
|
|
|
.get_matches();
|
2018-12-08 23:35:48 -08:00
|
|
|
|
2019-02-25 12:33:24 -08:00
|
|
|
let home_dir = dirs::home_dir().expect("Failed to read home directory");
|
2019-01-07 17:57:27 -08:00
|
|
|
let current_dir = env::current_dir().unwrap();
|
2019-02-25 12:33:24 -08:00
|
|
|
let config = config::Config::load(&home_dir);
|
|
|
|
|
let mut loader = loader::Loader::new(config.binary_directory.clone());
|
2019-01-17 15:15:40 -08:00
|
|
|
|
2019-02-25 12:33:24 -08:00
|
|
|
if matches.subcommand_matches("init-config").is_some() {
|
|
|
|
|
let config = config::Config::new(&home_dir);
|
|
|
|
|
config.save(&home_dir)?;
|
|
|
|
|
} else if let Some(matches) = matches.subcommand_matches("generate") {
|
2019-01-14 14:07:42 -08:00
|
|
|
let grammar_path = matches.value_of("grammar-path");
|
2019-05-20 13:25:01 -07:00
|
|
|
let report_symbol_name = matches.value_of("report-states-for-rule").or_else(|| {
|
|
|
|
|
if matches.is_present("report-states") {
|
|
|
|
|
Some("")
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-07-18 16:06:13 -07:00
|
|
|
if matches.is_present("log") {
|
|
|
|
|
logger::init();
|
|
|
|
|
}
|
2021-03-08 12:01:45 -08:00
|
|
|
let new_abi = !matches.is_present("prev-abi");
|
|
|
|
|
let generate_bindings = !matches.is_present("no-bindings");
|
2019-05-20 13:25:01 -07:00
|
|
|
generate::generate_parser_in_directory(
|
|
|
|
|
¤t_dir,
|
|
|
|
|
grammar_path,
|
2021-03-08 12:01:45 -08:00
|
|
|
new_abi,
|
|
|
|
|
generate_bindings,
|
2019-05-20 13:25:01 -07:00
|
|
|
report_symbol_name,
|
|
|
|
|
)?;
|
2019-01-08 21:03:51 -08:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("test") {
|
|
|
|
|
let debug = matches.is_present("debug");
|
|
|
|
|
let debug_graph = matches.is_present("debug-graph");
|
2019-09-06 10:57:59 +08:00
|
|
|
let update = matches.is_present("update");
|
2019-01-08 21:03:51 -08:00
|
|
|
let filter = matches.value_of("filter");
|
2019-12-05 15:28:16 -08:00
|
|
|
let languages = loader.languages_at_path(¤t_dir)?;
|
|
|
|
|
let language = languages
|
|
|
|
|
.first()
|
2021-06-09 12:32:22 -04:00
|
|
|
.ok_or_else(|| anyhow!("No language found"))?;
|
2019-12-05 15:28:16 -08:00
|
|
|
let test_dir = current_dir.join("test");
|
|
|
|
|
|
|
|
|
|
// Run the corpus tests. Look for them at two paths: `test/corpus` and `corpus`.
|
|
|
|
|
let mut test_corpus_dir = test_dir.join("corpus");
|
|
|
|
|
if !test_corpus_dir.is_dir() {
|
|
|
|
|
test_corpus_dir = current_dir.join("corpus");
|
|
|
|
|
}
|
|
|
|
|
if test_corpus_dir.is_dir() {
|
2021-03-08 12:01:45 -08:00
|
|
|
test::run_tests_at_path(
|
|
|
|
|
*language,
|
|
|
|
|
&test_corpus_dir,
|
|
|
|
|
debug,
|
|
|
|
|
debug_graph,
|
|
|
|
|
filter,
|
|
|
|
|
update,
|
|
|
|
|
)?;
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
2019-10-24 12:01:27 -07:00
|
|
|
|
2019-12-05 15:28:16 -08:00
|
|
|
// Check that all of the queries are valid.
|
|
|
|
|
test::check_queries_at_path(*language, ¤t_dir.join("queries"))?;
|
|
|
|
|
|
|
|
|
|
// Run the syntax highlighting tests.
|
|
|
|
|
let test_highlight_dir = test_dir.join("highlight");
|
|
|
|
|
if test_highlight_dir.is_dir() {
|
|
|
|
|
test_highlight::test_highlights(&loader, &test_highlight_dir)?;
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
2019-01-07 22:01:40 -08:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("parse") {
|
2019-01-08 21:03:51 -08:00
|
|
|
let debug = matches.is_present("debug");
|
|
|
|
|
let debug_graph = matches.is_present("debug-graph");
|
2021-01-04 22:07:38 +00:00
|
|
|
let debug_xml = matches.is_present("debug-xml");
|
2019-01-17 17:15:10 -08:00
|
|
|
let quiet = matches.is_present("quiet");
|
|
|
|
|
let time = matches.is_present("time");
|
2019-04-08 09:21:03 -07:00
|
|
|
let edits = matches
|
|
|
|
|
.values_of("edits")
|
|
|
|
|
.map_or(Vec::new(), |e| e.collect());
|
2020-10-22 15:55:51 -07:00
|
|
|
let cancellation_flag = util::cancel_on_stdin();
|
|
|
|
|
|
2019-03-14 11:52:50 -07:00
|
|
|
let timeout = matches
|
|
|
|
|
.value_of("timeout")
|
2019-03-14 12:39:04 -07:00
|
|
|
.map_or(0, |t| u64::from_str_radix(t, 10).unwrap());
|
2020-07-14 15:04:39 -07:00
|
|
|
|
|
|
|
|
let paths = collect_paths(matches.value_of("paths-file"), matches.values_of("paths"))?;
|
|
|
|
|
|
2019-01-17 17:15:10 -08:00
|
|
|
let max_path_length = paths.iter().map(|p| p.chars().count()).max().unwrap();
|
2019-02-06 16:17:35 -08:00
|
|
|
let mut has_error = false;
|
2019-09-11 14:44:49 -07:00
|
|
|
loader.find_all_languages(&config.parser_directories)?;
|
2020-09-29 12:34:25 -04:00
|
|
|
|
2020-09-29 15:43:30 -04:00
|
|
|
let should_track_stats = matches.is_present("stat");
|
2020-09-30 09:28:58 -04:00
|
|
|
let mut stats = parse::Stats::default();
|
2020-09-29 12:34:25 -04:00
|
|
|
|
2019-01-17 17:15:10 -08:00
|
|
|
for path in paths {
|
2019-12-06 11:41:21 -08:00
|
|
|
let path = Path::new(&path);
|
2021-06-09 12:51:28 -04:00
|
|
|
let language = loader.select_language(path, ¤t_dir, matches.value_of("scope"))?;
|
2020-09-29 15:43:30 -04:00
|
|
|
|
|
|
|
|
let this_file_errored = parse::parse_file_at_path(
|
2019-01-17 17:15:10 -08:00
|
|
|
language,
|
|
|
|
|
path,
|
2019-04-08 09:21:03 -07:00
|
|
|
&edits,
|
2019-01-17 17:15:10 -08:00
|
|
|
max_path_length,
|
|
|
|
|
quiet,
|
|
|
|
|
time,
|
2019-03-14 11:52:50 -07:00
|
|
|
timeout,
|
2019-01-17 17:15:10 -08:00
|
|
|
debug,
|
|
|
|
|
debug_graph,
|
2021-01-04 22:07:38 +00:00
|
|
|
debug_xml,
|
2020-10-22 15:55:51 -07:00
|
|
|
Some(&cancellation_flag),
|
2019-01-17 17:15:10 -08:00
|
|
|
)?;
|
2020-09-29 15:43:30 -04:00
|
|
|
|
|
|
|
|
if should_track_stats {
|
|
|
|
|
stats.total_parses += 1;
|
|
|
|
|
if !this_file_errored {
|
|
|
|
|
stats.successful_parses += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
has_error |= this_file_errored;
|
2019-01-07 17:57:36 -08:00
|
|
|
}
|
2020-09-29 12:34:25 -04:00
|
|
|
|
2020-09-29 15:43:30 -04:00
|
|
|
if should_track_stats {
|
2020-09-29 12:34:25 -04:00
|
|
|
println!("{}", stats)
|
2019-01-07 17:57:36 -08:00
|
|
|
}
|
2019-02-06 16:17:35 -08:00
|
|
|
|
|
|
|
|
if has_error {
|
2021-06-09 12:32:22 -04:00
|
|
|
return Err(anyhow!(""));
|
2019-02-06 16:17:35 -08:00
|
|
|
}
|
2019-09-11 14:44:49 -07:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("query") {
|
2019-09-18 14:22:55 -07:00
|
|
|
let ordered_captures = matches.values_of("captures").is_some();
|
2020-07-14 15:04:39 -07:00
|
|
|
let paths = collect_paths(matches.value_of("paths-file"), matches.values_of("paths"))?;
|
2019-09-11 14:44:49 -07:00
|
|
|
loader.find_all_languages(&config.parser_directories)?;
|
2021-06-09 12:51:28 -04:00
|
|
|
let language = loader.select_language(
|
2020-07-14 15:04:39 -07:00
|
|
|
Path::new(&paths[0]),
|
2019-09-11 14:44:49 -07:00
|
|
|
¤t_dir,
|
|
|
|
|
matches.value_of("scope"),
|
|
|
|
|
)?;
|
|
|
|
|
let query_path = Path::new(matches.value_of("query-path").unwrap());
|
2020-07-14 15:04:39 -07:00
|
|
|
let range = matches.value_of("byte-range").map(|br| {
|
2020-07-12 20:45:17 +07:00
|
|
|
let r: Vec<&str> = br.split(":").collect();
|
2021-05-28 14:15:05 -07:00
|
|
|
r[0].parse().unwrap()..r[1].parse().unwrap()
|
2020-07-12 20:45:17 +07:00
|
|
|
});
|
2020-10-21 12:37:24 -04:00
|
|
|
let should_test = matches.is_present("test");
|
|
|
|
|
query::query_files_at_paths(
|
|
|
|
|
language,
|
|
|
|
|
paths,
|
|
|
|
|
query_path,
|
|
|
|
|
ordered_captures,
|
|
|
|
|
range,
|
|
|
|
|
should_test,
|
|
|
|
|
)?;
|
2020-03-04 14:27:31 -08:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("tags") {
|
|
|
|
|
loader.find_all_languages(&config.parser_directories)?;
|
2020-07-14 15:04:39 -07:00
|
|
|
let paths = collect_paths(matches.value_of("paths-file"), matches.values_of("paths"))?;
|
2020-07-09 11:28:07 -07:00
|
|
|
tags::generate_tags(
|
|
|
|
|
&loader,
|
|
|
|
|
matches.value_of("scope"),
|
|
|
|
|
&paths,
|
|
|
|
|
matches.is_present("quiet"),
|
|
|
|
|
matches.is_present("time"),
|
|
|
|
|
)?;
|
2019-02-19 11:24:50 -08:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("highlight") {
|
2019-10-24 12:01:27 -07:00
|
|
|
loader.configure_highlights(&config.theme.highlight_names);
|
2019-02-25 12:33:24 -08:00
|
|
|
loader.find_all_languages(&config.parser_directories)?;
|
2019-02-19 11:24:50 -08:00
|
|
|
|
2019-10-24 12:01:27 -07:00
|
|
|
let time = matches.is_present("time");
|
2020-10-22 15:55:51 -07:00
|
|
|
let quiet = matches.is_present("quiet");
|
|
|
|
|
let html_mode = quiet || matches.is_present("html");
|
2020-07-14 15:04:39 -07:00
|
|
|
let paths = collect_paths(matches.value_of("paths-file"), matches.values_of("paths"))?;
|
2020-10-22 15:55:51 -07:00
|
|
|
|
|
|
|
|
if html_mode && !quiet {
|
2019-02-19 11:24:50 -08:00
|
|
|
println!("{}", highlight::HTML_HEADER);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 15:55:51 -07:00
|
|
|
let cancellation_flag = util::cancel_on_stdin();
|
|
|
|
|
|
2020-03-04 14:27:31 -08:00
|
|
|
let mut lang = None;
|
2019-02-20 14:38:19 -08:00
|
|
|
if let Some(scope) = matches.value_of("scope") {
|
2020-03-04 14:27:31 -08:00
|
|
|
lang = loader.language_configuration_for_scope(scope)?;
|
|
|
|
|
if lang.is_none() {
|
2021-06-09 12:32:22 -04:00
|
|
|
return Err(anyhow!("Unknown scope '{}'", scope));
|
2019-02-20 14:38:19 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 11:24:50 -08:00
|
|
|
for path in paths {
|
2019-12-17 15:49:05 -08:00
|
|
|
let path = Path::new(&path);
|
2020-03-04 14:27:31 -08:00
|
|
|
let (language, language_config) = match lang {
|
2019-02-20 14:38:19 -08:00
|
|
|
Some(v) => v,
|
|
|
|
|
None => match loader.language_configuration_for_file_name(path)? {
|
|
|
|
|
Some(v) => v,
|
|
|
|
|
None => {
|
|
|
|
|
eprintln!("No language found for path {:?}", path);
|
|
|
|
|
continue;
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
2019-02-20 14:38:19 -08:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-24 12:01:27 -07:00
|
|
|
if let Some(highlight_config) = language_config.highlight_config(language)? {
|
2019-02-20 14:38:19 -08:00
|
|
|
let source = fs::read(path)?;
|
|
|
|
|
if html_mode {
|
2020-10-22 15:55:51 -07:00
|
|
|
highlight::html(
|
|
|
|
|
&loader,
|
|
|
|
|
&config.theme,
|
|
|
|
|
&source,
|
|
|
|
|
highlight_config,
|
|
|
|
|
quiet,
|
|
|
|
|
time,
|
|
|
|
|
)?;
|
2019-02-20 14:38:19 -08:00
|
|
|
} else {
|
2020-10-22 15:55:51 -07:00
|
|
|
highlight::ansi(
|
|
|
|
|
&loader,
|
|
|
|
|
&config.theme,
|
|
|
|
|
&source,
|
|
|
|
|
highlight_config,
|
|
|
|
|
time,
|
|
|
|
|
Some(&cancellation_flag),
|
|
|
|
|
)?;
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
2019-02-20 14:38:19 -08:00
|
|
|
} else {
|
2020-03-04 14:27:31 -08:00
|
|
|
eprintln!("No syntax highlighting config found for path {:?}", path);
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-02 23:04:15 +00:00
|
|
|
|
2020-10-22 15:55:51 -07:00
|
|
|
if html_mode && !quiet {
|
2020-03-02 23:04:15 +00:00
|
|
|
println!("{}", highlight::HTML_FOOTER);
|
|
|
|
|
}
|
2019-04-23 14:29:46 -07:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("build-wasm") {
|
|
|
|
|
let grammar_path = current_dir.join(matches.value_of("path").unwrap_or(""));
|
2019-05-14 11:12:56 -07:00
|
|
|
wasm::compile_language_to_wasm(&grammar_path, matches.is_present("docker"))?;
|
2020-05-16 14:56:40 +08:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("web-ui") {
|
|
|
|
|
let open_in_browser = !matches.is_present("quiet");
|
|
|
|
|
web_ui::serve(¤t_dir, open_in_browser);
|
2019-08-07 17:41:45 -07:00
|
|
|
} else if matches.subcommand_matches("dump-languages").is_some() {
|
|
|
|
|
loader.find_all_languages(&config.parser_directories)?;
|
|
|
|
|
for (configuration, language_path) in loader.get_all_language_configurations() {
|
|
|
|
|
println!(
|
2019-10-17 10:14:05 -07:00
|
|
|
concat!(
|
|
|
|
|
"scope: {}\n",
|
|
|
|
|
"parser: {:?}\n",
|
|
|
|
|
"highlights: {:?}\n",
|
|
|
|
|
"file_types: {:?}\n",
|
|
|
|
|
"content_regex: {:?}\n",
|
|
|
|
|
"injection_regex: {:?}\n",
|
|
|
|
|
),
|
2019-08-07 17:41:45 -07:00
|
|
|
configuration.scope.as_ref().unwrap_or(&String::new()),
|
|
|
|
|
language_path,
|
2019-10-17 10:14:05 -07:00
|
|
|
configuration.highlights_filenames,
|
2019-08-07 17:41:45 -07:00
|
|
|
configuration.file_types,
|
|
|
|
|
configuration.content_regex,
|
|
|
|
|
configuration.injection_regex,
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-12-08 23:35:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2018-12-05 12:50:12 -08:00
|
|
|
}
|
2019-09-11 14:44:49 -07:00
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
fn collect_paths<'a>(
|
|
|
|
|
paths_file: Option<&str>,
|
|
|
|
|
paths: Option<impl Iterator<Item = &'a str>>,
|
2021-06-09 12:32:22 -04:00
|
|
|
) -> Result<Vec<String>> {
|
2020-07-14 15:04:39 -07:00
|
|
|
if let Some(paths_file) = paths_file {
|
|
|
|
|
return Ok(fs::read_to_string(paths_file)
|
2021-06-09 12:32:22 -04:00
|
|
|
.with_context(|| format!("Failed to read paths file {}", paths_file))?
|
2020-07-14 15:04:39 -07:00
|
|
|
.trim()
|
|
|
|
|
.split_ascii_whitespace()
|
|
|
|
|
.map(String::from)
|
|
|
|
|
.collect::<Vec<_>>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(paths) = paths {
|
|
|
|
|
let mut result = Vec::new();
|
2019-12-06 11:41:21 -08:00
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
let mut incorporate_path = |path: &str, positive| {
|
|
|
|
|
if positive {
|
|
|
|
|
result.push(path.to_string());
|
|
|
|
|
} else {
|
|
|
|
|
if let Some(index) = result.iter().position(|p| p == path) {
|
|
|
|
|
result.remove(index);
|
|
|
|
|
}
|
2019-12-06 11:41:21 -08:00
|
|
|
}
|
2020-07-14 15:04:39 -07:00
|
|
|
};
|
2019-12-06 11:41:21 -08:00
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
for mut path in paths {
|
|
|
|
|
let mut positive = true;
|
|
|
|
|
if path.starts_with("!") {
|
|
|
|
|
positive = false;
|
|
|
|
|
path = path.trim_start_matches("!");
|
|
|
|
|
}
|
2019-12-06 11:41:21 -08:00
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
if Path::new(path).exists() {
|
|
|
|
|
incorporate_path(path, positive);
|
|
|
|
|
} else {
|
2021-06-09 12:32:22 -04:00
|
|
|
let paths =
|
|
|
|
|
glob(path).with_context(|| format!("Invalid glob pattern {:?}", path))?;
|
2020-07-14 15:04:39 -07:00
|
|
|
for path in paths {
|
|
|
|
|
if let Some(path) = path?.to_str() {
|
|
|
|
|
incorporate_path(path, positive);
|
|
|
|
|
}
|
2019-12-06 11:41:21 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-14 15:04:39 -07:00
|
|
|
|
2020-09-30 15:49:42 -04:00
|
|
|
if result.is_empty() {
|
2021-06-09 12:32:22 -04:00
|
|
|
return Err(anyhow!(
|
|
|
|
|
"No files were found at or matched by the provided pathname/glob"
|
|
|
|
|
));
|
2020-09-30 15:49:42 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
return Ok(result);
|
2019-12-06 11:41:21 -08:00
|
|
|
}
|
2020-07-14 15:04:39 -07:00
|
|
|
|
2021-06-09 12:32:22 -04:00
|
|
|
Err(anyhow!("Must provide one or more paths"))
|
2019-12-06 11:41:21 -08:00
|
|
|
}
|