Sublime Forum

Toggle Comment with a custom sublime-syntax

#1

I have a custom language syntax that was working fine under the old tmLanguage format. I converted it to a sublime-syntax file, and that was super painless and makes iteration and testing so much better. So, that’s great!

However, I quickly found out that Toggle Comment no longer worked with my language; both the hotkey shortcut and selecting it from the menu were equally broken. I confirmed that it was specific to my syntax, and I also confirmed that if I rolled back to the old format version, that did work correctly.

As far as I can tell… Previously, the sublime-package file also included a tmPreferences that defined what was necessary for this to work. The conversion to sublime-syntax doesn’t seem to handle this, either by including the necessary markup in that file, or creating a new analog to the tmPreferences file. Is this a mistake, or am I missing something in the process?

I finally got Toggle Comment working again by dragging the old Comments.tmPreferences file from my sublime-package into my Packages folder. Since the documentation makes no mention of this, and I couldn’t find anything searching online, is this what I’m supposed to be doing? It certainly functions, but is there a better way? Ideally, I feel like I should be able to include that into my sublime-syntax file, but if I can, I don’t fully know how to do it.

So, any guidance on this would be much appreciated!

5 Likes

How to enable toggle comment for new syntax?
#2

So far, only tmLanguage files have been replaced with sublime-syntax - tmPreferences are still supported and are the only way to achieve this functionality with comments atm, so you were right to copy it :slight_smile:

3 Likes

#3

Sorry for reviving an old thread, but I have a new syntax, works fine, but I have no Comments.tmPreferences file. Is this still the way to go to enable comment toggling? If yes, how do I configure it to work with my syntax and where do I it in the sublime folder?

1 Like

#4

Yes, this is still the way to do this.

You need to create a tmPreferences file that contains the information that tells Sublime how comments work in your particular syntax. Technically the location and name of the file doesn’t matter so long as it’s inside of a package, has the correct contents and is of type tmPreferences, but to keep things understandable you should name it something like Comments.tmPreferences and store it in the same location as your syntax.

For an example of what the content should look like, select the View Package File command from the command palette and enter the text html comments and then open HTML/Comments.tmPreferences.

  • Make sure you change the scope to something appropriate; this is how Sublime knows to use this information for your syntax
  • TM_COMMENT_START and TM_COMMENT_END together create a block comment
  • TM_COMMENT_START used alone makes a line comment
  • You can have multiple rules by adding numbers to the end of the above, for example TM_COMMENT_START and TM_COMMENT_START_2 to create two different ways to comment.
5 Likes

#5

I see, thanks for the answer!

0 Likes

#6

Hi its been sometime since the last response and ST has reached version 4 already.

Is there any update on how to toggle comment with a custom sublime-syntax?

Thanks

0 Likes

#7

I have followed OdatNurd’s post and created a pl_comments.tmPreferences file, and this is the step I have followed to create it:

Sublime Version: Build 4107
(1) Tools > Developer > New Syntax
(2) Paste the following code and renamed the file as “pl_comments.tmPreferences”.
(3) Save the file under /User

<?xml version="1.0" encoding="UTF-8"?> name Comments scope text.pl settings shellVariables name TM_COMMENT_START value // name TM_COMMENT_START_2 value { name TM_COMMENT_END_2 value }

However when I open a file with *.pl extension and press my shortcut for a single line comment on my mac (i.e. Command + /), there is nothing happen. Did I did something wrong? Do I need to restart Sublime to make it work?

Cheers

0 Likes

#8

You shouldn’t have to restart for the change to take effect, no. One possible issue is that the scope you’re using may not be correct. To double check, open a .pl file that this should work in, and then choose Tools > Developer > Show Scope Name; the first line of the scope in the popup is what you should use here.

Possibly it would be source.pl and not text.pl, or something along those lines.

0 Likes

#9

Yes it works! After checking the scope, I have updated the scope name.

Thank you OdatNurd!

1 Like

#10

Well… it’s not working for me
Obviously I’m new to sublime text.

I’ve got a very simple karel.tmPreferences at %appdata%\packages\user. It seems to be working because it is not throwing any error…

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
   <dict>
      <key>name</key> 
      <string>Karel Metadata</string>
      <key>scope</key> 
      <string>source.kl</string>
      <key>settings</key>
      <dict><!-- a compléter --></dict>
      <key>shellVariables</key>
         <dict>
            <key>name</key>
            <string>TM_COMMENT_START</string>
            <key>value</key>
            <string>-- </string>
         </dict>
   </dict>
</plist>

In the same folder I also have a karel.sublime-syntax. Here is an incomplete version of the file

name: Karel
scope: source.kl
version: 2
file_extensions:
- kl

contexts:
#The prototype context is prepended to all contexts but those setting
prototype:
- include: comments

main:
- include: function
- include: strings
- include: keywords
- include: directives
- include: types
- include: symbols

comments:
# Comments begin with a ‘--’ and finish at the end of the line.
- match: --
scope: punctuation.definition.comment.kl
push:
- meta_scope: comment.line.double-hyphen.kl
- match: ‘\n’
pop: true

Any idea what’s wrong with this?

0 Likes

#11

Your XML should be like:

<dict>
   <key>shellVariables</key>
   <array>
      <dict>
         <key>name</key>
         <string>TM_COMMENT_START</string>
         <key>value</key>
         <string>-- </string>
      </dict>
   </array>
</dict>

I’d suggest looking at other .tmPreferences files for reference. You can do this using View Package File from the command line.

0 Likes