Digital Ninja
Digital Ninja

Reputation: 3731

How to specify the scope of a snippet between a PHP or a HTML file?

It seems that when examining the scope of a PHP file, whose extension is .php, and whose syntax is correctly recognised as PHP, Sublime will still report it as text.html.basic (among others). This makes my HTML snippet always execute in it.

Since what I want is to make a snippet that has todo as its tabTrigger, to insert a commented out tag, obviously i need it to be different for the different filetypes (<!-- --> or /* */). But this way, the <!-- --> comment gets inserted into the PHP file...

Is there an extra detail i'm missing, or is this a bug?

Upvotes: 1

Views: 299

Answers (2)

MattDMo
MattDMo

Reputation: 102862

The PHP syntax in Sublime Text is an extension of the HTML syntax, meaning all HTML is valid in a .php file, plus the extra <?php … ?> tag. If your cursor is in a .php file, but outside the <?php tag, the scope will be embedding.php text.html.basic and comments should take the <!-- … --> HTML form.

Once you're inside that PHP tag, you now have an additional scope of source.php. Comments now should use PHP comments /* … */ and/or // ….

For your snippet, just set the scope as source.php, and it should only trigger inside actual PHP code.

If you want your snippet to trigger only in HTML, and not in PHP, set the source to text.html.basic -source.php.

Note This is all true in the most recent version of Sublime Text 3 (3.2.2, Build 3211). The PHP syntax was entirely refactored earlier, so this may not work correctly with earlier builds. If you haven't upgraded, now is a great time!

Upvotes: 2

idleberg
idleberg

Reputation: 12882

In side your PHP tags, you need to use the source.php, while outside of the tags it's text.html.php. You likely want something like this:

PHP

<snippet>
  <content><![CDATA[/* TODO $1 */]]></content>
  <tabTrigger>todo</tabTrigger>
  <scope>source.php</scope>
</snippet>

HTML (PHP)

<snippet>
  <content><![CDATA[<!-- TODO $1 -->]]></content>
  <tabTrigger>todo</tabTrigger>
  <scope>text.html.php</scope>
</snippet>

Upvotes: 0

Related Questions