Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable automatic commenting in Sublime Text for a custom syntax

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?

like image 588
SethMMorton Avatar asked Aug 14 '13 18:08

SethMMorton


1 Answers

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>

like image 62
d_rail Avatar answered Oct 20 '22 14:10

d_rail