mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-19 04:21:06 +00:00
Handle file extension conflicts in --list-languages
This commit is contained in:
parent
31fb7087f1
commit
c477e23fe9
@ -170,6 +170,21 @@ impl HighlightingAssets {
|
|||||||
self.theme_set.themes.keys().map(|s| s.as_ref())
|
self.theme_set.themes.keys().map(|s| s.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn syntax_for_file_name(
|
||||||
|
&self,
|
||||||
|
file_name: impl AsRef<Path>,
|
||||||
|
mapping: &SyntaxMapping,
|
||||||
|
) -> Option<&SyntaxReference> {
|
||||||
|
let file_name = file_name.as_ref();
|
||||||
|
match mapping.get_syntax_for(file_name) {
|
||||||
|
Some(MappingTarget::MapToUnknown) => None,
|
||||||
|
Some(MappingTarget::MapTo(syntax_name)) => {
|
||||||
|
self.syntax_set.find_syntax_by_name(syntax_name)
|
||||||
|
}
|
||||||
|
None => self.get_extension_syntax(file_name.as_os_str()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
|
pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
|
||||||
match self.theme_set.themes.get(theme) {
|
match self.theme_set.themes.get(theme) {
|
||||||
Some(theme) => theme,
|
Some(theme) => theme,
|
||||||
|
@ -86,6 +86,29 @@ pub fn list_languages(config: &Config) -> Result<()> {
|
|||||||
.filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
|
.filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect::<Vec<_>>();
|
.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());
|
languages.sort_by_key(|lang| lang.name.to_uppercase());
|
||||||
|
|
||||||
let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.mappings());
|
let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.mappings());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user