3

So I've added:

    <dict>
        <key>name</key>
        <string>JSON Key</string>
        <key>scope</key>
        <string>meta.structure.dictionary.json string2.quoted.double.json</string>
        <key>settings</key>
        <dict>
            <key>foreground</key>
            <string>#00FF00</string>
        </dict>
    </dict>

to my color scheme called custom.tmTheme and I've added:

    <key>string2</key>
    <dict>
        <key>begin</key>
        <string>"</string>
        <key>beginCaptures</key>
        <dict>
            <key>0</key>
            <dict>
                <key>name</key>
                <string>punctuation.definition.string2.begin.json</string>
            </dict>
        </dict>
        <key>end</key>
        <string>"\s.:</string>
        <key>endCaptures</key>
        <dict>
            <key>0</key>
            <dict>
                <key>name</key>
                <string>punctuation.definition.string2.end.json</string>
            </dict>
        </dict>
        <key>name</key>
        <string>string2.quoted.double.json</string>
        <key>patterns</key>
        <array>
            <dict>
                <key>match</key>
                <string>(?x:                # turn on extended mode
                 \\                # a literal backslash
                 (?:               # ...followed by...
                   ["\\/bfnrt]     # one of these characters
                   |               # ...or...
                   u               # a u
                   [0-9a-fA-F]{4}  # and four hex digits
                 )
               )</string>
                <key>name</key>
                <string>constant.character.escape.json</string>
            </dict>
            <dict>
                <key>match</key>
                <string>\\.</string>
                <key>name</key>
                <string>invalid.illegal.unrecognized-string-escape.json</string>
            </dict>
        </array>
    </dict>

to a JSON-test.tmLanguage file in my packages folder, but yet I still don't see green keys.

Does anyone have any idea where I went wrong?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Jason Nichols
  • 3,739
  • 3
  • 29
  • 49
  • For anyone, who is looking for a nice way to add color to their theme for JSON or just wants the Monokai+JSON support, check out MonokaiJSON+ project: https://goo.gl/39ZBnA – electronix384128 Dec 21 '14 at 21:04

1 Answers1

5

You don't need to create or modify a .tmLanguage file, you should be able to do this simply using the JavaScript -> JSON syntax. Modify your .tmTheme like so:

<dict>
    <key>name</key>
    <string>JSON Key</string>
    <key>scope</key>
    <string>meta.structure.dictionary.json string.quoted.double.json -meta.structure.dictionary.value.json</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#00FF00</string>
    </dict>
</dict>

and you should be all set. It will highlight all strings contained within JSON dictionaries, but not if the scope contains meta.structure.dictionary.value.json, so it will only highlight the keys.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • First off, upvotes, for a nifty solution. Thanks! Second though, this doesn't change the keys of sub entries i.e. `{'key1' : {'key2' : 'value2'}}` key2 doesn't change because it's part of the value of key1. I really want clarity on the syntax creation and grammar as well, just out of personal interest. – Jason Nichols May 02 '14 at 15:32
  • 1
    @JasonNichols check out my answer [here](http://stackoverflow.com/a/21213086/1426065) for a possible solution. Each level (up to 18 deep) is highlighted in a different color, and the keys and values are different colors so they can be easily distinguished. The relevant lines of the `.tmTheme` file are on Github [here](https://github.com/MattDMo/Neon-color-scheme/blob/master/Neon.tmTheme#L1150-L1363) if you don't want to use the entire theme. – MattDMo May 02 '14 at 15:47