2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-04 18:00:24 +00:00

Add more default mappings, reverse traversal

This commit is contained in:
sharkdp 2020-03-22 10:37:35 +01:00 committed by David Peter
parent 978def2d40
commit dfd3ef022e
2 changed files with 32 additions and 5 deletions

View File

@ -285,6 +285,7 @@ mod tests {
fn syntax_detection_well_defined_mapping_for_duplicate_extensions() {
let test = SyntaxDetectionTest::new();
assert_eq!(test.syntax_name("test.h"), "C++");
assert_eq!(test.syntax_name("test.sass"), "Sass");
assert_eq!(test.syntax_name("test.hs"), "Haskell (improved)");
assert_eq!(test.syntax_name("test.js"), "JavaScript (Babel)");
@ -309,11 +310,11 @@ mod tests {
fn syntax_detection_with_custom_mapping() {
let mut test = SyntaxDetectionTest::new();
assert_ne!(test.syntax_name("test.h"), "C++");
test.syntax_mapping
.insert("*.h", MappingTarget::MapTo("C++"))
.ok();
assert_eq!(test.syntax_name("test.h"), "C++");
test.syntax_mapping
.insert("*.h", MappingTarget::MapTo("C"))
.ok();
assert_eq!(test.syntax_name("test.h"), "C");
}
#[test]

View File

@ -22,9 +22,19 @@ impl<'a> SyntaxMapping<'a> {
pub fn builtin() -> SyntaxMapping<'a> {
let mut mapping = Self::empty();
mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
mapping
.insert("build", MappingTarget::MapToUnknown)
.unwrap();
mapping
.insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
.unwrap();
mapping
.insert(
"/etc/profile",
MappingTarget::MapTo("Bourne Again Shell (bash)"),
)
.unwrap();
mapping
}
@ -41,7 +51,7 @@ impl<'a> SyntaxMapping<'a> {
pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
let candidate = Candidate::new(path.as_ref());
let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
for (ref glob, ref syntax) in &self.mappings {
for (ref glob, ref syntax) in self.mappings.iter().rev() {
if glob.is_match_candidate(&candidate)
|| canddidate_filename
.as_ref()
@ -74,6 +84,22 @@ fn basic() {
);
}
#[test]
fn user_can_override_builtin_mappings() {
let mut map = SyntaxMapping::builtin();
assert_eq!(
map.get_syntax_for("/etc/profile"),
Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
);
map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
.ok();
assert_eq!(
map.get_syntax_for("/etc/profile"),
Some(MappingTarget::MapTo("My Syntax"))
);
}
#[test]
fn builtin_mappings() {
let map = SyntaxMapping::builtin();