2018-08-20 21:12:52 +02:00
|
|
|
use std::collections::BTreeMap;
|
2020-04-21 08:19:24 +02:00
|
|
|
use std::ffi::OsStr;
|
2018-05-10 12:36:09 +02:00
|
|
|
use std::fs::{self, File};
|
2018-10-09 21:18:40 +02:00
|
|
|
use std::io::BufReader;
|
2020-03-21 16:48:27 +01:00
|
|
|
use std::path::Path;
|
2018-10-09 21:18:40 +02:00
|
|
|
|
2018-05-10 12:36:09 +02:00
|
|
|
use syntect::dumps::{dump_to_file, from_binary, from_reader};
|
|
|
|
use syntect::highlighting::{Theme, ThemeSet};
|
2018-10-09 21:18:40 +02:00
|
|
|
use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
|
|
|
|
|
2020-04-21 15:50:46 +02:00
|
|
|
use crate::assets_metadata::AssetsMetadata;
|
2019-03-08 11:48:22 +01:00
|
|
|
use crate::errors::*;
|
|
|
|
use crate::inputfile::{InputFile, InputFileReader};
|
2020-03-22 09:55:13 +01:00
|
|
|
use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
|
2018-08-28 20:12:45 +02:00
|
|
|
|
2019-10-15 03:25:53 +02:00
|
|
|
#[derive(Debug)]
|
2018-05-10 12:36:09 +02:00
|
|
|
pub struct HighlightingAssets {
|
2020-03-21 20:01:36 +01:00
|
|
|
pub(crate) syntax_set: SyntaxSet,
|
|
|
|
pub(crate) theme_set: ThemeSet,
|
2020-03-21 20:31:32 +01:00
|
|
|
fallback_theme: Option<&'static str>,
|
2018-05-10 12:36:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HighlightingAssets {
|
2020-03-21 20:31:32 +01:00
|
|
|
pub fn default_theme() -> &'static str {
|
|
|
|
"Monokai Extended"
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:36:00 +01:00
|
|
|
pub fn from_files(source_dir: &Path, include_integrated_assets: bool) -> Result<Self> {
|
|
|
|
let mut theme_set = if include_integrated_assets {
|
|
|
|
Self::get_integrated_themeset()
|
|
|
|
} else {
|
2018-10-09 21:18:40 +02:00
|
|
|
ThemeSet {
|
|
|
|
themes: BTreeMap::new(),
|
|
|
|
}
|
2018-08-20 21:12:52 +02:00
|
|
|
};
|
2018-08-19 10:49:09 +02:00
|
|
|
|
2018-05-16 21:22:16 +02:00
|
|
|
let theme_dir = source_dir.join("themes");
|
2018-08-19 10:49:09 +02:00
|
|
|
|
2018-10-09 21:18:40 +02:00
|
|
|
let res = theme_set.add_from_folder(&theme_dir);
|
2019-03-08 11:46:49 +01:00
|
|
|
if res.is_err() {
|
2018-08-19 10:49:09 +02:00
|
|
|
println!(
|
|
|
|
"No themes were found in '{}', using the default set",
|
2018-05-23 11:31:55 +02:00
|
|
|
theme_dir.to_string_lossy()
|
2018-08-19 10:49:09 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:36:00 +01:00
|
|
|
let mut syntax_set_builder = if !include_integrated_assets {
|
2018-10-09 21:18:40 +02:00
|
|
|
let mut builder = SyntaxSetBuilder::new();
|
|
|
|
builder.add_plain_text_syntax();
|
|
|
|
builder
|
|
|
|
} else {
|
|
|
|
Self::get_integrated_syntaxset().into_builder()
|
|
|
|
};
|
|
|
|
|
2018-05-16 21:22:16 +02:00
|
|
|
let syntax_dir = source_dir.join("syntaxes");
|
2018-08-19 10:49:09 +02:00
|
|
|
if syntax_dir.exists() {
|
2018-10-09 21:18:40 +02:00
|
|
|
syntax_set_builder.add_from_folder(syntax_dir, true)?;
|
2018-08-19 10:49:09 +02:00
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"No syntaxes were found in '{}', using the default set.",
|
2018-05-16 21:22:16 +02:00
|
|
|
syntax_dir.to_string_lossy()
|
2018-08-19 10:49:09 +02:00
|
|
|
);
|
2018-05-16 21:22:16 +02:00
|
|
|
}
|
2018-05-10 12:36:09 +02:00
|
|
|
|
2018-10-09 21:18:40 +02:00
|
|
|
Ok(HighlightingAssets {
|
|
|
|
syntax_set: syntax_set_builder.build(),
|
|
|
|
theme_set,
|
2020-03-21 20:31:32 +01:00
|
|
|
fallback_theme: None,
|
2018-10-09 21:18:40 +02:00
|
|
|
})
|
2018-05-10 12:36:09 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 15:50:46 +02:00
|
|
|
pub fn from_cache(cache_path: &Path) -> Result<Self> {
|
|
|
|
let syntax_set_path = cache_path.join("syntaxes.bin");
|
|
|
|
let theme_set_path = cache_path.join("themes.bin");
|
|
|
|
|
|
|
|
let syntax_set_file = File::open(&syntax_set_path).chain_err(|| {
|
2018-05-10 12:36:09 +02:00
|
|
|
format!(
|
|
|
|
"Could not load cached syntax set '{}'",
|
2020-03-21 16:48:27 +01:00
|
|
|
syntax_set_path.to_string_lossy()
|
2018-05-10 12:36:09 +02:00
|
|
|
)
|
|
|
|
})?;
|
2018-10-09 21:18:40 +02:00
|
|
|
let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file))
|
|
|
|
.chain_err(|| "Could not parse cached syntax set")?;
|
2018-05-10 12:36:09 +02:00
|
|
|
|
|
|
|
let theme_set_file = File::open(&theme_set_path).chain_err(|| {
|
|
|
|
format!(
|
|
|
|
"Could not load cached theme set '{}'",
|
|
|
|
theme_set_path.to_string_lossy()
|
|
|
|
)
|
|
|
|
})?;
|
2018-10-09 21:18:40 +02:00
|
|
|
let theme_set: ThemeSet = from_reader(BufReader::new(theme_set_file))
|
|
|
|
.chain_err(|| "Could not parse cached theme set")?;
|
2018-05-10 12:36:09 +02:00
|
|
|
|
|
|
|
Ok(HighlightingAssets {
|
|
|
|
syntax_set,
|
|
|
|
theme_set,
|
2020-03-21 20:31:32 +01:00
|
|
|
fallback_theme: None,
|
2018-05-10 12:36:09 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-09 21:18:40 +02:00
|
|
|
fn get_integrated_syntaxset() -> SyntaxSet {
|
|
|
|
from_binary(include_bytes!("../assets/syntaxes.bin"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_integrated_themeset() -> ThemeSet {
|
|
|
|
from_binary(include_bytes!("../assets/themes.bin"))
|
|
|
|
}
|
|
|
|
|
2020-03-21 16:48:27 +01:00
|
|
|
pub fn from_binary() -> Self {
|
2018-10-09 21:18:40 +02:00
|
|
|
let syntax_set = Self::get_integrated_syntaxset();
|
|
|
|
let theme_set = Self::get_integrated_themeset();
|
2018-05-10 12:36:09 +02:00
|
|
|
|
|
|
|
HighlightingAssets {
|
|
|
|
syntax_set,
|
|
|
|
theme_set,
|
2020-03-21 20:31:32 +01:00
|
|
|
fallback_theme: None,
|
2018-05-10 12:36:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:19:07 +02:00
|
|
|
pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {
|
2019-05-30 17:47:40 +02:00
|
|
|
let _ = fs::create_dir_all(target_dir);
|
2018-05-16 21:22:16 +02:00
|
|
|
let theme_set_path = target_dir.join("themes.bin");
|
|
|
|
let syntax_set_path = target_dir.join("syntaxes.bin");
|
2018-05-10 12:36:09 +02:00
|
|
|
|
|
|
|
print!(
|
|
|
|
"Writing theme set to {} ... ",
|
|
|
|
theme_set_path.to_string_lossy()
|
|
|
|
);
|
2018-05-16 22:23:53 +02:00
|
|
|
dump_to_file(&self.theme_set, &theme_set_path).chain_err(|| {
|
|
|
|
format!(
|
|
|
|
"Could not save theme set to {}",
|
|
|
|
theme_set_path.to_string_lossy()
|
2018-05-10 12:36:09 +02:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
println!("okay");
|
|
|
|
|
|
|
|
print!(
|
|
|
|
"Writing syntax set to {} ... ",
|
|
|
|
syntax_set_path.to_string_lossy()
|
|
|
|
);
|
2018-05-16 22:23:53 +02:00
|
|
|
dump_to_file(&self.syntax_set, &syntax_set_path).chain_err(|| {
|
|
|
|
format!(
|
|
|
|
"Could not save syntax set to {}",
|
|
|
|
syntax_set_path.to_string_lossy()
|
2018-05-10 12:36:09 +02:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
println!("okay");
|
|
|
|
|
2020-04-21 15:50:46 +02:00
|
|
|
print!(
|
|
|
|
"Writing metadata to folder {} ... ",
|
|
|
|
target_dir.to_string_lossy()
|
|
|
|
);
|
2020-04-21 17:19:07 +02:00
|
|
|
AssetsMetadata::new(current_version).save_to_folder(target_dir)?;
|
2020-04-21 15:50:46 +02:00
|
|
|
println!("okay");
|
|
|
|
|
2018-05-10 12:36:09 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:31:32 +01:00
|
|
|
pub fn set_fallback_theme(&mut self, theme: &'static str) {
|
|
|
|
self.fallback_theme = Some(theme);
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:01:36 +01:00
|
|
|
pub fn syntaxes(&self) -> &[SyntaxReference] {
|
|
|
|
self.syntax_set.syntaxes()
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:16:32 +01:00
|
|
|
pub fn themes(&self) -> impl Iterator<Item = &String> {
|
|
|
|
self.theme_set.themes.keys()
|
2020-03-21 20:01:36 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 20:16:32 +01:00
|
|
|
pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
|
2018-07-23 21:38:45 +02:00
|
|
|
match self.theme_set.themes.get(theme) {
|
|
|
|
Some(theme) => theme,
|
|
|
|
None => {
|
2020-03-21 21:45:03 +01:00
|
|
|
if theme != "" {
|
|
|
|
use ansi_term::Colour::Yellow;
|
|
|
|
eprintln!(
|
|
|
|
"{}: Unknown theme '{}', using default.",
|
|
|
|
Yellow.paint("[bat warning]"),
|
|
|
|
theme
|
|
|
|
);
|
|
|
|
}
|
2020-03-21 20:31:32 +01:00
|
|
|
&self.theme_set.themes[self.fallback_theme.unwrap_or(Self::default_theme())]
|
2018-07-23 21:38:45 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-21 08:26:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 20:42:10 +01:00
|
|
|
pub(crate) fn get_syntax(
|
2018-10-07 13:26:50 +02:00
|
|
|
&self,
|
|
|
|
language: Option<&str>,
|
2020-04-21 20:06:09 +02:00
|
|
|
file: &InputFile,
|
2018-10-07 13:26:50 +02:00
|
|
|
reader: &mut InputFileReader,
|
2018-10-17 22:30:09 +02:00
|
|
|
mapping: &SyntaxMapping,
|
2018-10-09 21:18:40 +02:00
|
|
|
) -> &SyntaxReference {
|
2020-04-05 02:49:55 +02:00
|
|
|
let syntax = match (language, file) {
|
|
|
|
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
|
|
|
(None, InputFile::Ordinary(ofile)) => {
|
2020-04-21 08:19:24 +02:00
|
|
|
let path = Path::new(ofile.provided_path());
|
2020-04-19 16:56:53 +02:00
|
|
|
let line_syntax = self.get_first_line_syntax(reader);
|
2020-03-22 09:55:13 +01:00
|
|
|
|
2020-03-22 10:19:35 +01:00
|
|
|
let absolute_path = path.canonicalize().ok().unwrap_or(path.to_owned());
|
|
|
|
match mapping.get_syntax_for(absolute_path) {
|
2020-03-22 09:55:13 +01:00
|
|
|
Some(MappingTarget::MapTo(syntax_name)) => {
|
|
|
|
// TODO: we should probably return an error here if this syntax can not be
|
|
|
|
// found. Currently, we just fall back to 'plain'.
|
|
|
|
self.syntax_set.find_syntax_by_name(syntax_name)
|
|
|
|
}
|
|
|
|
Some(MappingTarget::MapToUnknown) => line_syntax,
|
2020-04-21 08:19:24 +02:00
|
|
|
None => {
|
|
|
|
let file_name = path.file_name().unwrap_or_default();
|
|
|
|
self.get_extension_syntax(file_name).or(line_syntax)
|
|
|
|
}
|
2020-03-22 09:55:13 +01:00
|
|
|
}
|
2018-05-18 12:30:30 +02:00
|
|
|
}
|
2020-04-05 02:49:55 +02:00
|
|
|
(None, InputFile::StdIn(None)) => String::from_utf8(reader.first_line.clone())
|
2018-10-07 14:31:23 +02:00
|
|
|
.ok()
|
|
|
|
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)),
|
2020-04-05 02:49:55 +02:00
|
|
|
(None, InputFile::StdIn(Some(file_name))) => self
|
2020-04-21 20:06:09 +02:00
|
|
|
.get_extension_syntax(&file_name)
|
2020-04-19 16:56:53 +02:00
|
|
|
.or(self.get_first_line_syntax(reader)),
|
2020-04-05 02:49:55 +02:00
|
|
|
(_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
|
2018-05-18 12:30:30 +02:00
|
|
|
};
|
|
|
|
|
2018-05-19 11:46:41 +02:00
|
|
|
syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
|
2018-05-18 12:30:30 +02:00
|
|
|
}
|
2020-04-19 16:56:53 +02:00
|
|
|
|
2020-04-21 08:19:24 +02:00
|
|
|
fn get_extension_syntax(&self, file_name: &OsStr) -> Option<&SyntaxReference> {
|
2020-04-19 16:56:53 +02:00
|
|
|
self.syntax_set
|
2020-04-21 08:19:24 +02:00
|
|
|
.find_syntax_by_extension(file_name.to_str().unwrap_or_default())
|
2020-04-19 16:56:53 +02:00
|
|
|
.or_else(|| {
|
|
|
|
self.syntax_set.find_syntax_by_extension(
|
|
|
|
Path::new(file_name)
|
|
|
|
.extension()
|
|
|
|
.and_then(|x| x.to_str())
|
2020-04-21 08:19:24 +02:00
|
|
|
.unwrap_or_default(),
|
2020-04-19 16:56:53 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_first_line_syntax(&self, reader: &mut InputFileReader) -> Option<&SyntaxReference> {
|
|
|
|
String::from_utf8(reader.first_line.clone())
|
|
|
|
.ok()
|
|
|
|
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
|
|
|
|
}
|
2020-03-21 19:35:04 +01:00
|
|
|
}
|
2020-03-21 20:42:10 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-04-21 20:06:09 +02:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::inputfile::OrdinaryFile;
|
|
|
|
|
|
|
|
use std::ffi::{OsStr, OsString};
|
2020-03-21 20:42:10 +01:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
use tempdir::TempDir;
|
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
struct SyntaxDetectionTest<'a> {
|
2020-03-21 20:42:10 +01:00
|
|
|
assets: HighlightingAssets,
|
2020-03-22 09:55:13 +01:00
|
|
|
pub syntax_mapping: SyntaxMapping<'a>,
|
2020-03-21 20:42:10 +01:00
|
|
|
temp_dir: TempDir,
|
|
|
|
}
|
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
impl<'a> SyntaxDetectionTest<'a> {
|
2020-03-21 20:42:10 +01:00
|
|
|
fn new() -> Self {
|
|
|
|
SyntaxDetectionTest {
|
|
|
|
assets: HighlightingAssets::from_binary(),
|
2020-03-22 09:55:13 +01:00
|
|
|
syntax_mapping: SyntaxMapping::builtin(),
|
2020-03-21 20:42:10 +01:00
|
|
|
temp_dir: TempDir::new("bat_syntax_detection_tests")
|
|
|
|
.expect("creation of temporary directory"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:19:24 +02:00
|
|
|
fn syntax_for_file_with_content_os(&self, file_name: &OsStr, first_line: &str) -> String {
|
2020-03-21 20:42:10 +01:00
|
|
|
let file_path = self.temp_dir.path().join(file_name);
|
|
|
|
{
|
|
|
|
let mut temp_file = File::create(&file_path).unwrap();
|
|
|
|
writeln!(temp_file, "{}", first_line).unwrap();
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:19:24 +02:00
|
|
|
let input_file = InputFile::Ordinary(OrdinaryFile::from_path(file_path.as_os_str()));
|
2020-03-21 20:42:10 +01:00
|
|
|
let syntax = self.assets.get_syntax(
|
|
|
|
None,
|
2020-04-21 20:06:09 +02:00
|
|
|
&input_file,
|
2020-04-18 11:37:12 +02:00
|
|
|
&mut input_file.get_reader(io::stdin().lock()).unwrap(),
|
2020-03-21 20:42:10 +01:00
|
|
|
&self.syntax_mapping,
|
|
|
|
);
|
|
|
|
|
|
|
|
syntax.name.clone()
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:19:24 +02:00
|
|
|
fn syntax_for_file_os(&self, file_name: &OsStr) -> String {
|
|
|
|
self.syntax_for_file_with_content_os(file_name, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn syntax_for_file_with_content(&self, file_name: &str, first_line: &str) -> String {
|
|
|
|
self.syntax_for_file_with_content_os(OsStr::new(file_name), first_line)
|
|
|
|
}
|
|
|
|
|
2020-03-22 10:54:37 +01:00
|
|
|
fn syntax_for_file(&self, file_name: &str) -> String {
|
2020-04-05 02:49:55 +02:00
|
|
|
self.syntax_for_file_with_content(file_name, "")
|
2020-03-21 20:42:10 +01:00
|
|
|
}
|
2020-04-18 11:37:12 +02:00
|
|
|
|
|
|
|
fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String {
|
2020-04-21 20:06:09 +02:00
|
|
|
let input_file = InputFile::StdIn(Some(OsString::from(file_name)));
|
2020-04-18 11:37:12 +02:00
|
|
|
let syntax = self.assets.get_syntax(
|
|
|
|
None,
|
2020-04-21 20:06:09 +02:00
|
|
|
&input_file,
|
2020-04-18 11:37:12 +02:00
|
|
|
&mut input_file.get_reader(content).unwrap(),
|
|
|
|
&self.syntax_mapping,
|
|
|
|
);
|
|
|
|
syntax.name.clone()
|
|
|
|
}
|
2020-03-21 20:42:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn syntax_detection_basic() {
|
|
|
|
let test = SyntaxDetectionTest::new();
|
|
|
|
|
2020-03-22 10:54:37 +01:00
|
|
|
assert_eq!(test.syntax_for_file("test.rs"), "Rust");
|
|
|
|
assert_eq!(test.syntax_for_file("test.cpp"), "C++");
|
|
|
|
assert_eq!(test.syntax_for_file("test.build"), "NAnt Build File");
|
|
|
|
assert_eq!(
|
|
|
|
test.syntax_for_file("PKGBUILD"),
|
|
|
|
"Bourne Again Shell (bash)"
|
|
|
|
);
|
|
|
|
assert_eq!(test.syntax_for_file(".bashrc"), "Bourne Again Shell (bash)");
|
|
|
|
assert_eq!(test.syntax_for_file("Makefile"), "Makefile");
|
2020-03-21 20:42:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 08:19:24 +02:00
|
|
|
#[cfg(unix)]
|
|
|
|
#[test]
|
|
|
|
fn syntax_detection_invalid_utf8() {
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
|
|
|
|
let test = SyntaxDetectionTest::new();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test.syntax_for_file_os(OsStr::from_bytes(b"invalid_\xFEutf8_filename.rs")),
|
|
|
|
"Rust"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:42:10 +01:00
|
|
|
#[test]
|
|
|
|
fn syntax_detection_well_defined_mapping_for_duplicate_extensions() {
|
|
|
|
let test = SyntaxDetectionTest::new();
|
|
|
|
|
2020-03-22 10:54:37 +01:00
|
|
|
assert_eq!(test.syntax_for_file("test.h"), "C++");
|
|
|
|
assert_eq!(test.syntax_for_file("test.sass"), "Sass");
|
|
|
|
assert_eq!(test.syntax_for_file("test.hs"), "Haskell (improved)");
|
|
|
|
assert_eq!(test.syntax_for_file("test.js"), "JavaScript (Babel)");
|
2020-03-21 20:42:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn syntax_detection_first_line() {
|
|
|
|
let test = SyntaxDetectionTest::new();
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-04-05 02:49:55 +02:00
|
|
|
test.syntax_for_file_with_content("my_script", "#!/bin/bash"),
|
2020-03-21 20:42:10 +01:00
|
|
|
"Bourne Again Shell (bash)"
|
|
|
|
);
|
2020-03-22 09:55:13 +01:00
|
|
|
assert_eq!(
|
2020-04-05 02:49:55 +02:00
|
|
|
test.syntax_for_file_with_content("build", "#!/bin/bash"),
|
2020-03-22 09:55:13 +01:00
|
|
|
"Bourne Again Shell (bash)"
|
|
|
|
);
|
2020-03-22 10:54:37 +01:00
|
|
|
assert_eq!(
|
2020-04-05 02:49:55 +02:00
|
|
|
test.syntax_for_file_with_content("my_script", "<?php"),
|
2020-03-22 10:54:37 +01:00
|
|
|
"PHP"
|
|
|
|
);
|
2020-03-21 20:42:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn syntax_detection_with_custom_mapping() {
|
|
|
|
let mut test = SyntaxDetectionTest::new();
|
|
|
|
|
2020-03-22 10:54:37 +01:00
|
|
|
assert_eq!(test.syntax_for_file("test.h"), "C++");
|
2020-03-22 09:55:13 +01:00
|
|
|
test.syntax_mapping
|
2020-03-22 10:37:35 +01:00
|
|
|
.insert("*.h", MappingTarget::MapTo("C"))
|
2020-03-22 09:55:13 +01:00
|
|
|
.ok();
|
2020-03-22 10:54:37 +01:00
|
|
|
assert_eq!(test.syntax_for_file("test.h"), "C");
|
2020-03-21 20:42:10 +01:00
|
|
|
}
|
2020-03-22 09:55:13 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn syntax_detection_is_case_sensitive() {
|
|
|
|
let mut test = SyntaxDetectionTest::new();
|
|
|
|
|
2020-03-22 10:54:37 +01:00
|
|
|
assert_ne!(test.syntax_for_file("README.MD"), "Markdown");
|
2020-03-22 09:55:13 +01:00
|
|
|
test.syntax_mapping
|
|
|
|
.insert("*.MD", MappingTarget::MapTo("Markdown"))
|
|
|
|
.ok();
|
2020-03-22 10:54:37 +01:00
|
|
|
assert_eq!(test.syntax_for_file("README.MD"), "Markdown");
|
2020-03-22 09:55:13 +01:00
|
|
|
}
|
2020-03-26 23:49:16 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn syntax_detection_stdin_filename() {
|
|
|
|
let test = SyntaxDetectionTest::new();
|
|
|
|
|
|
|
|
// from file extension
|
2020-04-18 11:37:12 +02:00
|
|
|
assert_eq!(test.syntax_for_stdin_with_content("test.cpp", b"a"), "C++");
|
2020-03-26 23:49:16 +01:00
|
|
|
// from first line (fallback)
|
|
|
|
assert_eq!(
|
2020-04-18 11:37:12 +02:00
|
|
|
test.syntax_for_stdin_with_content("my_script", b"#!/bin/bash"),
|
2020-03-26 23:49:16 +01:00
|
|
|
"Bourne Again Shell (bash)"
|
|
|
|
);
|
|
|
|
}
|
2020-03-21 20:42:10 +01:00
|
|
|
}
|