diff --git a/cli/src/loader.rs b/cli/src/loader.rs index 49bab4b4..23a55cc6 100644 --- a/cli/src/loader.rs +++ b/cli/src/loader.rs @@ -27,7 +27,7 @@ struct LanguageRepo { } pub struct LanguageConfiguration { - pub name: String, + scope: Option, _content_regex: Option, _first_line_regex: Option, injection_regex: Option, @@ -79,6 +79,21 @@ impl Loader { } } + pub fn language_configuration_for_scope( + &self, + scope: &str, + ) -> Result> { + for (i, repo) in self.language_repos.iter().enumerate() { + for configuration in &repo.configurations { + if configuration.scope.as_ref().map_or(false, |s| s == scope) { + let (language, _) = self.language_configuration_for_id(i)?; + return Ok(Some((language, &configuration))); + } + } + } + Ok(None) + } + pub fn language_configuration_for_file_name( &self, path: &Path, @@ -258,7 +273,7 @@ impl Loader { fn find_language_at_path<'a>(&'a mut self, parser_path: &Path) -> Result { #[derive(Deserialize)] struct LanguageConfigurationJSON { - name: String, + scope: Option, #[serde(rename = "file-types")] file_types: Option>, #[serde(rename = "content-regex")] @@ -284,7 +299,7 @@ impl Loader { configurations .into_iter() .map(|conf| LanguageConfiguration { - name: conf.name, + scope: conf.scope, file_types: conf.file_types.unwrap_or(Vec::new()), _content_regex: conf .content_regex diff --git a/cli/src/main.rs b/cli/src/main.rs index 9cd4e131..255f680b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -72,6 +72,7 @@ fn run() -> error::Result<()> { .multiple(true) .required(true), ) + .arg(Arg::with_name("scope").long("scope").takes_value(true)) .arg(Arg::with_name("html").long("html").short("h")), ) .get_matches(); @@ -169,17 +170,40 @@ fn run() -> error::Result<()> { println!("{}", highlight::HTML_HEADER); } + let language_config; + if let Some(scope) = matches.value_of("scope") { + language_config = loader.language_configuration_for_scope(scope)?; + if language_config.is_none() { + return Err(error::Error(format!("Unknown scope '{}'", scope))); + } + } else { + language_config = None; + } + for path in paths { let path = Path::new(path); - if let Some((language, config)) = loader.language_configuration_for_file_name(path)? { - if let Some(sheet) = config.highlight_property_sheet(language)? { - let source = fs::read(path)?; - if html_mode { - highlight::html(&loader, &theme, &source, language, sheet)?; - } else { - highlight::ansi(&loader, &theme, &source, language, sheet)?; + let (language, config) = match language_config { + Some(v) => v, + None => match loader.language_configuration_for_file_name(path)? { + Some(v) => v, + None => { + eprintln!("No language found for path {:?}", path); + continue; } + }, + }; + + if let Some(sheet) = config.highlight_property_sheet(language)? { + let source = fs::read(path)?; + if html_mode { + highlight::html(&loader, &theme, &source, language, sheet)?; + } else { + highlight::ansi(&loader, &theme, &source, language, sheet)?; } + } else { + return Err(error::Error(format!( + "No syntax highlighting property sheet specified" + ))); } } }