guillermooo / sublime-undocs

Sublime Text Unofficial Documentation
http://docs.sublimetext.info
Other
605 stars 333 forks source link

Snippets - custom variables (.sublime-options files?) #226

Open keith-hall opened 7 years ago

keith-hall commented 7 years ago

The snippets documentation refers to custom variables and states that they can be defined in .sublime-options files. https://github.com/guillermooo/sublime-undocs/blame/8a3b31288b1e329263c65701b5c133dc8755bc27/source/extensibility/snippets.rst#L105

However, there is no other mention of .sublime-options files in the documentation, specifically, what file format they should be. Is this information actually true?

In my experience, custom variables can be defined in tmPreferences files, in the shellVariables dict section. As you know, shellVariables are used to define TM_COMMENT_START and TM_COMMENT_END, which can be used in snippets. Example: https://github.com/sublimehq/Packages/blob/4db940a44b74727ade298927d5ffdb8826fc9769/HTML/Comments.tmPreferences#L14-L16

It therefore stands to reason that other variables can be defined here too. Example/proof:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
    <key>name</key>
    <string>Comments</string>
    <key>scope</key>
    <string>text.html</string>
    <key>settings</key>
    <dict>
        <key>shellVariables</key>
        <array>
            <dict>
                <key>name</key>
                <string>TM_COMMENT_START</string>
                <key>value</key>
                <string>&lt;!-- </string>
            </dict>
            <dict>
                <key>name</key>
                <string>TM_COMMENT_END</string>
                <key>value</key>
                <string> --&gt;</string>
            </dict>
            <dict>
                <key>name</key>
                <string>HELLO_WORLD</string>
                <key>value</key>
                <string>wow!</string>
            </dict>
        </array>
    </dict>
</dict>
</plist>

Snippet:

<snippet>
    <content><![CDATA[
Hello, ${1:this} is a ${2:snippet}. $HELLO_WORLD
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <!-- <tabTrigger>hello</tabTrigger> -->
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

Output when inserting snippet in a HTML file:

Hello, this is a snippet. wow!

Therefore, please can the documentation be updated to reflect the facts.

FichteFoll commented 7 years ago

I feel like I've read this before ...? But yeah, this is definitely a (known) error.

keith-hall commented 7 years ago

I had the idea I've mentioned it before, but I couldn't find it anywhere... if it is a dupe, I apologise!

davipatti commented 7 years ago

I cannot reproduce keith-hall's original post. Further documentation on how to implement custom variables for snippets would be really appreciated.

luisobo commented 6 years ago

I'm on the same boat. I could reproduce thee example, defining custom variables in a Default.tmPreferences file under User.

I can confirm that the documentation seems wrong.

FichteFoll commented 6 years ago

The documentation is objectively wrong on this part. I haven't gotten around to working on it, however. Pull requests are welcome.

apjanke commented 5 years ago

Ping on this. Any progress? I'm trying to get $TM_FULLNAME an $TM_YEAR in the Octave-function.sublime-snippet from the Matlab package working, but not having any luck.