2019-02-01 14:39:37 -08:00
|
|
|
use lazy_static::lazy_static;
|
2019-01-17 17:16:04 -08:00
|
|
|
use std::fs;
|
2019-01-15 10:27:39 -08:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-09-18 17:35:47 -07:00
|
|
|
use tree_sitter::Language;
|
2019-10-24 12:01:27 -07:00
|
|
|
use tree_sitter_highlight::HighlightConfiguration;
|
2021-06-09 12:51:28 -04:00
|
|
|
use tree_sitter_loader::Loader;
|
2021-12-18 10:48:59 -06:00
|
|
|
use tree_sitter_tags::TagsConfiguration;
|
2019-01-15 10:27:39 -08:00
|
|
|
|
2019-02-01 14:39:37 -08:00
|
|
|
include!("./dirs.rs");
|
|
|
|
|
|
2019-01-15 10:27:39 -08:00
|
|
|
lazy_static! {
|
2021-06-09 15:03:27 -04:00
|
|
|
static ref TEST_LOADER: Loader = Loader::with_parser_lib_path(SCRATCH_DIR.clone());
|
2019-01-15 10:27:39 -08:00
|
|
|
}
|
|
|
|
|
|
2019-10-24 12:01:27 -07:00
|
|
|
pub fn test_loader<'a>() -> &'a Loader {
|
|
|
|
|
&*TEST_LOADER
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 10:27:39 -08:00
|
|
|
pub fn fixtures_dir<'a>() -> &'static Path {
|
|
|
|
|
&FIXTURES_DIR
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_language(name: &str) -> Language {
|
|
|
|
|
TEST_LOADER
|
2019-02-13 19:30:59 -08:00
|
|
|
.load_language_at_path(&GRAMMARS_DIR.join(name).join("src"), &HEADER_DIR)
|
2019-01-15 10:27:39 -08:00
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-14 12:31:07 -07:00
|
|
|
pub fn get_language_queries_path(language_name: &str) -> PathBuf {
|
|
|
|
|
GRAMMARS_DIR.join(language_name).join("queries")
|
2019-03-08 13:13:02 -08:00
|
|
|
}
|
|
|
|
|
|
2019-09-18 17:35:47 -07:00
|
|
|
pub fn get_highlight_config(
|
2019-10-14 12:31:07 -07:00
|
|
|
language_name: &str,
|
2020-01-27 12:32:37 -08:00
|
|
|
injection_query_filename: Option<&str>,
|
2019-10-24 12:01:27 -07:00
|
|
|
highlight_names: &[String],
|
2019-09-18 17:35:47 -07:00
|
|
|
) -> HighlightConfiguration {
|
2019-02-19 11:24:50 -08:00
|
|
|
let language = get_language(language_name);
|
2019-10-14 12:31:07 -07:00
|
|
|
let queries_path = get_language_queries_path(language_name);
|
|
|
|
|
let highlights_query = fs::read_to_string(queries_path.join("highlights.scm")).unwrap();
|
2020-01-27 12:32:37 -08:00
|
|
|
let injections_query = if let Some(injection_query_filename) = injection_query_filename {
|
|
|
|
|
fs::read_to_string(queries_path.join(injection_query_filename)).unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
String::new()
|
|
|
|
|
};
|
2019-10-14 12:31:07 -07:00
|
|
|
let locals_query = fs::read_to_string(queries_path.join("locals.scm")).unwrap_or(String::new());
|
2019-10-24 12:01:27 -07:00
|
|
|
let mut result = HighlightConfiguration::new(
|
|
|
|
|
language,
|
|
|
|
|
&highlights_query,
|
|
|
|
|
&injections_query,
|
|
|
|
|
&locals_query,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2021-05-17 21:44:41 +02:00
|
|
|
result.configure(&highlight_names);
|
2019-10-24 12:01:27 -07:00
|
|
|
result
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-18 10:48:59 -06:00
|
|
|
pub fn get_tags_config(language_name: &str) -> TagsConfiguration {
|
|
|
|
|
let language = get_language(language_name);
|
|
|
|
|
let queries_path = get_language_queries_path(language_name);
|
|
|
|
|
let tags_query = fs::read_to_string(queries_path.join("tags.scm")).unwrap();
|
|
|
|
|
let locals_query = fs::read_to_string(queries_path.join("locals.scm")).unwrap_or(String::new());
|
|
|
|
|
TagsConfiguration::new(language, &tags_query, &locals_query).unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 14:44:06 -08:00
|
|
|
pub fn get_test_language(name: &str, parser_code: &str, path: Option<&Path>) -> Language {
|
2019-01-15 10:27:39 -08:00
|
|
|
let parser_c_path = SCRATCH_DIR.join(&format!("{}-parser.c", name));
|
|
|
|
|
if !fs::read_to_string(&parser_c_path)
|
|
|
|
|
.map(|content| content == parser_code)
|
|
|
|
|
.unwrap_or(false)
|
|
|
|
|
{
|
|
|
|
|
fs::write(&parser_c_path, parser_code).unwrap();
|
|
|
|
|
}
|
2019-02-04 14:44:06 -08:00
|
|
|
let scanner_path = path.and_then(|p| {
|
|
|
|
|
let result = p.join("scanner.c");
|
|
|
|
|
if result.exists() {
|
|
|
|
|
Some(result)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-01-15 10:27:39 -08:00
|
|
|
TEST_LOADER
|
2019-01-17 17:16:04 -08:00
|
|
|
.load_language_from_sources(name, &HEADER_DIR, &parser_c_path, &scanner_path)
|
2019-01-15 10:27:39 -08:00
|
|
|
.unwrap()
|
|
|
|
|
}
|
2021-03-11 14:46:13 -08:00
|
|
|
|
|
|
|
|
pub fn get_test_grammar(name: &str) -> (String, Option<PathBuf>) {
|
|
|
|
|
let dir = fixtures_dir().join("test_grammars").join(name);
|
|
|
|
|
let grammar = fs::read_to_string(&dir.join("grammar.json")).unwrap();
|
|
|
|
|
(grammar, Some(dir))
|
|
|
|
|
}
|