J.Doe
J.Doe

Reputation: 33

How to choose different comment marks?

In php we can use either # or // as line comments, and it is configured by default in the Comments.tmPreferences file as follows:

<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>

I find the documents in https://docs.sublimetext.io/reference/comments.html, additional line comment marks can be used. But it didn't tell how to choose between them. My question is can I set up separate key bindings for each TM_COMMENT_START mark? And how? This is the default key map setting:

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

which args should I use to choose different TM_COMMENT_START?

Upvotes: 3

Views: 187

Answers (1)

Keith Hall
Keith Hall

Reputation: 16065

Looking at Packages/Default/comment.py, one can see that this is currently not possible without making some changes to the code, as it is hardcoded to always use the first block or line comment character defined in the tmPreferences file.

The following change can be made:

--- Shipped Packages/Default/comment.py 2018-10-11 19:11:54
+++ Packages/Default/comment.py 2018-10-22 10:54:03
@@ -168,7 +168,7 @@
         for pos in start_positions:
             view.insert(edit, pos, start)

-    def add_comment(self, view, edit, comment_data, prefer_block, region):
+    def add_comment(self, view, edit, comment_data, prefer_block, region, preferred_index):
         (line_comments, block_comments) = comment_data

         if len(line_comments) == 0 and len(block_comments) == 0:
@@ -183,19 +183,19 @@
         if region.empty():
             if prefer_block:
                 # add the block comment
-                self.block_comment_region(view, edit, block_comments[0], region)
+                self.block_comment_region(view, edit, block_comments[preferred_index], region)
             else:
                 # comment out the line
-                self.line_comment_region(view, edit, line_comments[0], region)
+                self.line_comment_region(view, edit, line_comments[preferred_index], region)
         else:
             if prefer_block:
                 # add the block comment
-                self.block_comment_region(view, edit, block_comments[0], region)
+                self.block_comment_region(view, edit, block_comments[preferred_index], region)
             else:
                 # add a line comment to each line
-                self.line_comment_region(view, edit, line_comments[0], region)
-
-    def run(self, edit, block=False):
+                self.line_comment_region(view, edit, line_comments[preferred_index], region)
+
+    def run(self, edit, block=False, preferred_index=0):
         for region in self.view.sel():
             comment_data = build_comment_data(self.view, region.begin())
             if (region.end() != self.view.size() and
@@ -222,8 +222,8 @@
                 if self.remove_block_comment(self.view, edit, comment_data, line):
                     continue

-                self.add_comment(self.view, edit, comment_data, block, line)
+                self.add_comment(self.view, edit, comment_data, block, line, preferred_index)
                 continue

             # Add a comment instead
-            self.add_comment(self.view, edit, comment_data, block, region)
+            self.add_comment(self.view, edit, comment_data, block, region, preferred_index)

and then one can modify the arguments sent from the keybinding to include the new preferred_index parameter set to 1 (to represent #) when working in a PHP context:

{ "keys": ["ctrl+/"], "command": "toggle_comment", "args": { "block": false, "preferred_index": 1 }, "context":
    [
        { "key": "selector", "operator": "equal", "operand": "source.php", "match_all": true },
    ],
}

Upvotes: 3

Related Questions