on master help sign in
Add more default mappings, reverse traversal
commitdfd3ef022ec68195a47b2f76ef683125b8d46d44 authorsharkdp <davidpeter@web.de> dateSun 22 Mar 2020, 10:37 (7 years ago) parent978def2
2 files changed+31−4
src/assets.rs+4−3 src/syntax_mapping.rs+27−1
src/assets.rs+4−3
@@ -285,6 +285,7 @@ mod tests {
285285 fn syntax_detection_well_defined_mapping_for_duplicate_extensions() {
286286 let test = SyntaxDetectionTest::new();
287287
288+ assert_eq!(test.syntax_name("test.h"), "C++");
288289 assert_eq!(test.syntax_name("test.sass"), "Sass");
289290 assert_eq!(test.syntax_name("test.hs"), "Haskell (improved)");
290291 assert_eq!(test.syntax_name("test.js"), "JavaScript (Babel)");
@@ -309,11 +310,11 @@ mod tests {
309310 fn syntax_detection_with_custom_mapping() {
310311 let mut test = SyntaxDetectionTest::new();
311312
312− assert_ne!(test.syntax_name("test.h"), "C++");
313+ assert_eq!(test.syntax_name("test.h"), "C++");
313314 test.syntax_mapping
314− .insert("*.h", MappingTarget::MapTo("C++"))
315+ .insert("*.h", MappingTarget::MapTo("C"))
315316 .ok();
316− assert_eq!(test.syntax_name("test.h"), "C++");
317+ assert_eq!(test.syntax_name("test.h"), "C");
317318 }
318319
319320 #[test]
src/syntax_mapping.rs+27−1
@@ -22,9 +22,19 @@ impl<'a> SyntaxMapping<'a> {
2222
2323 pub fn builtin() -> SyntaxMapping<'a> {
2424 let mut mapping = Self::empty();
25+ mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
2526 mapping
2627 .insert("build", MappingTarget::MapToUnknown)
2728 .unwrap();
29+ mapping
30+ .insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
31+ .unwrap();
32+ mapping
33+ .insert(
34+ "/etc/profile",
35+ MappingTarget::MapTo("Bourne Again Shell (bash)"),
36+ )
37+ .unwrap();
2838
2939 mapping
3040 }
@@ -41,7 +51,7 @@ impl<'a> SyntaxMapping<'a> {
4151 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
4252 let candidate = Candidate::new(path.as_ref());
4353 let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
44− for (ref glob, ref syntax) in &self.mappings {
54+ for (ref glob, ref syntax) in self.mappings.iter().rev() {
4555 if glob.is_match_candidate(&candidate)
4656 || canddidate_filename
4757 .as_ref()
@@ -74,6 +84,22 @@ fn basic() {
7484 );
7585}
7686
87+#[test]
88+fn user_can_override_builtin_mappings() {
89+ let mut map = SyntaxMapping::builtin();
90+
91+ assert_eq!(
92+ map.get_syntax_for("/etc/profile"),
93+ Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
94+ );
95+ map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
96+ .ok();
97+ assert_eq!(
98+ map.get_syntax_for("/etc/profile"),
99+ Some(MappingTarget::MapTo("My Syntax"))
100+ );
101+}
102+
77103#[test]
78104fn builtin_mappings() {
79105 let map = SyntaxMapping::builtin();