How to add your own syntax to Sublime Text 2

118 views Asked by At

I want to add my own syntax (language) to Sublime Text 2. I already know how to include it to the syntax list so my only problem now is the syntax highlighting. But first I want to understand the Oniguruma language used for matching the words I want to highlight so I can make things on my own.

Here's my code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>fileTypes</key>
    <array>
        <string>xmp</string>
    </array>
    <key>name</key>
    <string>XMP</string>
    <key>patterns</key>
    <array>
        <dict>
            <key>match</key>
            <string>??????????????????????????????????????????????</string>
            <key>name</key>
            <string>source.keyword.xmp</string>
        </dict>
    </array>
    <key>scopeName</key>
    <string>source.xmp</string>
    <key>uuid</key>
    <string>750d3b02-a1bc-4b09-a794-1b78c54a4e21</string>
</dict>
</plist>

And just like the other programming languages (e.g HTML or XML), instead of using <> (in HTML) I'll use [] and also the / for closing the statement. So basically I'll just replace those <> to [] but how can I translate it to Oniguruma (regex)?


Here's what I want it to be

<key>patterns</key>
<array>
    <dict>
        <key>match</key>
        <string>[SIZE=""][/SIZE]</string>
        <key>name</key>
        <string>code.size.xmp</string>
    </dict>
    <dict>
        <key>match</key>
        <string>[BOLD][/BOLD]</string>
        <key>name</key>
        <string>code.bold.xmp</string>
    </dict>
</array>
2

There are 2 answers

0
Kerwin On
replace: /<(\/)?(.*?)>/g
     to: [\1\2]

https://regex101.com/r/zC8mG5/8

0
JnFrks On

Honestly I can't understand what you're pointing to...

Perhaps I did understand the substitution [\1\2] that you're saying. Based on the explanation, it'll only match any character except newlines (please correct me if I'm wrong) but I want to match the exact word and the symbols [size=""] not any random characters. And I also want an auto listing function...

Like, if I'm going to type [ it'll automatically list those only possible exact words included to my language, just like the other default programming languages that Sublime Text already has. (e.g Java)

Yes, not only the codes that I wanted to be translated to regex but also the syntax-highlighting (coloring) and the automatic listing function I'm talking about...

Here's just an examples of my codes

[SIZE=""]texts here[/SIZE]          Will change the font size. (Usage [SIZE="integer"])
[BOLD]texts here[/BOLD]             Will change the font weight.
[CENTER]texts here[/CENTER]         Will align texts to center.
[ITALIC]texts here[/ITALIC]         ...
[UNDERLINE]texts here[/UNDERLINE]   ...
[STRIKE]texts here[/STRIKE]         ...
....

Thanks for the answer tho :)