@@ -153,9 +153,12 @@ impl HighlightingAssets {
153153 }
154154
155155 /// Detect the syntax based on, in order:
156− /// 1. Syntax mappings (e.g. `/etc/profile` -> `Bourne Again Shell (bash)`)
156+ /// 1. Syntax mappings with [MappingTarget::MapTo] and [MappingTarget::MapToUnknown]
157+ /// (e.g. `/etc/profile` -> `Bourne Again Shell (bash)`)
157158 /// 2. The file name (e.g. `Dockerfile`)
158− /// 3. The file name extension (e.g. `.rs`)
159+ /// 3. Syntax mappings with [MappingTarget::MapExtensionToUnknown]
160+ /// (e.g. `*.conf`)
161+ /// 4. The file name extension (e.g. `.rs`)
159162 ///
160163 /// When detecting syntax based on syntax mappings, the full path is taken
161164 /// into account. When detecting syntax based on file name, no regard is
@@ -165,9 +168,9 @@ impl HighlightingAssets {
165168 ///
166169 /// Returns [Error::UndetectedSyntax] if it was not possible detect syntax
167170 /// based on path/file name/extension (or if the path was mapped to
168− /// [MappingTarget::MapToUnknown]). In this case it is appropriate to fall
169− /// back to other methods to detect syntax. Such as using the contents of
170− /// the first line of the file.
171+ /// [MappingTarget::MapToUnknown] or [MappingTarget::MapExtensionToUnknown]).
172+ /// In this case it is appropriate to fall back to other methods to detect
173+ /// syntax. Such as using the contents of the first line of the file.
171174 ///
172175 /// Returns [Error::UnknownSyntax] if a syntax mapping exist, but the mapped
173176 /// syntax does not exist.
@@ -177,20 +180,31 @@ impl HighlightingAssets {
177180 mapping: &SyntaxMapping,
178181 ) -> Result<SyntaxReferenceInSet> {
179182 let path = path.as_ref();
180− match mapping.get_syntax_for(path) {
181− Some(MappingTarget::MapToUnknown) => {
182− Err(Error::UndetectedSyntax(path.to_string_lossy().into()))
183− }
184183
185− Some(MappingTarget::MapTo(syntax_name)) => self
184+ let syntax_match = mapping.get_syntax_for(path);
185+
186+ if let Some(MappingTarget::MapToUnknown) = syntax_match {
187+ return Err(Error::UndetectedSyntax(path.to_string_lossy().into()));
188+ }
189+
190+ if let Some(MappingTarget::MapTo(syntax_name)) = syntax_match {
191+ return self
186192 .find_syntax_by_name(syntax_name)?
187− .ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned())),
193+ .ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned()));
194+ }
188195
189− None => {
190− let file_name = path.file_name().unwrap_or_default();
191− self.get_extension_syntax(file_name)?
192− .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
196+ let file_name = path.file_name().unwrap_or_default();
197+
198+ match (self.get_syntax_for_file_name(file_name)?, syntax_match) {
199+ (Some(syntax), _) => Ok(syntax),
200+
201+ (_, Some(MappingTarget::MapExtensionToUnknown)) => {
202+ Err(Error::UndetectedSyntax(path.to_string_lossy().into()))
193203 }
204+
205+ _ => self
206+ .get_syntax_for_file_extension(file_name)?
207+ .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into())),
194208 }
195209 }
196210
@@ -263,14 +277,24 @@ impl HighlightingAssets {
263277 .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
264278 }
265279
266− fn get_extension_syntax(&self, file_name: &OsStr) -> Result<Option<SyntaxReferenceInSet>> {
280+ fn get_syntax_for_file_name(&self, file_name: &OsStr) -> Result<Option<SyntaxReferenceInSet>> {
267281 let mut syntax = self.find_syntax_by_extension(Some(file_name))?;
268282 if syntax.is_none() {
269− syntax = self.find_syntax_by_extension(Path::new(file_name).extension())?;
283+ syntax = try_with_stripped_suffix(file_name, |stripped_file_name| {
284+ self.get_syntax_for_file_name(stripped_file_name) // Note: recursion
285+ })?;
270286 }
287+ Ok(syntax)
288+ }
289+
290+ fn get_syntax_for_file_extension(
291+ &self,
292+ file_name: &OsStr,
293+ ) -> Result<Option<SyntaxReferenceInSet>> {
294+ let mut syntax = self.find_syntax_by_extension(Path::new(file_name).extension())?;
271295 if syntax.is_none() {
272296 syntax = try_with_stripped_suffix(file_name, |stripped_file_name| {
273− self.get_extension_syntax(stripped_file_name) // Note: recursion
297+ self.get_syntax_for_file_extension(stripped_file_name) // Note: recursion
274298 })?;
275299 }
276300 Ok(syntax)
@@ -530,6 +554,42 @@ mod tests {
530554 assert_eq!(test.syntax_for_file("test.h"), "C");
531555 }
532556
557+ #[test]
558+ fn syntax_detection_with_extension_mapping_to_unknown() {
559+ let mut test = SyntaxDetectionTest::new();
560+
561+ // Normally, a CMakeLists.txt file shall use the CMake syntax, even if it is
562+ // a bash script in disguise
563+ assert_eq!(
564+ test.syntax_for_file_with_content("CMakeLists.txt", "#!/bin/bash"),
565+ "CMake"
566+ );
567+
568+ // Other .txt files shall use the Plain Text syntax
569+ assert_eq!(
570+ test.syntax_for_file_with_content("some-other.txt", "#!/bin/bash"),
571+ "Plain Text"
572+ );
573+
574+ // If we setup MapExtensionToUnknown on *.txt, the match on the full
575+ // file name of "CMakeLists.txt" shall have higher prio, and CMake shall
576+ // still be used for it
577+ test.syntax_mapping
578+ .insert("*.txt", MappingTarget::MapExtensionToUnknown)
579+ .ok();
580+ assert_eq!(
581+ test.syntax_for_file_with_content("CMakeLists.txt", "#!/bin/bash"),
582+ "CMake"
583+ );
584+
585+ // However, for *other* files with a .txt extension, first-line fallback
586+ // shall now be used
587+ assert_eq!(
588+ test.syntax_for_file_with_content("some-other.txt", "#!/bin/bash"),
589+ "Bourne Again Shell (bash)"
590+ );
591+ }
592+
533593 #[test]
534594 fn syntax_detection_is_case_sensitive() {
535595 let mut test = SyntaxDetectionTest::new();