Sublime Forum

Insert_snippet not working with meta tags

#1

Hi, I have an insert_snippet as follows

<snippet>
    <content>
        <![CDATA[
{/**${SELECTION} **/}
]]>
    </content>
    <tabTrigger>comment</tabTrigger>
    <description>Comment React code</description>
    <scope>meta.jsx.js, meta.tag.jsx</scope>
</snippet>

This will basically comment out the selected JSX code. My file has an extension .js. I have also configured this snippet to work when alt+tshift+t is pressed.

However, I am finding that the above snippet is also working on the following scopes as well (within the same file) which seems contrary to what is mentioned in the scope of the snippet.

source.js meta.class.js meta.class.property.js meta.group.braces.curly.js variable.other.readwrite.js

Can you please let me know why this is happening?

Thanks

0 Likes

#2

The scope in the snippet only applies to using the tab trigger or the command palette entry that’s added automatically for the snippet. When you use the insert_snippet command, it just always inserts the snippet file or snippet contents (depending on the argument you give) without doing any scope check.

If you’re using a key binding, you need to add a context to the binding that uses the same scope so that the key binding knows to not be active in the wrong places.

0 Likes

#3

That did the trick, thanks for the help, Sir. Much appreciated.

0 Likes

#4

Hi, I have a few other questions please .

  1. Is there a way I can use the above snippet for the current line ? What I do now is I have to select the entire line and then press the key combination to comment it out as it only targets the currently selected text.

  2. Secondly, I want this snippet to work based on the key combination cmd+/. This is the same key combination that I use to toggle comments for JS code . I have JS and JSX code in the same file and having one key throughout will aid my development . If its JS code, I want the normal functionality however, if the scope is JSX , I want my snippet to start doing its work.

Please suggest.

Thanks

0 Likes

#5

For something that you can do manually by taking multiple actions, you can use a macro.:

[
    {
        "command": "expand_selection",
        "args": {"to": "line"}
    },
    {
        "command": "move",
        "args": {
            "by": "characters",
            "forward": false,
            "extend": true
        },
    },
    {
        "command": "insert_snippet",
        "args": {
              "contents": "{/**${SELECTION} **/}"
          }
    }
]

This expands the selection to the current line, then moves left once (otherwise you catch the end of the line in the selection) and inserts the snippet content.

Saved in your User package as jsx_comment.sublime-macro, you can bind it to a key using the run_macro_file command (apply a context as appropriate):

{
    "keys": ["ctrl+alt+t"],
    "command": "run_macro_file",
    "args": {
        "file": "res://Packages/User/jsx_comment.sublime-macro"
    },
},

You can apply a context that makes a binding apply only for the JSX files (assuming their scope is unique) and bind it to the same key that you would otherwise use; it will take precedence over the other one automatically.

0 Likes