Enable automatic commenting in Sublime Text for a custom syntax

空扰寡人 提交于 2019-12-04 16:30:26

问题


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?


回答1:


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>



来源:https://stackoverflow.com/questions/18239767/enable-automatic-commenting-in-sublime-text-for-a-custom-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!