sublimehq / Packages

Syntax highlighting files shipped with Sublime Text and Sublime Merge
https://sublimetext.com
Other
2.95k stars 587 forks source link

[RFC] `meta.embedded` vs. `meta.interpolation` #3051

Open deathaxe opened 3 years ago

deathaxe commented 3 years ago

What happened?

Relational

Template syntaxes such as ASP/ERB/JSP/PHP are html (or text) files which use <?...?> or <%...%> to mark active code being executed by the interpreter to expand the template's content.

Those parts are scoped meta.embedded source.xyz.embedded.html and their punctuations punctuation.section.embedded.

During ST4 dev cycle, string interpolation was introduced. It uses meta.string meta.interpolation to scope active code which is executed to expand a template string within source code. Python's f-strings (e.g. f"The {foo + bar}") are a very prominent example among others.

Issue

What is the difference between embedded code in HTML templates and interpolated code in template strings?

What happens if embedded code (<%...%>) is located within a string in a HTML template, such as <div class="<% embedded %>">?

Is it still embedded, is it interpolation or is it both?

What scope is used for punctuations then: punctuation.section.embedded, punctuation.section.interpolation or both?

Things become even more weird in case of HAML (Rails) or HEEx (Elixir) syntaxes, which support traditional <%...%> blocks as well as other kinds of interploation syntaxes such as {...}.

HEEx

     // Interpolation
     <p {attr_name}={"interpolated-string-value"} >

     // Embedded
     <% func arg %>

     </p>

PHP

<p {attr_name}="<?php echo "string with{$interpolation}" ?>" ><?php echo $content;?></p>

Here we have embedded code block which echo's a string with interpolation into a HTML string. Except none of that expression being correctly scoped as interpolation by current PHP syntax at the time writing this - how would the most inner part {$interpolation} be scoped?

Maybe meta.string.html meta.interpolation.html meta.embedded.html meta.string.php meta.interpolation.php?

Would we omit meta.interpolation.html and keep it scoped meta.embedded only?

Suggestions

Basically, I think we have two sane options

  1. Scope everything just meta.interpolation and abandon meta.embedded.
  2. Keep both scopes, but don't stack meta.interpolation meta.embedded within quoted strings.
willrowe commented 2 weeks ago

In this example:

<p {attr_name}="<?php echo "string with{$interpolation}" ?>" ><?php echo $content;?></p>

Everything between the <?php and ?> is embedded in HTML. PHP ignores everything up until <?php, then starts processing as PHP and then stops processing when it gets to ?>. The examples just happens to have the first embedded code in between quotes, but this is also valid:

<p {attr_name}="<?php echo "string with{$interpolation}\" >" ?><?php echo $content;?></p>

Notice that the end quote, space, and end angle bracket are now echoed out by PHP. PHP has no knowledge of HTML is not aware that it is inside of quotes.

However, string with{$interpolation} is interpolation and is distinct from PHP embedding. PHP handles this directly and the syntax requires that it start with a dollar sign (with or without curly braces) and be within a string enclosed with double quotes. This context is very explicit and is indeed interpolation.

I think that interpolation is very distinct from embeddings since interpolation requires a certain wrapping syntax within a string enclosed in some sort of quotes by most languages, whereas embedded content usually only requires the wrapping syntax and no other context.