From 2bad4df14e6bde8a4962b25ae138d3b1bf079e82 Mon Sep 17 00:00:00 2001 From: Kienyew Date: Sat, 8 Aug 2020 12:01:02 +0800 Subject: [PATCH] Handle file extension conflicts in --list-languages (#1076) --- src/bin/bat/main.rs | 13 +++++++++++++ src/syntax_mapping.rs | 20 ++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 65129fa7..03fff166 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -86,6 +86,19 @@ pub fn list_languages(config: &Config) -> Result<()> { .filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty()) .cloned() .collect::>(); + + for lang in languages.iter_mut() { + let lang_name = lang.name.clone(); + lang.file_extensions.retain(|extension| { + let test_file = Path::new("test").with_extension(extension); + match config.syntax_mapping.get_syntax_for(test_file) { + Some(MappingTarget::MapTo(primary_lang)) => lang_name == primary_lang, + Some(MappingTarget::MapToUnknown) => true, + None => true, + } + }); + } + languages.sort_by_key(|lang| lang.name.to_uppercase()); let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.mappings()); diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index 0b65ae65..91a0976d 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -24,6 +24,12 @@ impl<'a> SyntaxMapping<'a> { let mut mapping = Self::empty(); mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap(); mapping.insert("*.fs", MappingTarget::MapTo("F#")).unwrap(); + mapping + .insert("*.js", MappingTarget::MapTo("JavaScript (Babel)")) + .unwrap(); + mapping + .insert("*.sass", MappingTarget::MapTo("Sass")) + .unwrap(); mapping .insert("build", MappingTarget::MapToUnknown) .unwrap(); @@ -67,14 +73,16 @@ impl<'a> SyntaxMapping<'a> { "*.swap", "*.target", "*.timer", - ].iter() { - mapping - .insert(glob, MappingTarget::MapTo("INI")) - .unwrap(); + ] + .iter() + { + mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap(); } // pacman hooks - mapping.insert("*.hook", MappingTarget::MapTo("INI")).unwrap(); + mapping + .insert("*.hook", MappingTarget::MapTo("INI")) + .unwrap(); mapping } @@ -92,7 +100,7 @@ impl<'a> SyntaxMapping<'a> { &self.mappings } - pub(crate) fn get_syntax_for(&self, path: impl AsRef) -> Option> { + pub 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.iter().rev() {