on master help sign in
Add more default mappings, reverse traversal
dfd3ef0sharkdp
src/assets.rs+4−3
@@ -285,6 +285,7 @@ mod tests {
285 fn syntax_detection_well_defined_mapping_for_duplicate_extensions() {
285 fn syntax_detection_well_defined_mapping_for_duplicate_extensions() {
286 let test = SyntaxDetectionTest::new();
286 let test = SyntaxDetectionTest::new();
287
287
288+ assert_eq!(test.syntax_name("test.h"), "C++");
288 assert_eq!(test.syntax_name("test.sass"), "Sass");
289 assert_eq!(test.syntax_name("test.sass"), "Sass");
289 assert_eq!(test.syntax_name("test.hs"), "Haskell (improved)");
290 assert_eq!(test.syntax_name("test.hs"), "Haskell (improved)");
290 assert_eq!(test.syntax_name("test.js"), "JavaScript (Babel)");
291 assert_eq!(test.syntax_name("test.js"), "JavaScript (Babel)");
@@ -309,11 +310,11 @@ mod tests {
309 fn syntax_detection_with_custom_mapping() {
310 fn syntax_detection_with_custom_mapping() {
310 let mut test = SyntaxDetectionTest::new();
311 let mut test = SyntaxDetectionTest::new();
311
312
312− assert_ne!(test.syntax_name("test.h"), "C++");
313+ assert_eq!(test.syntax_name("test.h"), "C++");
313 test.syntax_mapping
314 test.syntax_mapping
314− .insert("*.h", MappingTarget::MapTo("C++"))
315+ .insert("*.h", MappingTarget::MapTo("C"))
315 .ok();
316 .ok();
316− assert_eq!(test.syntax_name("test.h"), "C++");
317+ assert_eq!(test.syntax_name("test.h"), "C");
317 }
318 }
318
319
319 #[test]
320 #[test]
src/syntax_mapping.rs+27−1
@@ -22,9 +22,19 @@ impl<'a> SyntaxMapping<'a> {
22
22
23 pub fn builtin() -> SyntaxMapping<'a> {
23 pub fn builtin() -> SyntaxMapping<'a> {
24 let mut mapping = Self::empty();
24 let mut mapping = Self::empty();
25+ mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
25 mapping
26 mapping
26 .insert("build", MappingTarget::MapToUnknown)
27 .insert("build", MappingTarget::MapToUnknown)
27 .unwrap();
28 .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();
28
38
29 mapping
39 mapping
30 }
40 }
@@ -41,7 +51,7 @@ impl<'a> SyntaxMapping<'a> {
41 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
51 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
42 let candidate = Candidate::new(path.as_ref());
52 let candidate = Candidate::new(path.as_ref());
43 let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
53 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() {
45 if glob.is_match_candidate(&candidate)
55 if glob.is_match_candidate(&candidate)
46 || canddidate_filename
56 || canddidate_filename
47 .as_ref()
57 .as_ref()
@@ -74,6 +84,22 @@ fn basic() {
74 );
84 );
75}
85}
76
86
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+
77#[test]
103#[test]
78fn builtin_mappings() {
104fn builtin_mappings() {
79 let map = SyntaxMapping::builtin();
105 let map = SyntaxMapping::builtin();