Description
A Plain-file rule that combines a filename regex with a MIME regex can fail even when both values should match. PlainSettings.test(filename:mimeType:) builds the MIME search range from filename rather than mimeType:
regex.firstMatch(
in: mimeType,
options: [],
range: NSRange(filename.startIndex..., in: filename)
)
For short filenames this truncates the MIME search range. For example, the filename hproxy produces a six-character range, so the pattern text/x-shellscript cannot match the detected MIME value.
Reproduction
Syntax Highlight 2.1.30 (79):
-
Create an executable extensionless file named hproxy beginning with #!/bin/bash.
-
Confirm its metadata:
/usr/bin/file --mime --brief hproxy
text/x-shellscript; charset=us-ascii
mdls -name kMDItemContentType hproxy
kMDItemContentType = "public.unix-executable"
-
Add a Plain text-file rule:
- Filename pattern:
.*
- MIME pattern:
text/x-shellscript
- Regular expression: enabled
- Syntax: Bash
-
Preview the file with Quick Look.
Expected
Both regexes match and the file is highlighted as Bash.
Actual
The file is not highlighted because MIME matching is limited to a range derived from the six-character filename.
Proposed fix
Construct the MIME range from mimeType:
range: NSRange(mimeType.startIndex..., in: mimeType)
A focused test should cover filenames shorter and longer than their MIME values.
Description
A Plain-file rule that combines a filename regex with a MIME regex can fail even when both values should match.
PlainSettings.test(filename:mimeType:)builds the MIME search range fromfilenamerather thanmimeType:For short filenames this truncates the MIME search range. For example, the filename
hproxyproduces a six-character range, so the patterntext/x-shellscriptcannot match the detected MIME value.Reproduction
Syntax Highlight 2.1.30 (79):
Create an executable extensionless file named
hproxybeginning with#!/bin/bash.Confirm its metadata:
Add a Plain text-file rule:
.*text/x-shellscriptPreview the file with Quick Look.
Expected
Both regexes match and the file is highlighted as Bash.
Actual
The file is not highlighted because MIME matching is limited to a range derived from the six-character filename.
Proposed fix
Construct the MIME range from
mimeType:A focused test should cover filenames shorter and longer than their MIME values.