diff --git a/crates/cli/src/init.rs b/crates/cli/src/init.rs index deb64292..0164cc8f 100644 --- a/crates/cli/src/init.rs +++ b/crates/cli/src/init.rs @@ -268,15 +268,6 @@ pub fn generate_grammar_files( .clone() .unwrap_or_else(|| format!("TreeSitter{}", language_name.to_upper_camel_case())); - fn pathsjson_to_variable<'a>(paths_json: &'a PathsJSON, default: &'a PathBuf) -> &'a str { - match paths_json { - PathsJSON::Empty => Some(default), - PathsJSON::Single(path_buf) => Some(path_buf), - PathsJSON::Multiple(paths) => paths.first(), - } - .map_or("", |path| path.as_os_str().to_str().unwrap_or("")) - } - let default_highlights_path = Path::new("queries").join(DEFAULT_HIGHLIGHTS_QUERY_FILE_NAME); let default_injections_path = Path::new("queries").join(DEFAULT_INJECTIONS_QUERY_FILE_NAME); let default_locals_path = Path::new("queries").join(DEFAULT_LOCALS_QUERY_FILE_NAME); @@ -308,22 +299,18 @@ pub fn generate_grammar_files( camel_parser_name: &camel_name, title_parser_name: &title_name, class_name: &class_name, - highlights_query_path: pathsjson_to_variable( - &tree_sitter_config.grammars[0].highlights, - &default_highlights_path, - ), - injections_query_path: pathsjson_to_variable( - &tree_sitter_config.grammars[0].injections, - &default_injections_path, - ), - locals_query_path: pathsjson_to_variable( - &tree_sitter_config.grammars[0].locals, - &default_locals_path, - ), - tags_query_path: pathsjson_to_variable( - &tree_sitter_config.grammars[0].tags, - &default_tags_path, - ), + highlights_query_path: tree_sitter_config.grammars[0] + .highlights + .to_variable_value(&default_highlights_path), + injections_query_path: tree_sitter_config.grammars[0] + .injections + .to_variable_value(&default_injections_path), + locals_query_path: tree_sitter_config.grammars[0] + .locals + .to_variable_value(&default_locals_path), + tags_query_path: tree_sitter_config.grammars[0] + .tags + .to_variable_value(&default_tags_path), }; // Create package.json @@ -503,11 +490,11 @@ pub fn generate_grammar_files( } "#}; - let indented_replacement = replacement - .lines() - .map(|line| if line.is_empty() { line.to_string() } else { format!(" {line}") }) - .collect::>() - .join("\n"); + let indented_replacement = replacement + .lines() + .map(|line| if line.is_empty() { line.to_string() } else { format!(" {line}") }) + .collect::>() + .join("\n"); contents = contents.replace(r#" c_config.flag("-utf-8");"#, &indented_replacement); } diff --git a/crates/loader/src/loader.rs b/crates/loader/src/loader.rs index 7c2e5226..28618be6 100644 --- a/crates/loader/src/loader.rs +++ b/crates/loader/src/loader.rs @@ -284,6 +284,17 @@ impl PathsJSON { const fn is_empty(&self) -> bool { matches!(self, Self::Empty) } + + /// Represent this set of paths as a string that can be included in templates + #[must_use] + pub fn to_variable_value<'a>(&'a self, default: &'a PathBuf) -> &'a str { + match self { + Self::Empty => Some(default), + Self::Single(path_buf) => Some(path_buf), + Self::Multiple(paths) => paths.first(), + } + .map_or("", |path| path.as_os_str().to_str().unwrap_or("")) + } } #[derive(Serialize, Deserialize, Clone)]