Enable automatic commenting in Sublime Text for a custom syntax
Asked Answered
M

1

15

I have created a .tmLanuage file for a custom language in Sublime Text. Everything is working well, except that I can't seem to get automatic commenting to work. I can't seem to find anything in the Sublime Text docs or on Google about how to do this, but perhaps this is because I am not using the right keywords.

Let me explain what I mean. Let's say I have the following C code:

int i = 1;
i += 2;

If I highlight this in Sublime Text and press ctrl+/, it gets changed to

// int i = 1;
// i += 2;

Similarly, for Python code:

i = 1
i += 2

would become

# i = 1
# i += 2

Clearly Sublime Text has to know about the language syntax in order to choose the proper comment character, which is why I assume I need to add something to my .tmLanguage file to get this to work. I took a look through the C.tmLanguage and Python.tmLanguage files that come with Sublime Text, and nothing jumped out at me as being the code that does this automatic commenting.

What do I have to add to my .tmLanguage file to enable this feature within Sublime Text? Or, is there some other file I must add/modify to enable this feature?

Mallen answered 14/8, 2013 at 18:43 Comment(0)
A
15

Take a look at "Comments (C++).tmPreferences" and you should be able to figure out how to edit it for your syntax.

  • Add your syntax's "scopeName" to the scope
  • TM_COMMENT_START = line comments
  • TM_COMMENT_START_2 / TM_COMMENT_END_2 = block comments

Comments (C++).tmPreferences:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>name</key>
    <string>Comments</string>
    <key>scope</key>
    <string>source.your_syntax</string>
    <key>settings</key>
    <dict>
        <key>shellVariables</key>
        <array>
            <dict>
                <key>name</key>
                <string>TM_COMMENT_START</string>
                <key>value</key>
                <string>// </string>
            </dict>
            <dict>
                <key>name</key>
                    <string>TM_COMMENT_START_2</string>
                <key>value</key>
                <string>/*</string>
            </dict>
            <dict>
                <key>name</key>
                <string>TM_COMMENT_END_2</string>
                <key>value</key>
                <string>*/</string>
            </dict>
            <dict>
                <key>name</key>
                <string>TM_COMMENT_DISABLE_INDENT_2</string>
                <key>value</key>
                <string>yes</string>
            </dict>
        </array>
    </dict>

Andrade answered 14/8, 2013 at 21:39 Comment(2)
Thanks! My issue clearly was that I was looking in the wrong file for examples.Mallen
Thanks AGS for the edit, I guess you have to use hard tabs for xml code.Andrade

© 2022 - 2024 — McMap. All rights reserved.