Search code examples
fortransublimetext3

How do you make Sublime Text 3 toggle comments on/off for Fortran?


I have written a syntax highlighter for Fortran, which properly highlights comments. The problem is that the ctrl+/ shortcut does not toggle comments on or off.

Is there something settings specific I need to add to enable this?

For reference, I used the standard *.sublime-syntax file structure that Sublime provides to build the highlighter. So, it looks something like:

%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
file_extensions: [f]
scope: source.fortran
contexts:
  main:
    # Comments begin with a '!' and finish at the end of the line
    - match: '!'
      scope: punctuation.definition.comment.fortran
      push: line_comment

  line_comment:
    - meta_scope: comment.line.fortran
    - match: $
      pop: true

Also, I'm using it on RedHat 7.


Solution

  • You need to create a file called Comments.tmPreferences in the same folder as your Fortran.sublime-syntax file, with the following contents:

    <?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>Comments</string>
        <key>scope</key>
        <string>source.fortran</string>
        <key>settings</key>
        <dict>
            <key>shellVariables</key>
            <array>
                <dict>
                    <key>name</key>
                    <string>TM_COMMENT_START</string>
                    <key>value</key>
                    <string>! </string>
                </dict>
            </array>
        </dict>
        <key>uuid</key>
        <string>65507EAD-D547-44E4-84F7-7D421DD078B0</string>
    </dict>
    </plist>
    

    I don't think the UUID is needed in Sublime (these files were originally developed for use in the Mac editor TextMate), so feel free to get rid of it if you want. Other than that, this should do what you want it to do - insert ! at the beginning of any selected line when you hit Ctrl/.