From dfd3ef022ec68195a47b2f76ef683125b8d46d44 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 22 Mar 2020 10:37:35 +0100 Subject: [PATCH] Add more default mappings, reverse traversal --- src/assets.rs | 9 +++++---- src/syntax_mapping.rs | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index 4d4bd184..23f82344 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -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] diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index 84c89d32..72cb021c 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -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) -> Option> { 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();