gravitystorm / openstreetmap-carto

A general-purpose OpenStreetMap mapnik style, in CartoCSS
Other
1.54k stars 823 forks source link

Syntax highlighting #4678

Closed sommerluk closed 2 years ago

sommerluk commented 2 years ago

In this project, we use a number of different file formats, but there are two file formats that are particularly important because they contain the main code of this project: The .mml files and the .mss files.

However, these two file formats are not as well-supported by syntax highlighting engines. For the .mml files, we are dealing with Yaml, for which there is broad syntax highlighting support, but we use a lot of SQL in these Yaml files, and this SQL is usually not highlighted at all (at least, I am not aware of editors that do this). However, for .mss there is syntax highlighting in some editors. It can also be highlighted like CSS, but this doesn't make most of the special features in .mss files visible.

Especially the .mml file, I found hard to read. So I played around a bit and came up with a dirty four-line hack to highlight the SQL in our .mml file. This keeps the yaml parts and the SQL parts visually different, which prevents confusion.

Color Scheme Breeze Dark: BreezeDark

Color Scheme Breeze Light: BreezeLight

The hack is for the KSyntaxHighlighting library and works with editors that use this library: Kate, KWrite, KDevelop.... To use it, just copy the attached carto-css-mml.xml to $HOME/.local/share/org.kde.syntax-highlighting/syntax/ (If this directory doesn't exist, create it first.) If Kate is running, close it. Then reopen the project.mml file in Kate, and you will get this highlighting. You can also force this highlighting on other files in Kate by using Tools → Highlighting → Markup → CartoCSS MML.

This hack enables the default SQL (PostgreSQL) highlighting for all fields starting with key: |- (newline). This is a good fit for our project.mml file, but is not the formally correct rule.

I'd like to hear if other people will find this useful and actually use it. If so, I'd be interested to hear if this works as expected and if it's worth creating a really sophisticated syntax highlighting (rather than a dirty hack).

For the .mss files, there has been syntax highlighting support in KSyntaxHighlighting for a few years now, including support for more exotic features like attachments (::fill, see top of screenshot) or in-string data fields (motorway_[width]x[height].svg, see bottom of screenshot). Recently, I also added support for functions like url("some-text"), but this change has not been submitted to the upstream yet. However, you can install the attached file, just like you can with .mml highlighting. In Kate, it will be located under Tools → Highlighting → Markup → CartoCSS MML.

Color Scheme Breeze Dark: CartoBreezeDark

Color Scheme Breeze Light: CartoBreezeLight

Also for the .mss syntax highlighting, I would be interested in hearing from you if you actually use it and if there are any missing features.

Note that both syntax highlighting schemes adapt to the color scheme you are currently using. They can also be customized in Kate to distinguish even more different syntax parts with even more colors, if you wish to do so.

syntax.zip

StyXman commented 2 years ago

I'd like to hear if other people will find this useful and actually use it

I would. I'll try to test it during this week and provide feedback.

pnorman commented 2 years ago

This hack enables the default SQL (PostgreSQL) highlighting for all fields starting with key: |- (newline). This is a good fit for our project.mml file, but is not the formally correct rule.

I don't use a KSyntaxHighlighting based editor, but but look into embedded languages. This is what you'd use for something like a CSS style block in HTML (e.g. https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#embedded-languages)

https://github.com/yohanboniface/carto-atom might be relevant to those using TextMate-based highlighting

sommerluk commented 2 years ago

Here is a new version: syntax.zip

It has the following improvements: