Sublime Forum

Adding support for Cmd + /

#1

I’m editing a .g file (ANTLR4), the Cmd + / combination does not comment out the selection.

So I’m wondering how can I add the support? It’s the same as C language aka /* .. */ style.

0 Likes

#2

http://docs.sublimetext.info/en/latest/reference/comments.html?highlight=comment

2 Likes

#3

I have defined a antlr.tmPreferences, and it works

However it only supports comments like # xxx, how can I make it support both /* */ and # ?

e.g when I have no selection, precede the line with a # ; otherwise wrap the selection with /* */

<?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>Miscellaneous</string>
        <key>scope</key>
        <string>source.antlr</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>
            </array>
        </dict>
    </dict>
</plist>
0 Likes

#4

The toggle_comment command that uses these settings takes a parameter that tells it if it should comment the text as a single line or as a block, so by default there are actually two key combinations used here::

Windows/Linux

{ "keys": ["ctrl+/"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+/"], "command": "toggle_comment", "args": { "block": true } },

MacOS

{ "keys": ["super+forward_slash"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["super+alt+forward_slash"], "command": "toggle_comment", "args": { "block": true } },

The file you’ve outlined above will work for block comments as well, only you need to hit a different set of keys to get it to work for you. In order to have a single key combination do both you would need a a plugin that invokes that command for you with an appropriate parameter. For example:

import sublime, sublime_plugin

class ToggleCommentEnhancedCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # Get the selection
        selection = self.view.sel ()[0]

        # Determine what line in the file the selection starts and ends at
        start_line = self.view.rowcol (selection.a)[0]
        end_line = self.view.rowcol (selection.b)[0]

        # Toggle; assume a block if they're not the same line
        self.view.run_command ('toggle_comment', {"block": start_line != end_line})

If you re-map the key that toggles comments for you to invoke toggle_comment_enhanced (with no arguments), the single key will assume single line if the selection is on a single line (which includes no selection at all) or a block selection if the selection spans lines.

4 Likes