Bug 400290

Summary: Syntax highlighting engine makes wrong guesses for XML files with different extension
Product: [Frameworks and Libraries] frameworks-syntax-highlighting Reporter: Ongun Kanat <ongun.kanat>
Component: frameworkAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: i, nibgonz, ongun.kanat
Priority: NOR    
Version First Reported In: 5.57.0   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed/Implemented In: 5.59.0
Sentry Crash Report:
Attachments: Example file. fontconfig configuration.

Description Ongun Kanat 2018-10-25 13:03:00 UTC
Created attachment 115888 [details]
Example file. fontconfig configuration.

SUMMARY
Syntax highlighting engine wrongly guesses the type of files written in XML but have different extensions such as .conf

STEPS TO REPRODUCE
1. Open a .conf file in Kate

OBSERVED RESULT
File type recognized as INI.

EXPECTED RESULT
File type should be recognized as XML.

SOFTWARE VERSIONS
(available in About System)
KDE Plasma Version: 5.14.2
KDE Frameworks Version: 5.51.0
Qt Version: 5.11.2

ADDITIONAL INFORMATION
n/a
Comment 1 Nibaldo G. 2018-10-29 06:04:55 UTC
The syntax highlighting is determined by the extension or MIME type. I think that the extension ".conf" is very generic so as to assign it to XML, since it could belong to any other language depending on the context.

In the case of Fontconfig, one solution (faster to implement) is to highlight as XML the files with the name "fonts.conf". However, other ".conf" files (ex: /etc/fonts/conf.d/) would not be highlighted as XML.

However, the most optimal solution is to highlight as XML all the files that have "<?xml" in the first line & first character. Although, this feature is not present (this could also apply to other headers / shebangs, for example, highlight with Bash the files that begin with "#! /bin/sh" or "#! /bin/bash", regardless of the file extension).

Also, you can use Modelines in Kate:
<!-- kate: syntax XML; -->
Comment 2 Ongun Kanat 2018-12-19 20:19:15 UTC
The thing is MIME-Type is independent from the extension and even simple programs like "file" can determine that a file is an XML file. 

$ file conf.d/10-default-fonts.conf 
conf.d/10-default-fonts.conf: XML 1.0 document, ASCII text
Comment 3 Nibaldo G. 2019-05-13 06:55:09 UTC
*** Bug 407297 has been marked as a duplicate of this bug. ***
Comment 4 Nibaldo G. 2019-05-19 11:31:19 UTC
For syntax highlighting, the extensions have priority over the MIME type, and the extension ".conf" is assigned the "INI Files" highlighter.
Another simple solution is to remove "*.conf" from the "INI Files" highlighter; then the .conf extension will not have an assigned syntax highlight and the MIME type will be used to define the appropriate highlight. There the XML highlighting will be used by default in the .conf files that have XML content, as in the case of Fontconfig files.

That is a good solution, since the extension .conf is very generic to assign it to a specific highlighter.
Comment 5 Nibaldo G. 2019-05-19 11:36:50 UTC
Proposed patch: https://phabricator.kde.org/D21288
Comment 6 Nibaldo G. 2019-05-19 11:48:58 UTC
Git commit 71446d3e9e5eda6b524be81726dd461f7c942f1e by Nibaldo González.
Committed on 19/05/2019 at 11:48.
Pushed by ngonzalez into branch 'master'.

Remove .conf extension from "INI Files" hl, to determine the highlighter using MIME type

Summary:

The extension `.conf` is very generic, since depending on the context, it can be used with a different language, and assigning it to the "INI Files" highlighter isn't very convenient.
For example, in "Fontconfig" the XML files have a `.conf` extension.

Therefore, I propose that the `.conf` extension isn't assigned to any syntax highlighter, so use the MIME type to determine the highlighting mode in those files.

For example, when applying this patch, the Fontconfig files (located in `/etc/fonts/`) are highlighted as XML by default.

Reviewers: #framework_syntax_highlighting, dhaumann, cullmann

Reviewed By: #framework_syntax_highlighting, cullmann

Subscribers: kwrite-devel, kde-frameworks-devel

Tags: #kate, #frameworks

Differential Revision: https://phabricator.kde.org/D21288

M  +1    -1    data/syntax/ini.xml

https://commits.kde.org/syntax-highlighting/71446d3e9e5eda6b524be81726dd461f7c942f1e