@@ -153,9 +153,12 @@ impl HighlightingAssets {
155 /// Detect the syntax based on, in order:
155 /// Detect the syntax based on, in order:
156+ /// 1. Syntax mappings with [MappingTarget::MapTo] and [MappingTarget::MapToUnknown]
156− /// 1. Syntax mappings (e.g. `/etc/profile` -> `Bourne Again Shell (bash)`)
157+ /// (e.g. `/etc/profile` -> `Bourne Again Shell (bash)`)
157 /// 2. The file name (e.g. `Dockerfile`)
158 /// 2. The file name (e.g. `Dockerfile`)
159+ /// 3. Syntax mappings with [MappingTarget::MapExtensionToUnknown]
158− /// 3. The file name extension (e.g. `.rs`)
161+ /// 4. The file name extension (e.g. `.rs`)
160 /// When detecting syntax based on syntax mappings, the full path is taken
163 /// When detecting syntax based on syntax mappings, the full path is taken
161 /// into account. When detecting syntax based on file name, no regard is
164 /// into account. When detecting syntax based on file name, no regard is
@@ -165,9 +168,9 @@ impl HighlightingAssets {
166 /// Returns [Error::UndetectedSyntax] if it was not possible detect syntax
169 /// Returns [Error::UndetectedSyntax] if it was not possible detect syntax
167 /// based on path/file name/extension (or if the path was mapped to
170 /// based on path/file name/extension (or if the path was mapped to
171+ /// [MappingTarget::MapToUnknown] or [MappingTarget::MapExtensionToUnknown]).
168− /// [MappingTarget::MapToUnknown]). In this case it is appropriate to fall
172+ /// In this case it is appropriate to fall back to other methods to detect
169− /// back to other methods to detect syntax. Such as using the contents of
170− /// the first line of the file.
173+ /// syntax. Such as using the contents of the first line of the file.
172 /// Returns [Error::UnknownSyntax] if a syntax mapping exist, but the mapped
175 /// Returns [Error::UnknownSyntax] if a syntax mapping exist, but the mapped
173 /// syntax does not exist.
176 /// syntax does not exist.
@@ -177,20 +180,31 @@ impl HighlightingAssets {
177 mapping: &SyntaxMapping,
180 mapping: &SyntaxMapping,
178 ) -> Result<SyntaxReferenceInSet> {
181 ) -> Result<SyntaxReferenceInSet> {
179 let path = path.as_ref();
182 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()))
184+ let syntax_match = mapping.get_syntax_for(path);
186+ if let Some(MappingTarget::MapToUnknown) = syntax_match {
187+ return Err(Error::UndetectedSyntax(path.to_string_lossy().into()));
185− Some(MappingTarget::MapTo(syntax_name)) => self
190+ if let Some(MappingTarget::MapTo(syntax_name)) = syntax_match {
186 .find_syntax_by_name(syntax_name)?
192 .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()));
190− let file_name = path.file_name().unwrap_or_default();
196+ let file_name = path.file_name().unwrap_or_default();
191− self.get_extension_syntax(file_name)?
197+
198+ match (self.get_syntax_for_file_name(file_name)?, syntax_match) {
199+ (Some(syntax), _) => Ok(syntax),
201+ (_, Some(MappingTarget::MapExtensionToUnknown)) => {
192− .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
202+ Err(Error::UndetectedSyntax(path.to_string_lossy().into()))
206+ .get_syntax_for_file_extension(file_name)?
207+ .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into())),
@@ -263,14 +277,24 @@ impl HighlightingAssets {
263 .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
277 .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
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>> {
267 let mut syntax = self.find_syntax_by_extension(Some(file_name))?;
281 let mut syntax = self.find_syntax_by_extension(Some(file_name))?;
268 if syntax.is_none() {
282 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
290+ fn get_syntax_for_file_extension(
293+ ) -> Result<Option<SyntaxReferenceInSet>> {
294+ let mut syntax = self.find_syntax_by_extension(Path::new(file_name).extension())?;
271 if syntax.is_none() {
295 if syntax.is_none() {
272 syntax = try_with_stripped_suffix(file_name, |stripped_file_name| {
296 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
276 Ok(syntax)
300 Ok(syntax)
@@ -530,6 +554,42 @@ mod tests {
530 assert_eq!(test.syntax_for_file("test.h"), "C");
554 assert_eq!(test.syntax_for_file("test.h"), "C");
558+ fn syntax_detection_with_extension_mapping_to_unknown() {
559+ let mut test = SyntaxDetectionTest::new();
561+ // Normally, a CMakeLists.txt file shall use the CMake syntax, even if it is
562+ // a bash script in disguise
564+ test.syntax_for_file_with_content("CMakeLists.txt", "#!/bin/bash"),
568+ // Other .txt files shall use the Plain Text syntax
570+ test.syntax_for_file_with_content("some-other.txt", "#!/bin/bash"),
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
578+ .insert("*.txt", MappingTarget::MapExtensionToUnknown)
581+ test.syntax_for_file_with_content("CMakeLists.txt", "#!/bin/bash"),
585+ // However, for *other* files with a .txt extension, first-line fallback
586+ // shall now be used
588+ test.syntax_for_file_with_content("some-other.txt", "#!/bin/bash"),
589+ "Bourne Again Shell (bash)"
534 fn syntax_detection_is_case_sensitive() {
594 fn syntax_detection_is_case_sensitive() {
535 let mut test = SyntaxDetectionTest::new();
595 let mut test = SyntaxDetectionTest::new();