在 Sublime Text 中为自定义语法启用自动注释 [英] Enable automatic commenting in Sublime Text for a custom syntax

查看:51
本文介绍了在 Sublime Text 中为自定义语法启用自动注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Sublime Text 中为自定义语言创建了一个 .tmLanuage 文件.一切都运行良好,除了我似乎无法让自动评论工作.我似乎无法在 Sublime Text 文档或 Google 上找到有关如何执行此操作的任何信息,但这可能是因为我没有使用正确的关键字.

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.

让我解释一下我的意思.假设我有以下 C 代码:

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

int i = 1;
i += 2;

如果我在 Sublime Text 中突出显示并按 ctrl+/,它会变成

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

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

同样,对于 Python 代码:

Similarly, for Python code:

i = 1
i += 2

会变成

# i = 1
# i += 2

显然,Sublime Text 必须了解语言语法才能选择正确的注释字符,这就是为什么我认为我需要在 .tmLanguage 文件中添加一些内容才能使其正常工作.我查看了 Sublime Text 附带的 C.tmLanguage 和 Python.tmLanguage 文件,没有任何东西让我觉得是自动注释的代码.

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.

我必须在我的 .tmLanguage 文件中添加什么才能在 Sublime Text 中启用此功能?或者,我是否必须添加/修改其他文件才能启用此功能?

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?

推荐答案

看看Comments (C++).tmPreferences",您应该能够弄清楚如何为您的语法编辑它.

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

  • 将语法的scopeName"添加到范围
  • TM_COMMENT_START = 行注释
  • TM_COMMENT_START_2/TM_COMMENT_END_2 = 阻止评论

评论 (C++).tmPreferences:

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>

这篇关于在 Sublime Text 中为自定义语法启用自动注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆