1
0
mirror of https://github.com/sharkdp/bat synced 2026-07-27 17:41:42 +00:00

Handle file extension conflicts in --list-languages

This commit is contained in:
Kienyew
2020-09-06 20:08:13 +08:00
committed by David Peter
parent 31fb7087f1
commit c477e23fe9
2 changed files with 38 additions and 0 deletions
+23
View File
@@ -86,6 +86,29 @@ pub fn list_languages(config: &Config) -> Result<()> {
.filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
.cloned()
.collect::<Vec<_>>();
// Handling of file-extension conflicts, see issue #1076
for lang in languages.iter_mut() {
let lang_name = lang.name.clone();
lang.file_extensions.retain(|extension| {
// The 'extension' variable is not certainly a real extension.
//
// Skip if 'extension' starts with '.', likely a hidden file like '.vimrc'
// Also skip if the 'extension' contains another real extension, likely
// that is a full match file name like 'CMakeLists.txt' and 'Cargo.lock'
if extension.starts_with('.') || Path::new(extension).extension().is_some() {
true
} else {
let test_file = Path::new("test").with_extension(extension);
if let Some(syntax) = assets.syntax_for_file_name(test_file, &config.syntax_mapping) {
syntax.name == lang_name
} else {
false
}
}
});
}
languages.sort_by_key(|lang| lang.name.to_uppercase());
let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.mappings());