cli: Add --scope flag to highlight command
This commit is contained in:
parent
d63368552a
commit
d2264d597f
2 changed files with 49 additions and 10 deletions
|
|
@ -27,7 +27,7 @@ struct LanguageRepo {
|
|||
}
|
||||
|
||||
pub struct LanguageConfiguration {
|
||||
pub name: String,
|
||||
scope: Option<String>,
|
||||
_content_regex: Option<Regex>,
|
||||
_first_line_regex: Option<Regex>,
|
||||
injection_regex: Option<Regex>,
|
||||
|
|
@ -79,6 +79,21 @@ impl Loader {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn language_configuration_for_scope(
|
||||
&self,
|
||||
scope: &str,
|
||||
) -> Result<Option<(Language, &LanguageConfiguration)>> {
|
||||
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<usize> {
|
||||
#[derive(Deserialize)]
|
||||
struct LanguageConfigurationJSON {
|
||||
name: String,
|
||||
scope: Option<String>,
|
||||
#[serde(rename = "file-types")]
|
||||
file_types: Option<Vec<String>>,
|
||||
#[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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue